/* global __dirname */ "use strict"; var _ = require('lodash'); var Mustache = require('mustache'); var path = require('path'); var fs = require('fs'); var indexTmplPath = path.normalize(__dirname + '/../html/index.mustache.html'); var indexTpml = fs.readFileSync(indexTmplPath).toString(); var { readFilesAsync } = require('../lib/fsio'); function readExampleFiles(repoSlug, exampleSlug, config) { const exampleDir = path.normalize(__dirname + '/../exemples/' + repoSlug + '/' + exampleSlug); const libsCssDir = path.normalize(__dirname + '/../css/vendor'); const libsJsDir = path.normalize(__dirname + '/../js/vendor'); const { html, js, css } = config; // libsCss, libsJs const files = [].concat(html, js, css); return readFilesAsync(exampleDir, files); } module.exports = function(exStore) { return { /** * Get bare index without repo or examples */ getIndexBare: function(req, res) { const menuRepo = exStore.getRepoMenu(); // const title = 'Home'; console.log(menuRepo); res.send(Mustache.render(indexTpml, { // title, menuRepo, filesJSON: '[]' })); }, /** * Get index with repo selected only */ getIndexRepo: function getIndexRepo(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, filesJSON: '[]' })); }, /** * Get index with selected repo&example */ getIndexExample: 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) })); }); } }; };