sandboxApp.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 path = require('path');
  10. var Mustache = require('mustache');
  11. var app = express();
  12. var fs = require('fs');
  13. var Promise = require('bluebird'); Promise.promisifyAll(fs);
  14. var sandboxTpml = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
  15. var ExampleStore = require('./lib/ExampleStore');
  16. var examplesDir = __dirname + '/exemples';
  17. // var examplesDir = __dirname + '/test/integration/test-examples';
  18. var exStore = new ExampleStore(examplesDir);
  19. var {
  20. readFileAsync,
  21. readFilesAsync
  22. } = require('./lib/fsio');
  23. var {
  24. getIndexBare,
  25. getIndexRepo,
  26. getIndexExample
  27. } = require('./lib/indexHandlers')(exStore, examplesDir);
  28. /**
  29. * Initialize example store
  30. */
  31. exStore.init();
  32. // .then(() => console.log(exStore.getMenu()));
  33. /**
  34. * Initialize Express app: root folder as static, body parsers
  35. */
  36. app.use(express.static(__dirname));
  37. app.use(bodyParser.json());
  38. app.use(bodyParser.urlencoded({ extended: true }));
  39. function addExample(slug, title) {
  40. examples.push({ slug: slug, title: title });
  41. return fs.writeFileAsync(examplesJSON, beautify(examples, null, 2, 100));
  42. }
  43. function readConfigJson(exampleSlug) {
  44. console.log(exampleSlug);
  45. return require('./exemples/jquery/' + exampleSlug + '/config.json');
  46. }
  47. /**
  48. * Index page: render with only repo list in menu
  49. */
  50. app.get('/', getIndexBare);
  51. /**
  52. * Repo page: render with repo list and selected repo's example list in menu
  53. */
  54. app.get('/:repoSlug', getIndexRepo);
  55. /**
  56. * Example page: render with repo list and selected repo's example list in menu,
  57. * and the editor with the selected example
  58. */
  59. app.get('/:repoSlug/:exampleSlug', getIndexExample);
  60. app.post('/examples', function(req, res) {
  61. var title = req.body.title;
  62. if(! req.body.title) {
  63. res.status(400).send('Le titre ne peut pas être vide !');
  64. }
  65. var existingTitle = _.find(examples, { title: title });
  66. if(existingTitle) {
  67. res.status(400).send("L'exemple '" + title + "' existe déjà !");
  68. }
  69. var exampleSlug = slug(req.body.title.toLowerCase());
  70. var targetDir = __dirname + '/exemples/' + exampleSlug;
  71. fs.mkdirAsync(targetDir)
  72. .then(() => Promise.map(
  73. ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
  74. ))
  75. .then(files => addExample(exampleSlug, title))
  76. .then(() => res.json({ slug: exampleSlug, title: title }));
  77. });
  78. app.get('/examples/:slug', function(req, res) {
  79. const { slug } = req.params;
  80. const config = readConfigJson(slug);
  81. const { title, html, js, css, libsCss, libsJs } = config;
  82. readFileAsync(__dirname + '/exemples/' + slug + '/example.html')
  83. .then(body =>
  84. Mustache.render(sandboxTpml, { body, slug, title, js, css, libsCss, libsJs })
  85. )
  86. .then(html => res.send(html));
  87. });
  88. app.get('/menu', (rea, res) => {
  89. res.send(exStore.getMenu());
  90. });
  91. app.get('/list/:repoPath', function(req, res) {
  92. const { repoPath } = req.params;
  93. const repo = exStore.getList(repoPath);
  94. if(! repo) {
  95. return res.status(404).send('Repo ' + repoPath + ' not found');
  96. }
  97. console.log('found repo', repo);
  98. const data = repo.examples.map(e => (
  99. { slug: e.slug, title: e.title }
  100. ));
  101. res.json(data);
  102. });
  103. app.put('/examples/:slug', function(req, res) {
  104. var slug = req.params.slug;
  105. var existing = _.find(examples, { slug: slug });
  106. if(! existing) {
  107. res.status(404).send("L'exemple avec l'identifiant '" + slug + "' est introuvable !");
  108. }
  109. var targetDir = __dirname + '/exemples/' + slug;
  110. if(req.body.html) {
  111. fs.writeFileSync(targetDir + '/contenu.html', req.body.html);
  112. }
  113. if(req.body.javascript) {
  114. fs.writeFileSync(targetDir + '/script.js', req.body.javascript);
  115. }
  116. var theDate = new Date();
  117. console.log(theDate.getHours() + ':' + theDate.getMinutes() + " - Sauvegarde de l'exemple '" + existing.title + " effectuée'");
  118. res.json({ success: true });
  119. });
  120. module.exports = app;