Benoît Hubert пре 8 година
родитељ
комит
a186384de5
2 измењених фајлова са 45 додато и 5 уклоњено
  1. 7 0
      lib/exampleTmpl.json
  2. 38 5
      sandboxApp.js

+ 7 - 0
lib/exampleTmpl.json

@@ -0,0 +1,7 @@
+{
+  "html": [ "example.html" ],
+  "js": [ "script.js" ],
+  "css": [],
+  "libsJs": [ "jquery-3.2.1.min.js" ],
+  "libsCss": [ "styles.css" ]
+}

+ 38 - 5
sandboxApp.js

@@ -12,6 +12,7 @@ var app           = express();
 var fs            = require('fs');
 var Promise       = require('bluebird'); Promise.promisifyAll(fs);
 var sandboxTpml   = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
+var exampleTmpl  = require('./lib/exampleTmpl.json');
 var ExampleStore  = require('./lib/ExampleStore');
 var examplesDir   = __dirname + '/exemples';
 // var examplesDir   = __dirname + '/test/integration/test-examples';
@@ -56,6 +57,16 @@ function readConfigJson(exampleSlug) {
   return require('./exemples/jquery/' + exampleSlug + '/config.json');
 }
 
+function mapObjToArray(obj, key, value) {
+  var arr = [];
+  for(var p in obj) {
+    arr.push({
+      [key]: p,
+      [value]: obj[p]
+    });
+  }
+  return arr;
+}
 
 /**
  * Index page: render with only repo list in menu
@@ -73,26 +84,48 @@ app.get('/:repoSlug', getIndexRepo);
  */
 app.get('/:repoSlug/:exampleSlug', getIndexExample);
 
+/**
+ * Create a new example for specified repo
+ */
 app.post('/:repoSlug/examples', function(req, res) {
-  var title = req.body.title;
-  var { repoSlug } = req.params;
-  if(! req.body.title) {
+
+  // Check for title and extract params
+  if(! req.body || ! req.body.title) {
     res.status(400).send('Le titre ne peut pas être vide !');
   }
+  const { title }    = req.body;
+  const { repoSlug } = req.params;
+
+  // Get repo from store
   var repo = exStore.getRepo(repoSlug);
   if(! repo) {
     res.status(404).send("Repo " + repoSlug + "not found");
   }
+
+  // Prevent duplicate title
   var existingTitle = _.find(repo.examples, { title: title });
   if(existingTitle) {
     res.status(400).send("L'exemple '" + title + "' existe déjà !");
   }
   var exampleSlug = slug(req.body.title.toLowerCase());
-  var targetDir = __dirname + '/exemples/' + repoSlug + '/' + exampleSlug;
 
+  // Prepare config
+  var config = Object.assign({
+    slug: exampleSlug,
+    title,
+    category: repo.defaultCategory
+  }, exampleTmpl);
+
+  // Prepare files to write
+  var targetDir = __dirname + '/exemples/' + repoSlug + '/' + exampleSlug;
+  var files = mapObjToArray({
+    'contenu.html': '',
+    'script.js': '',
+    'config.json': beautify(config, null, 2, 100)
+  }, 'file', 'content');
   fs.mkdirAsync(targetDir)
   .then(() => Promise.map(
-    ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
+    files, ({ file, content }) => fs.writeFileAsync(targetDir + '/' + file, content)
   ))
   .then(files => repo.examples.push({ slug: slug, title: title }))
   .then(() => res.json({ slug: exampleSlug, title: title }));