sandboxApp.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. files: JSON.stringify({ html, js, css })
  126. }));
  127. });
  128. });
  129. app.post('/examples', function(req, res) {
  130. var title = req.body.title;
  131. if(! req.body.title) {
  132. res.status(400).send('Le titre ne peut pas être vide !');
  133. }
  134. var existingTitle = _.find(examples, { title: title });
  135. if(existingTitle) {
  136. res.status(400).send("L'exemple '" + title + "' existe déjà !");
  137. }
  138. var exampleSlug = slug(req.body.title.toLowerCase());
  139. var targetDir = __dirname + '/exemples/' + exampleSlug;
  140. fs.mkdirAsync(targetDir)
  141. .then(() => Promise.map(
  142. ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
  143. ))
  144. .then(files => addExample(exampleSlug, title))
  145. .then(() => res.json({ slug: exampleSlug, title: title }));
  146. });
  147. app.get('/examples/:slug', function(req, res) {
  148. const { slug } = req.params;
  149. const config = readConfigJson(slug);
  150. const { title, html, js, css, libsCss, libsJs } = config;
  151. readFileAsync(__dirname + '/exemples/' + slug + '/example.html')
  152. .then(body =>
  153. Mustache.render(sandboxTpml, { body, slug, title, js, css, libsCss, libsJs })
  154. )
  155. .then(html => res.send(html));
  156. });
  157. app.get('/menu', (rea, res) => {
  158. res.send(exStore.getMenu());
  159. });
  160. app.get('/list/:repoPath', function(req, res) {
  161. const { repoPath } = req.params;
  162. const repo = exStore.getList(repoPath);
  163. if(! repo) {
  164. return res.status(404).send('Repo ' + repoPath + ' not found');
  165. }
  166. console.log('found repo', repo);
  167. const data = repo.examples.map(e => (
  168. { slug: e.slug, title: e.title }
  169. ));
  170. res.json(data);
  171. });
  172. app.put('/examples/:slug', function(req, res) {
  173. var slug = req.params.slug;
  174. var existing = _.find(examples, { slug: slug });
  175. if(! existing) {
  176. res.status(404).send("L'exemple avec l'identifiant '" + slug + "' est introuvable !");
  177. }
  178. var targetDir = __dirname + '/exemples/' + slug;
  179. if(req.body.html) {
  180. fs.writeFileSync(targetDir + '/contenu.html', req.body.html);
  181. }
  182. if(req.body.javascript) {
  183. fs.writeFileSync(targetDir + '/script.js', req.body.javascript);
  184. }
  185. var theDate = new Date();
  186. console.log(theDate.getHours() + ':' + theDate.getMinutes() + " - Sauvegarde de l'exemple '" + existing.title + " effectuée'");
  187. res.json({ success: true });
  188. });
  189. module.exports = app;