|
|
@@ -10,85 +10,19 @@ var fs = require('fs');
|
|
|
var Promise = require('bluebird');
|
|
|
var Mustache = require('mustache');
|
|
|
var app = express();
|
|
|
-// var examplesJSON = __dirname + '/exemples/liste.json';
|
|
|
+var ExampleStore = require('./lib/ExampleStore');
|
|
|
+var indexTpml = fs.readFileSync(__dirname + '/html/index.mustache.html').toString();
|
|
|
var sandboxTpml = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
|
|
|
-// var examples = require(examplesJSON);
|
|
|
Promise.promisifyAll(fs);
|
|
|
+var exStore = new ExampleStore(__dirname + '/exemples');
|
|
|
+exStore.init()
|
|
|
+.then(() => console.log(exStore.getMenu()));
|
|
|
|
|
|
// Initialize Express app: root folder as static, body parsers
|
|
|
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,
|
|
|
- fullPath,
|
|
|
- examples: []
|
|
|
- };
|
|
|
- this.repos.push(repoDescriptor);
|
|
|
- return scandir(fullPath, ['.gitkeep', 'repo-config.json'])
|
|
|
- .then(examples => Promise.map(
|
|
|
- examples, example => loadExample(repoDescriptor, example)
|
|
|
- ))
|
|
|
- .then(() => console.log(this.repos[0]))
|
|
|
-};
|
|
|
-
|
|
|
-ExampleStore.prototype.loadExample = function(repo, slug) {
|
|
|
- const exampleConfig = require(repo.fullPath + '/' + slug + '/config.json');
|
|
|
- repo.examples.push(Object.assign({ slug }, exampleConfig));
|
|
|
- return exampleConfig;
|
|
|
-};
|
|
|
-
|
|
|
-ExampleStore.prototype.getList = function(path) {
|
|
|
- return _.find(this.repos, { path });
|
|
|
-}
|
|
|
-
|
|
|
-ExampleStore.prototype.getMenu = function(path) {
|
|
|
- const self = this;
|
|
|
- return '<ul>' + self.repos.reduce((menu, repo) => {
|
|
|
- return '<li>'+ repo.title + '<ul>' + repo.examples.reduce((submenu, example) =>
|
|
|
- (submenu + '<li><a href="#' + repo.path + '/' + example.slug + '">' + example.title + '</a></li>'),
|
|
|
- '') + '</ul></li>';
|
|
|
- }, '') + '</ul>';
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-var es = new ExampleStore(__dirname + '/exemples');
|
|
|
-es.init()
|
|
|
-.then(() => console.log(es.getMenu()));
|
|
|
-
|
|
|
|
|
|
function addExample(slug, title) {
|
|
|
examples.push({ slug: slug, title: title });
|
|
|
@@ -126,6 +60,35 @@ function readFileAsync(file) {
|
|
|
// ]);
|
|
|
// }
|
|
|
|
|
|
+app.get('/', function(req, res) {
|
|
|
+ const menuRepo = exStore.getRepoMenu();
|
|
|
+ // const title = 'Home';
|
|
|
+ console.log(menuRepo);
|
|
|
+ res.send(Mustache.render(indexTpml, {
|
|
|
+ title,
|
|
|
+ menuRepo
|
|
|
+ }));
|
|
|
+});
|
|
|
+
|
|
|
+app.get('/:repoSlug',
|
|
|
+ function(req, res, next) {
|
|
|
+ if(! exStore.repoExists(req.params.repoSlug)) {
|
|
|
+ return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
|
|
|
+ }
|
|
|
+ next();
|
|
|
+ },
|
|
|
+ function(req, res) {
|
|
|
+ const menuRepo = exStore.getRepoMenu();
|
|
|
+ const menuRepo = exStore.getRepoMenu();
|
|
|
+ // const title = 'Home';
|
|
|
+ console.log(menuRepo);
|
|
|
+ res.send(Mustache.render(indexTpml, {
|
|
|
+ title,
|
|
|
+ menuRepo,
|
|
|
+ menuRepo
|
|
|
+ }));
|
|
|
+});
|
|
|
+
|
|
|
app.post('/examples', function(req, res) {
|
|
|
var title = req.body.title;
|
|
|
if(! req.body.title) {
|
|
|
@@ -158,12 +121,12 @@ app.get('/examples/:slug', function(req, res) {
|
|
|
});
|
|
|
|
|
|
app.get('/menu', (rea, res) => {
|
|
|
- res.send(es.getMenu());
|
|
|
+ res.send(exStore.getMenu());
|
|
|
});
|
|
|
|
|
|
app.get('/list/:repoPath', function(req, res) {
|
|
|
const { repoPath } = req.params;
|
|
|
- const repo = es.getList(repoPath);
|
|
|
+ const repo = exStore.getList(repoPath);
|
|
|
if(! repo) {
|
|
|
return res.status(404).send('Repo ' + repoPath + ' not found');
|
|
|
}
|