'use strict'; const DIR = __dirname + '/../'; const DIR_SERVER = './'; const DIR_COMMON = '../common/'; const fs = require('fs'); const path = require('path'); const flop = require('flop'); const ponse = require('ponse'); const rendy = require('rendy'); const format = require('format-io'); const squad = require('squad'); const apart = require('apart'); const config = require(DIR_SERVER + 'config'); const root = require(DIR_SERVER + 'root'); const prefixer = require(DIR_SERVER + 'prefixer'); const CloudFunc = require(DIR_COMMON + 'cloudfunc'); const Template = require(DIR_SERVER + 'template')(); const prefix = squad(prefixer, apart(config, 'prefix')); const isDev = process.env.NODE_ENV === 'development'; const getIndexPath = (isDev) => { const dist = isDev ? 'dist-dev' : 'dist'; return path.join(DIR, `${dist}/index.html`); }; const FS = CloudFunc.FS; module.exports = (req, res, next) => { check(req, res, next); route(req, res, next); }; module.exports._getIndexPath = getIndexPath; /** * additional processing of index file */ function indexProcessing(options) { const keysPanel = '
{ if (dir) dir.path = format.addSlashToEnd(name); if (!error) return buildIndex(dir, (error, data) => { p.name = getIndexPath(isDev); if (error) return ponse.sendError(error, p); ponse.send(data, p); }); if (error.code !== 'ENOTDIR') return ponse.sendError(error, p); fs.realpath(fullPath, (error, pathReal) => { if (!error) p.name = pathReal; else p.name = name; p.gzip = false; ponse.sendFile(p); }); }); } function buildIndex(json, callback) { fs.readFile(getIndexPath(), 'utf8', (error, template) => { if (error) return callback(error); const panel = CloudFunc.buildFromJSON({ data: json, prefix: prefix(), template: Template, }); const data = indexProcessing({ panel, data: template, }); callback(error, data); }); } function check(req, res, next) { if (!req) throw Error('req could not be empty!'); if (!res) throw Error('res could not be empty!'); if (typeof next !== 'function') throw Error('next should be function!'); }