sandboxApp.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. fullPath,
  53. examples: []
  54. };
  55. this.repos.push(repoDescriptor);
  56. return scandir(fullPath, ['.gitkeep', 'repo-config.json'])
  57. .then(examples => Promise.map(
  58. examples, example => loadExample(repoDescriptor, example)
  59. ))
  60. .then(() => console.log(this.repos[0]))
  61. };
  62. ExampleStore.prototype.loadExample = function(repo, slug) {
  63. const exampleConfig = require(repo.fullPath + '/' + slug + '/config.json');
  64. repo.examples.push(Object.assign({ slug }, exampleConfig));
  65. };
  66. var es = new ExampleStore(__dirname + '/exemples');
  67. es.init();
  68. function addExample(slug, title) {
  69. examples.push({ slug: slug, title: title });
  70. return fs.writeFileAsync(examplesJSON, beautify(examples, null, 2, 100));
  71. }
  72. function readConfigJson(exampleSlug) {
  73. console.log(exampleSlug);
  74. return require('./exemples/' + exampleSlug + '/config.json');
  75. }
  76. function readFileAsync(file) {
  77. return fs.readFileAsync(file)
  78. .then(buf => (buf.toString()));
  79. }
  80. // function readFilesAsync(path, files) {
  81. // // console.log('reading files', files, 'from path', path);
  82. // return Promise.map(files,
  83. // f => readFileAsync(path + '/' + f)
  84. // );
  85. // }
  86. // function readExampleFiles(slug, config) {
  87. // const exampleDir = __dirname + '/exemples/' + slug;
  88. // const libsCssDir = __dirname + '/css/vendor';
  89. // const libsJsDir = __dirname + '/js/vendor';
  90. // const { html, js, css, libsCss, libsJs } = config;
  91. // return Promise.all([
  92. // readFilesAsync(exampleDir, html),
  93. // readFilesAsync(exampleDir, js),
  94. // readFilesAsync(exampleDir, css),
  95. // readFilesAsync(libsJsDir, libsJs),
  96. // readFilesAsync(libsCssDir, libsCss),
  97. // ]);
  98. // }
  99. app.post('/examples', function(req, res) {
  100. var title = req.body.title;
  101. if(! req.body.title) {
  102. res.status(400).send('Le titre ne peut pas être vide !');
  103. }
  104. var existingTitle = _.find(examples, { title: title });
  105. if(existingTitle) {
  106. res.status(400).send("L'exemple '" + title + "' existe déjà !");
  107. }
  108. var exampleSlug = slug(req.body.title.toLowerCase());
  109. var targetDir = __dirname + '/exemples/' + exampleSlug;
  110. fs.mkdirAsync(targetDir)
  111. .then(() => Promise.map(
  112. ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
  113. ))
  114. .then(files => addExample(exampleSlug, title))
  115. .then(() => res.json({ slug: exampleSlug, title: title }));
  116. });
  117. app.get('/examples/:slug', function(req, res) {
  118. const { slug } = req.params;
  119. const config = readConfigJson(slug);
  120. const { title, html, js, css, libsCss, libsJs } = config;
  121. readFileAsync(__dirname + '/exemples/' + slug + '/example.html')
  122. .then(body =>
  123. Mustache.render(sandboxTpml, { body, slug, title, js, css, libsCss, libsJs })
  124. )
  125. .then(html => res.send(html));
  126. });
  127. app.put('/examples/:slug', function(req, res) {
  128. var slug = req.params.slug;
  129. var existing = _.find(examples, { slug: slug });
  130. if(! existing) {
  131. res.status(404).send("L'exemple avec l'identifiant '" + slug + "' est introuvable !");
  132. }
  133. var targetDir = __dirname + '/exemples/' + slug;
  134. if(req.body.html) {
  135. fs.writeFileSync(targetDir + '/contenu.html', req.body.html);
  136. }
  137. if(req.body.javascript) {
  138. fs.writeFileSync(targetDir + '/script.js', req.body.javascript);
  139. }
  140. var theDate = new Date();
  141. console.log(theDate.getHours() + ':' + theDate.getMinutes() + " - Sauvegarde de l'exemple '" + existing.title + " effectuée'");
  142. res.json({ success: true });
  143. });
  144. module.exports = app;