sandboxApp.js 6.0 KB

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