From 671101d201ab70b21aa26690b81a0ad62b68e294 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 27 Sep 2017 13:05:37 +0300 Subject: [PATCH] feature(template) add: read all templates on start --- server/route.js | 54 ++++------------------------------------------ server/template.js | 28 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 50 deletions(-) create mode 100644 server/template.js diff --git a/server/route.js b/server/route.js index 4c1bb0bd..d652faf7 100644 --- a/server/route.js +++ b/server/route.js @@ -1,16 +1,13 @@ 'use strict'; const DIR = __dirname + '/../'; -const DIR_TMPL = DIR + 'tmpl/'; const DIR_SERVER = './'; const DIR_COMMON = '../common/'; -const DIR_FS = DIR_TMPL + 'fs/'; const fs = require('fs'); const flop = require('flop'); const ponse = require('ponse'); -const files = require('files-io'); const rendy = require('rendy'); const format = require('format-io'); const squad = require('squad'); @@ -20,8 +17,10 @@ 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 prefix = squad(prefixer, apart(config, 'prefix')); +const Template = require(DIR_SERVER + 'template')(); + +const prefix = squad(prefixer, apart(config, 'prefix')); const isDev = process.env.NODE_ENV === 'development'; const getIndexPath = () => { @@ -29,23 +28,12 @@ const getIndexPath = () => { return DIR + `${dist}/index.html`; }; -const TMPL_PATH = [ - 'file', - 'panel', - 'path', - 'pathLink', - 'link' -]; - -const Template = {}; const FS = CloudFunc.FS; module.exports = (req, res, next) => { check(req, res, next); - readFiles(() => { - route(req, res, next); - }); + route(req, res, next); }; /** @@ -127,40 +115,6 @@ function indexProcessing(options) { return data; } -function readFiles(callback) { - const paths = {}; - const lengthTmpl = Object.keys(Template).length; - const lenthPath = TMPL_PATH.length; - const isRead = lengthTmpl === lenthPath; - - if (typeof callback !== 'function') - throw Error('callback should be a function!'); - - if (isRead) - return callback(); - - const filesList = TMPL_PATH.map((name) => { - const path = DIR_FS + name + '.hbs'; - - paths[path] = name; - - return path; - }); - - files.read(filesList, 'utf8', (error, files) => { - if (error) - throw error; - - Object.keys(files).forEach((path) => { - const name = paths[path]; - - Template[name] = files[path]; - }); - - callback(); - }); -} - /** * routing of server queries */ diff --git a/server/template.js b/server/template.js new file mode 100644 index 00000000..c0bc4226 --- /dev/null +++ b/server/template.js @@ -0,0 +1,28 @@ +'use strict'; + +const fs = require('fs'); +const join = require('path').join; + +const DIR = __dirname + '/../'; +const DIR_TMPL = DIR + 'tmpl/'; +const DIR_FS = DIR_TMPL + 'fs/'; + +const TMPL_PATH = [ + 'file', + 'panel', + 'path', + 'pathLink', + 'link', +]; + +module.exports = () => { + const templates = {}; + + TMPL_PATH.forEach((name) => { + const path = join(DIR_FS, `${name}.hbs`); + templates[name] = fs.readFileSync(path, 'utf8'); + }); + + return templates; +}; +