| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /* global __dirname */
- "use strict";
- var _ = require('lodash');
- var Mustache = require('mustache');
- var path = require('path');
- var fs = require('fs');
- var chokidar = require('chokidar');
- var indexTmplPath = path.normalize(__dirname + '/../html/index.mustache.html');
- var indexTpml;
- var {
- readFilesAsync
- } = require('../lib/fsio');
- var translator = require('../lib/translator');
- // One-liner for current directory, ignores .dotfiles
- chokidar.watch('./html', {ignored: /(^|[\/\\])\../}).on('all', (event, path) => {
- // console.log(event, path);
- if(path === 'html/index.mustache.html') {
- console.log('reload index html');
- indexTpml = fs.readFileSync(indexTmplPath).toString();
- }
- });
- module.exports = function(exStore, exDir) {
- function readExampleFiles(repoSlug, exampleSlug, config) {
- const exampleDir = exDir + '/' + 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);
- }
- function renderIndex(req, withRepo, withExample) {
- // Extract repoSlug and exampleSlug from req.params
- const { locale, params: { repoSlug, exampleSlug } } = req;
- let repo;
- let example;
- let menuExample;
- let statusCode;
- // Initialize view data
- let data = {
- menuRepo: exStore.getRepoMenu()
- }
- // Fetch example repository if needed
- if(withRepo) {
- repo = exStore.getRepo(repoSlug);
- if(! repo) {
- // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
- data.errorMessage = translator.get(locale, "repoNotFound", [repoSlug]); //'Repo ' + params.repoSlug + ' not found';
- statusCode = 404;
- }
- else {
- data.menuExample = exStore.getExampleMenu(repo.path);
- data.showControls = true;
- }
- }
- // Fetch example if needed
- if(withExample && repo) {
- example = _.find(repo.examples, { slug: exampleSlug });
- if(! example) {
- // return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
- data.errorMessage = translator.get(locale, "exampleNotFound", [repoSlug, exampleSlug]);
- statusCode = 404;
- }
- else {
- data.showEditor = true;
- }
- }
- // Mustache.render(indexTpml, data);
- return (
- exampleSlug && example ?
- readExampleFiles(repoSlug, exampleSlug, example) : Promise.resolve([])
- ).then(files =>
- ({ files, filesJSON: JSON.stringify(files) })
- )
- .then(({ files, filesJSON }) => Object.assign(data, { files, filesJSON }))
- .then(c => { console.log(c); return c; })
- .then(data => ({
- html: Mustache.render(indexTpml, data),
- code: statusCode ? statusCode : 200
- }));
- }
- return {
- /**
- * Extract language header from req
- */
- getAcceptLanguage: function (req, res, next) {
- const acceptLanguageHdr = req.headers["accept-language"];
- const re = /[a-z]{2}\-[A-Z]{2}/;
- const matches = re.exec(acceptLanguageHdr);
- if(matches) {
- req.locale = matches[0];
- }
- next();
- },
- /**
- * Get bare index without repo or examples
- */
- getIndexBare: function(req, res) {
- renderIndex(req)
- .then(({ html, code }) => res.send(html));
- },
- /**
- * Get index with repo selected only
- */
- getIndexRepo: function getIndexRepo(req, res) {
- renderIndex(req, true)
- .then(({ html, code }) => res.status(code).send(html));
- // 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) {
- renderIndex(req, true, true)
- .then(({ html, code }) => res.status(code).send(html));
- // console.log('getIndexExample', req.params);
- // 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');
- // }
- // // console.log('#1');
- // // const title = 'Home';
- // // console.log(menuExample);
- // const { repoSlug, exampleSlug } = req.params;
- // readExampleFiles(repoSlug, exampleSlug, example)
- // .then(files => {
- // console.log('example files', files);
- // res.send(Mustache.render(indexTpml, {
- // // title,
- // menuRepo,
- // menuExample,
- // files,
- // filesJSON: JSON.stringify(files)
- // }));
- // });
- }
- };
- };
|