sandboxApp.js 4.0 KB

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