|
|
@@ -10,9 +10,9 @@ var fs = require('fs');
|
|
|
var Promise = require('bluebird');
|
|
|
var Mustache = require('mustache');
|
|
|
var app = express();
|
|
|
-var examplesJSON = __dirname + '/exemples/liste.json';
|
|
|
+// var examplesJSON = __dirname + '/exemples/liste.json';
|
|
|
var sandboxTpml = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
|
|
|
-var examples = require(examplesJSON);
|
|
|
+// var examples = require(examplesJSON);
|
|
|
Promise.promisifyAll(fs);
|
|
|
|
|
|
// Initialize Express app: root folder as static, body parsers
|
|
|
@@ -20,6 +20,58 @@ app.use(express.static(__dirname));
|
|
|
app.use(bodyParser.json());
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
|
|
+function scandir(path, excludes) {
|
|
|
+ excludes = excludes || [];
|
|
|
+ return fs.readdirAsync(path)
|
|
|
+ .then(dirContent => {
|
|
|
+ excludes.forEach(file => {
|
|
|
+ var idxInContent = dirContent.indexOf(file);
|
|
|
+ if(idxInContent !== -1) {
|
|
|
+ dirContent.splice(idxInContent, 1);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return dirContent;
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function ExampleStore(path) {
|
|
|
+ this.rootPath = path;
|
|
|
+ this.repos = [];
|
|
|
+}
|
|
|
+
|
|
|
+ExampleStore.prototype.init = function() {
|
|
|
+ const loadRepository = this.loadRepository.bind(this);
|
|
|
+ return scandir(this.rootPath, ['.gitkeep'])
|
|
|
+ .then(repositories => Promise.map(
|
|
|
+ repositories, loadRepository
|
|
|
+ ));
|
|
|
+};
|
|
|
+
|
|
|
+ExampleStore.prototype.loadRepository = function(repoPath) {
|
|
|
+ const loadExample = this.loadExample.bind(this);
|
|
|
+ const fullPath = this.rootPath + '/' + repoPath;
|
|
|
+ const repoConfig = require(fullPath + '/repo-config.json');
|
|
|
+ const repoDescriptor = {
|
|
|
+ title: repoConfig.title,
|
|
|
+ path: repoPath,
|
|
|
+ examples: []
|
|
|
+ };
|
|
|
+ this.repos.push(repoDescriptor);
|
|
|
+ return scandir(fullPath, ['.gitkeep', 'repo-config.json'])
|
|
|
+ .then(examples => Promise.map(
|
|
|
+ examples, example => loadExample(repoDescriptor, example)
|
|
|
+ ))
|
|
|
+};
|
|
|
+
|
|
|
+ExampleStore.prototype.loadExample = function(repo, example) {
|
|
|
+ var exampleConfig = require(repo.path + '/' + example + '/config.json');
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+var es = new ExampleStore(__dirname + '/exemples');
|
|
|
+es.init();
|
|
|
+
|
|
|
+
|
|
|
function addExample(slug, title) {
|
|
|
examples.push({ slug: slug, title: title });
|
|
|
return fs.writeFileAsync(examplesJSON, beautify(examples, null, 2, 100));
|