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