sandboxApp.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 path = require('path');
  11. var Promise = require('bluebird');
  12. var Mustache = require('mustache');
  13. var app = express();
  14. var ExampleStore = require('./lib/ExampleStore');
  15. var indexTpml = fs.readFileSync(__dirname + '/html/index.mustache.html').toString();
  16. var sandboxTpml = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
  17. Promise.promisifyAll(fs);
  18. var exStore = new ExampleStore(__dirname + '/exemples');
  19. exStore.init()
  20. .then(() => console.log(exStore.getMenu()));
  21. // Initialize Express app: root folder as static, body parsers
  22. app.use(express.static(__dirname));
  23. app.use(bodyParser.json());
  24. app.use(bodyParser.urlencoded({ extended: true }));
  25. function addExample(slug, title) {
  26. examples.push({ slug: slug, title: title });
  27. return fs.writeFileAsync(examplesJSON, beautify(examples, null, 2, 100));
  28. }
  29. function readConfigJson(exampleSlug) {
  30. console.log(exampleSlug);
  31. return require('./exemples/jquery/' + exampleSlug + '/config.json');
  32. }
  33. function readFileAsync(file) {
  34. return fs.readFileAsync(file)
  35. .then(buf => (buf.toString()));
  36. }
  37. function readFilesAsync(fullPath, files) {
  38. // console.log('reading files', files, 'from path', path);
  39. return Promise.reduce(files,
  40. (carry, f) => readFileAsync(fullPath + '/' + f)
  41. .then(content =>
  42. (carry.concat([{
  43. // type: path.extname(f).substr(1),
  44. name: f,
  45. content
  46. }]))
  47. ),
  48. []
  49. );
  50. }
  51. function readExampleFiles(repoSlug, exampleSlug, config) {
  52. const exampleDir = __dirname + '/exemples/' + repoSlug + '/' + exampleSlug;
  53. const libsCssDir = __dirname + '/css/vendor';
  54. const libsJsDir = __dirname + '/js/vendor';
  55. const { html, js, css } = config; // libsCss, libsJs
  56. return Promise.all([
  57. readFilesAsync(exampleDir, html),
  58. readFilesAsync(exampleDir, js),
  59. readFilesAsync(exampleDir, css),
  60. // readFilesAsync(libsJsDir, libsJs),
  61. // readFilesAsync(libsCssDir, libsCss),
  62. ]);
  63. }
  64. /**
  65. * Index page: render with only repo list in menu
  66. */
  67. app.get('/', function(req, res) {
  68. const menuRepo = exStore.getRepoMenu();
  69. // const title = 'Home';
  70. console.log(menuRepo);
  71. res.send(Mustache.render(indexTpml, {
  72. title,
  73. menuRepo
  74. }));
  75. });
  76. /**
  77. * Repo page: render with repo list and selected repo's example list in menu
  78. */
  79. app.get('/:repoSlug',
  80. function(req, res) {
  81. const repo = exStore.getRepo(req.params.repoSlug);
  82. if(! repo) {
  83. return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  84. }
  85. const menuRepo = exStore.getRepoMenu();
  86. const menuExample = exStore.getExampleMenu(repo.path);
  87. // const title = 'Home';
  88. console.log(menuExample);
  89. res.send(Mustache.render(indexTpml, {
  90. // title,
  91. menuRepo,
  92. menuExample
  93. }));
  94. });
  95. /**
  96. * Example page: render with repo list and selected repo's example list in menu,
  97. * and the editor with the selected example
  98. */
  99. app.get('/:repoSlug/:exampleSlug',
  100. function(req, res) {
  101. const repo = exStore.getRepo(req.params.repoSlug);
  102. if(! repo) {
  103. return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  104. }
  105. const menuRepo = exStore.getRepoMenu();
  106. const menuExample = exStore.getExampleMenu(repo.path);
  107. const example = _.find(repo.examples, { slug: req.params.exampleSlug });
  108. if(! example) {
  109. return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
  110. }
  111. // const title = 'Home';
  112. console.log(menuExample);
  113. const { repoSlug, exampleSlug } = req.params;
  114. readExampleFiles(repoSlug, exampleSlug, example)
  115. .then(([ html, js, css ]) => {
  116. console.log('example files', html, js, css);
  117. res.send(Mustache.render(indexTpml, {
  118. // title,
  119. menuRepo,
  120. menuExample,
  121. html,
  122. js,
  123. css,
  124. mainHtml: html[0].content
  125. }));
  126. });
  127. });
  128. app.post('/examples', function(req, res) {
  129. var title = req.body.title;
  130. if(! req.body.title) {
  131. res.status(400).send('Le titre ne peut pas être vide !');
  132. }
  133. var existingTitle = _.find(examples, { title: title });
  134. if(existingTitle) {
  135. res.status(400).send("L'exemple '" + title + "' existe déjà !");
  136. }
  137. var exampleSlug = slug(req.body.title.toLowerCase());
  138. var targetDir = __dirname + '/exemples/' + exampleSlug;
  139. fs.mkdirAsync(targetDir)
  140. .then(() => Promise.map(
  141. ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
  142. ))
  143. .then(files => addExample(exampleSlug, title))
  144. .then(() => res.json({ slug: exampleSlug, title: title }));
  145. });
  146. app.get('/examples/:slug', function(req, res) {
  147. const { slug } = req.params;
  148. const config = readConfigJson(slug);
  149. const { title, html, js, css, libsCss, libsJs } = config;
  150. readFileAsync(__dirname + '/exemples/' + slug + '/example.html')
  151. .then(body =>
  152. Mustache.render(sandboxTpml, { body, slug, title, js, css, libsCss, libsJs })
  153. )
  154. .then(html => res.send(html));
  155. });
  156. app.get('/menu', (rea, res) => {
  157. res.send(exStore.getMenu());
  158. });
  159. app.get('/list/:repoPath', function(req, res) {
  160. const { repoPath } = req.params;
  161. const repo = exStore.getList(repoPath);
  162. if(! repo) {
  163. return res.status(404).send('Repo ' + repoPath + ' not found');
  164. }
  165. console.log('found repo', repo);
  166. const data = repo.examples.map(e => (
  167. { slug: e.slug, title: e.title }
  168. ));
  169. res.json(data);
  170. });
  171. app.put('/examples/:slug', function(req, res) {
  172. var slug = req.params.slug;
  173. var existing = _.find(examples, { slug: slug });
  174. if(! existing) {
  175. res.status(404).send("L'exemple avec l'identifiant '" + slug + "' est introuvable !");
  176. }
  177. var targetDir = __dirname + '/exemples/' + slug;
  178. if(req.body.html) {
  179. fs.writeFileSync(targetDir + '/contenu.html', req.body.html);
  180. }
  181. if(req.body.javascript) {
  182. fs.writeFileSync(targetDir + '/script.js', req.body.javascript);
  183. }
  184. var theDate = new Date();
  185. console.log(theDate.getHours() + ':' + theDate.getMinutes() + " - Sauvegarde de l'exemple '" + existing.title + " effectuée'");
  186. res.json({ success: true });
  187. });
  188. module.exports = app;