sandboxApp.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* global __dirname */
  2. /* jshint strict:false */
  3. "use strict";
  4. var express = require('express');
  5. var bodyParser = require('body-parser');
  6. var slug = require('slug');
  7. var beautify = require("json-beautify");
  8. var _ = require('lodash');
  9. var fs = require('fs');
  10. var Promise = require('bluebird');
  11. var Mustache = require('mustache');
  12. var app = express();
  13. // var examplesJSON = __dirname + '/exemples/liste.json';
  14. var sandboxTpml = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
  15. // var examples = require(examplesJSON);
  16. Promise.promisifyAll(fs);
  17. // Initialize Express app: root folder as static, body parsers
  18. app.use(express.static(__dirname));
  19. app.use(bodyParser.json());
  20. app.use(bodyParser.urlencoded({ extended: true }));
  21. function scandir(path, excludes) {
  22. excludes = excludes || [];
  23. return fs.readdirAsync(path)
  24. .then(dirContent => {
  25. excludes.forEach(file => {
  26. var idxInContent = dirContent.indexOf(file);
  27. if(idxInContent !== -1) {
  28. dirContent.splice(idxInContent, 1);
  29. }
  30. });
  31. return dirContent;
  32. })
  33. }
  34. function ExampleStore(path) {
  35. this.rootPath = path;
  36. this.repos = [];
  37. }
  38. ExampleStore.prototype.init = function() {
  39. const loadRepository = this.loadRepository.bind(this);
  40. return scandir(this.rootPath, ['.gitkeep'])
  41. .then(repositories => Promise.map(
  42. repositories, loadRepository
  43. ));
  44. };
  45. ExampleStore.prototype.loadRepository = function(repoPath) {
  46. const loadExample = this.loadExample.bind(this);
  47. const fullPath = this.rootPath + '/' + repoPath;
  48. const repoConfig = require(fullPath + '/repo-config.json');
  49. const repoDescriptor = {
  50. title: repoConfig.title,
  51. path: repoPath,
  52. examples: []
  53. };
  54. this.repos.push(repoDescriptor);
  55. return scandir(fullPath, ['.gitkeep', 'repo-config.json'])
  56. .then(examples => Promise.map(
  57. examples, example => loadExample(repoDescriptor, example)
  58. ))
  59. };
  60. ExampleStore.prototype.loadExample = function(repo, example) {
  61. var exampleConfig = require(repo.path + '/' + example + '/config.json');
  62. };
  63. var es = new ExampleStore(__dirname + '/exemples');
  64. es.init();
  65. function addExample(slug, title) {
  66. examples.push({ slug: slug, title: title });
  67. return fs.writeFileAsync(examplesJSON, beautify(examples, null, 2, 100));
  68. }
  69. function readConfigJson(exampleSlug) {
  70. console.log(exampleSlug);
  71. return require('./exemples/' + exampleSlug + '/config.json');
  72. }
  73. function readFileAsync(file) {
  74. return fs.readFileAsync(file)
  75. .then(buf => (buf.toString()));
  76. }
  77. // function readFilesAsync(path, files) {
  78. // // console.log('reading files', files, 'from path', path);
  79. // return Promise.map(files,
  80. // f => readFileAsync(path + '/' + f)
  81. // );
  82. // }
  83. // function readExampleFiles(slug, config) {
  84. // const exampleDir = __dirname + '/exemples/' + slug;
  85. // const libsCssDir = __dirname + '/css/vendor';
  86. // const libsJsDir = __dirname + '/js/vendor';
  87. // const { html, js, css, libsCss, libsJs } = config;
  88. // return Promise.all([
  89. // readFilesAsync(exampleDir, html),
  90. // readFilesAsync(exampleDir, js),
  91. // readFilesAsync(exampleDir, css),
  92. // readFilesAsync(libsJsDir, libsJs),
  93. // readFilesAsync(libsCssDir, libsCss),
  94. // ]);
  95. // }
  96. app.post('/examples', function(req, res) {
  97. var title = req.body.title;
  98. if(! req.body.title) {
  99. res.status(400).send('Le titre ne peut pas être vide !');
  100. }
  101. var existingTitle = _.find(examples, { title: title });
  102. if(existingTitle) {
  103. res.status(400).send("L'exemple '" + title + "' existe déjà !");
  104. }
  105. var exampleSlug = slug(req.body.title.toLowerCase());
  106. var targetDir = __dirname + '/exemples/' + exampleSlug;
  107. fs.mkdirAsync(targetDir)
  108. .then(() => Promise.map(
  109. ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
  110. ))
  111. .then(files => addExample(exampleSlug, title))
  112. .then(() => res.json({ slug: exampleSlug, title: title }));
  113. });
  114. app.get('/examples/:slug', function(req, res) {
  115. const { slug } = req.params;
  116. const config = readConfigJson(slug);
  117. const { title, html, js, css, libsCss, libsJs } = config;
  118. readFileAsync(__dirname + '/exemples/' + slug + '/example.html')
  119. .then(body =>
  120. Mustache.render(sandboxTpml, { body, slug, title, js, css, libsCss, libsJs })
  121. )
  122. .then(html => res.send(html));
  123. });
  124. app.put('/examples/:slug', function(req, res) {
  125. var slug = req.params.slug;
  126. var existing = _.find(examples, { slug: slug });
  127. if(! existing) {
  128. res.status(404).send("L'exemple avec l'identifiant '" + slug + "' est introuvable !");
  129. }
  130. var targetDir = __dirname + '/exemples/' + slug;
  131. if(req.body.html) {
  132. fs.writeFileSync(targetDir + '/contenu.html', req.body.html);
  133. }
  134. if(req.body.javascript) {
  135. fs.writeFileSync(targetDir + '/script.js', req.body.javascript);
  136. }
  137. var theDate = new Date();
  138. console.log(theDate.getHours() + ':' + theDate.getMinutes() + " - Sauvegarde de l'exemple '" + existing.title + " effectuée'");
  139. res.json({ success: true });
  140. });
  141. module.exports = app;