| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /* global __dirname */
- /* jshint strict:false */
- "use strict";
- var express = require('express');
- var bodyParser = require('body-parser');
- var slug = require('slug');
- var beautify = require("json-beautify");
- var _ = require('lodash');
- var fs = require('fs');
- var Promise = require('bluebird');
- var Mustache = require('mustache');
- var app = express();
- // var examplesJSON = __dirname + '/exemples/liste.json';
- var sandboxTpml = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
- // var examples = require(examplesJSON);
- Promise.promisifyAll(fs);
- // Initialize Express app: root folder as static, body parsers
- app.use(express.static(__dirname));
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: true }));
- function scandir(path, excludes) {
- excludes = excludes || [];
- return fs.readdirAsync(path)
- .then(dirContent => {
- excludes.forEach(file => {
- var idxInContent = dirContent.indexOf(file);
- if(idxInContent !== -1) {
- dirContent.splice(idxInContent, 1);
- }
- });
- return dirContent;
- })
- }
- function ExampleStore(path) {
- this.rootPath = path;
- this.repos = [];
- }
- ExampleStore.prototype.init = function() {
- const loadRepository = this.loadRepository.bind(this);
- return scandir(this.rootPath, ['.gitkeep'])
- .then(repositories => Promise.map(
- repositories, loadRepository
- ));
- };
- ExampleStore.prototype.loadRepository = function(repoPath) {
- const loadExample = this.loadExample.bind(this);
- const fullPath = this.rootPath + '/' + repoPath;
- const repoConfig = require(fullPath + '/repo-config.json');
- const repoDescriptor = {
- title: repoConfig.title,
- path: repoPath,
- examples: []
- };
- this.repos.push(repoDescriptor);
- return scandir(fullPath, ['.gitkeep', 'repo-config.json'])
- .then(examples => Promise.map(
- examples, example => loadExample(repoDescriptor, example)
- ))
- };
- ExampleStore.prototype.loadExample = function(repo, example) {
- var exampleConfig = require(repo.path + '/' + example + '/config.json');
-
- };
- var es = new ExampleStore(__dirname + '/exemples');
- es.init();
- function addExample(slug, title) {
- examples.push({ slug: slug, title: title });
- return fs.writeFileAsync(examplesJSON, beautify(examples, null, 2, 100));
- }
- function readConfigJson(exampleSlug) {
- console.log(exampleSlug);
- return require('./exemples/' + exampleSlug + '/config.json');
- }
- function readFileAsync(file) {
- return fs.readFileAsync(file)
- .then(buf => (buf.toString()));
- }
- // function readFilesAsync(path, files) {
- // // console.log('reading files', files, 'from path', path);
- // return Promise.map(files,
- // f => readFileAsync(path + '/' + f)
- // );
- // }
- // function readExampleFiles(slug, config) {
- // const exampleDir = __dirname + '/exemples/' + slug;
- // const libsCssDir = __dirname + '/css/vendor';
- // const libsJsDir = __dirname + '/js/vendor';
- // const { html, js, css, libsCss, libsJs } = config;
- // return Promise.all([
- // readFilesAsync(exampleDir, html),
- // readFilesAsync(exampleDir, js),
- // readFilesAsync(exampleDir, css),
- // readFilesAsync(libsJsDir, libsJs),
- // readFilesAsync(libsCssDir, libsCss),
- // ]);
- // }
- app.post('/examples', function(req, res) {
- var title = req.body.title;
- if(! req.body.title) {
- res.status(400).send('Le titre ne peut pas être vide !');
- }
- var existingTitle = _.find(examples, { title: title });
- if(existingTitle) {
- res.status(400).send("L'exemple '" + title + "' existe déjà !");
- }
- var exampleSlug = slug(req.body.title.toLowerCase());
- var targetDir = __dirname + '/exemples/' + exampleSlug;
- fs.mkdirAsync(targetDir)
- .then(() => Promise.map(
- ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
- ))
- .then(files => addExample(exampleSlug, title))
- .then(() => res.json({ slug: exampleSlug, title: title }));
- });
- app.get('/examples/:slug', function(req, res) {
- const { slug } = req.params;
- const config = readConfigJson(slug);
- const { title, html, js, css, libsCss, libsJs } = config;
- readFileAsync(__dirname + '/exemples/' + slug + '/example.html')
- .then(body =>
- Mustache.render(sandboxTpml, { body, slug, title, js, css, libsCss, libsJs })
- )
- .then(html => res.send(html));
- });
- app.put('/examples/:slug', function(req, res) {
- var slug = req.params.slug;
- var existing = _.find(examples, { slug: slug });
- if(! existing) {
- res.status(404).send("L'exemple avec l'identifiant '" + slug + "' est introuvable !");
- }
- var targetDir = __dirname + '/exemples/' + slug;
- if(req.body.html) {
- fs.writeFileSync(targetDir + '/contenu.html', req.body.html);
- }
- if(req.body.javascript) {
- fs.writeFileSync(targetDir + '/script.js', req.body.javascript);
- }
- var theDate = new Date();
- console.log(theDate.getHours() + ':' + theDate.getMinutes() + " - Sauvegarde de l'exemple '" + existing.title + " effectuée'");
- res.json({ success: true });
- });
- module.exports = app;
|