|
|
@@ -1,25 +1,40 @@
|
|
|
/* global __dirname */
|
|
|
/* jshint strict:false */
|
|
|
"use strict";
|
|
|
-var express = require('express');
|
|
|
-var bodyParser = require('body-parser');
|
|
|
-var slug = require('slug');
|
|
|
-var beautify = require("json-beautify");
|
|
|
-var _ = require('lodash');
|
|
|
-var fs = require('fs');
|
|
|
-var path = require('path');
|
|
|
-var Promise = require('bluebird');
|
|
|
-var Mustache = require('mustache');
|
|
|
-var app = express();
|
|
|
-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();
|
|
|
-Promise.promisifyAll(fs);
|
|
|
-var exStore = new ExampleStore(__dirname + '/exemples');
|
|
|
+var express = require('express');
|
|
|
+var bodyParser = require('body-parser');
|
|
|
+var slug = require('slug');
|
|
|
+var beautify = require("json-beautify");
|
|
|
+var _ = require('lodash');
|
|
|
+var path = require('path');
|
|
|
+var Mustache = require('mustache');
|
|
|
+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 ExampleStore = require('./lib/ExampleStore');
|
|
|
+var exStore = new ExampleStore(__dirname + '/exemples');
|
|
|
+var {
|
|
|
+ readFileAsync,
|
|
|
+ readFilesAsync
|
|
|
+} = require('./lib/fsio');
|
|
|
+var {
|
|
|
+ getIndexBare,
|
|
|
+ getIndexRepo,
|
|
|
+ getIndexExample
|
|
|
+} = require('./lib/indexHandlers')(exStore);
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Initialize example store
|
|
|
+ */
|
|
|
exStore.init()
|
|
|
.then(() => console.log(exStore.getMenu()));
|
|
|
|
|
|
-// Initialize Express app: root folder as static, body parsers
|
|
|
+
|
|
|
+/**
|
|
|
+ * Initialize Express app: root folder as static, body parsers
|
|
|
+ */
|
|
|
app.use(express.static(__dirname));
|
|
|
app.use(bodyParser.json());
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
@@ -35,100 +50,26 @@ function readConfigJson(exampleSlug) {
|
|
|
return require('./exemples/jquery/' + exampleSlug + '/config.json');
|
|
|
}
|
|
|
|
|
|
-function readFileAsync(file) {
|
|
|
- return fs.readFileAsync(file)
|
|
|
- .then(buf => (buf.toString()));
|
|
|
-}
|
|
|
|
|
|
-function readFilesAsync(fullPath, files) {
|
|
|
- // console.log('reading files', files, 'from path', path);
|
|
|
- return Promise.reduce(files,
|
|
|
- (carry, f) => readFileAsync(fullPath + '/' + f)
|
|
|
- .then(content =>
|
|
|
- (carry.concat([{
|
|
|
- type: path.extname(f).substr(1),
|
|
|
- name: f,
|
|
|
- content
|
|
|
- }]))
|
|
|
- ),
|
|
|
- []
|
|
|
- );
|
|
|
-}
|
|
|
|
|
|
-function readExampleFiles(repoSlug, exampleSlug, config) {
|
|
|
- const exampleDir = __dirname + '/exemples/' + repoSlug + '/' + exampleSlug;
|
|
|
- const libsCssDir = __dirname + '/css/vendor';
|
|
|
- const libsJsDir = __dirname + '/js/vendor';
|
|
|
- const { html, js, css } = config; // libsCss, libsJs
|
|
|
- const files = [].concat(html, js, css);
|
|
|
- return readFilesAsync(exampleDir, files);
|
|
|
-}
|
|
|
+
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Index page: render with only repo list in menu
|
|
|
*/
|
|
|
-app.get('/', function(req, res) {
|
|
|
- const menuRepo = exStore.getRepoMenu();
|
|
|
- // const title = 'Home';
|
|
|
- console.log(menuRepo);
|
|
|
- res.send(Mustache.render(indexTpml, {
|
|
|
- title,
|
|
|
- menuRepo
|
|
|
- }));
|
|
|
-});
|
|
|
+app.get('/', getIndexBare);
|
|
|
|
|
|
/**
|
|
|
* Repo page: render with repo list and selected repo's example list in menu
|
|
|
*/
|
|
|
-app.get('/:repoSlug',
|
|
|
- function(req, res) {
|
|
|
- const repo = exStore.getRepo(req.params.repoSlug);
|
|
|
- if(! repo) {
|
|
|
- return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
|
|
|
- }
|
|
|
- const menuRepo = exStore.getRepoMenu();
|
|
|
- const menuExample = exStore.getExampleMenu(repo.path);
|
|
|
- // const title = 'Home';
|
|
|
- console.log(menuExample);
|
|
|
- res.send(Mustache.render(indexTpml, {
|
|
|
- // title,
|
|
|
- menuRepo,
|
|
|
- menuExample
|
|
|
- }));
|
|
|
-});
|
|
|
+app.get('/:repoSlug', getIndexRepo);
|
|
|
|
|
|
/**
|
|
|
* Example page: render with repo list and selected repo's example list in menu,
|
|
|
* and the editor with the selected example
|
|
|
*/
|
|
|
-app.get('/:repoSlug/:exampleSlug',
|
|
|
- function(req, res) {
|
|
|
- const repo = exStore.getRepo(req.params.repoSlug);
|
|
|
- if(! repo) {
|
|
|
- return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
|
|
|
- }
|
|
|
- const menuRepo = exStore.getRepoMenu();
|
|
|
- const menuExample = exStore.getExampleMenu(repo.path);
|
|
|
- const example = _.find(repo.examples, { slug: req.params.exampleSlug });
|
|
|
- if(! example) {
|
|
|
- return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
|
|
|
- }
|
|
|
- // const title = 'Home';
|
|
|
- console.log(menuExample);
|
|
|
- const { repoSlug, exampleSlug } = req.params;
|
|
|
- readExampleFiles(repoSlug, exampleSlug, example)
|
|
|
- .then(files => {
|
|
|
- // console.log('example files', html, js, css);
|
|
|
- res.send(Mustache.render(indexTpml, {
|
|
|
- // title,
|
|
|
- menuRepo,
|
|
|
- menuExample,
|
|
|
- files,
|
|
|
- filesJSON: JSON.stringify(files)
|
|
|
- }));
|
|
|
- });
|
|
|
-});
|
|
|
+app.get('/:repoSlug/:exampleSlug', getIndexExample);
|
|
|
|
|
|
app.post('/examples', function(req, res) {
|
|
|
var title = req.body.title;
|