|
@@ -2,82 +2,73 @@
|
|
|
|
|
|
const _fs = require('fs-magic');
|
|
const _fs = require('fs-magic');
|
|
const _path = require('path');
|
|
const _path = require('path');
|
|
-const _assetsPath = _path.join(__dirname, '../assets');
|
|
|
|
-const _langPath = _path.join(__dirname, '../i18n');
|
|
|
|
const _cli = require('commander');
|
|
const _cli = require('commander');
|
|
const _pkg = require('../package.json');
|
|
const _pkg = require('../package.json');
|
|
-const _pageRenderer = require('../lib/page-renderer');
|
|
|
|
|
|
+const _render = require('../lib/page-renderer');
|
|
const _jsonReader = require('../lib/json-reader');
|
|
const _jsonReader = require('../lib/json-reader');
|
|
|
|
+const _getResources = require('../lib/resources');
|
|
|
|
|
|
-// global paths
|
|
|
|
-let templatePath = null;
|
|
|
|
-let cssPath = null;
|
|
|
|
|
|
+// paths
|
|
|
|
+const _assetsPath = _path.join(__dirname, '../assets');
|
|
|
|
+const _langPath = _path.join(__dirname, '../i18n');
|
|
|
|
|
|
-async function generator(configFilename, pageDefinitionFile, distPath){
|
|
|
|
|
|
+// default files
|
|
|
|
+const _defaults = {
|
|
|
|
+ distDir: _path.join(__dirname, '../dist'),
|
|
|
|
+ configFile: _path.join(__dirname, '../config.json'),
|
|
|
|
+ templateFile: _path.join(_assetsPath, 'template.ejs'),
|
|
|
|
+ cssFile: _path.join(_assetsPath, 'layout.css')
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+// generate static pages
|
|
|
|
+async function generator(configFilename, distPath, opt){
|
|
// load config
|
|
// load config
|
|
const config = await _jsonReader(configFilename);
|
|
const config = await _jsonReader(configFilename);
|
|
|
|
|
|
// load page definitions
|
|
// load page definitions
|
|
- const pages = await _jsonReader(pageDefinitionFile);
|
|
|
|
-
|
|
|
|
- // load template
|
|
|
|
- const tpl = await _fs.readFile(templatePath, 'utf8');
|
|
|
|
-
|
|
|
|
- // load css
|
|
|
|
- const css = await _fs.readFile(cssPath, 'utf8');
|
|
|
|
-
|
|
|
|
- const lang = pageDefinitionFile.substr(-10, 2);
|
|
|
|
|
|
+ const pageDefinitions = await _jsonReader(opt.pages);
|
|
|
|
|
|
- console.log('Create/Empty distribution folder: "'+distPath+'"\n');
|
|
|
|
-
|
|
|
|
- if(await _fs.exists(distPath)) {
|
|
|
|
- await _fs.rmrf(distPath);
|
|
|
|
- }
|
|
|
|
- await _fs.mkdirp(distPath);
|
|
|
|
|
|
+ // load resources
|
|
|
|
+ const resources = await _getResources({
|
|
|
|
+ template: opt.template,
|
|
|
|
+ stylesheet: opt.stylesheet,
|
|
|
|
+ lang: null
|
|
|
|
+ });
|
|
|
|
|
|
console.log('Generating static pages');
|
|
console.log('Generating static pages');
|
|
|
|
|
|
// for each errorcode generate custom page
|
|
// for each errorcode generate custom page
|
|
- await Promise.all(Object.keys(pages).map(async function(code){
|
|
|
|
- // page config
|
|
|
|
- const pconf = pages[code];
|
|
|
|
-
|
|
|
|
- // inject errorcode
|
|
|
|
- pconf.code = code;
|
|
|
|
-
|
|
|
|
- // inject lang
|
|
|
|
- pconf.lang = lang || 'en';
|
|
|
|
-
|
|
|
|
- // inject page title
|
|
|
|
- if(config && config.page_title) {
|
|
|
|
- pconf.page_title = config.page_title.replace('%code%', code);
|
|
|
|
- pconf.page_title = pconf.page_title.replace('%title%', pconf.title);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // inject error
|
|
|
|
- pconf.error = 'Error '+ pconf.code
|
|
|
|
- if(config && config.error) {
|
|
|
|
- pconf.error = config.error.replace('%code%', code);
|
|
|
|
|
|
+ await Promise.all(Object.keys(pageDefinitions).map(async function(code){
|
|
|
|
+ // get page config. title+message available
|
|
|
|
+ const pageData = pageDefinitions[code];
|
|
|
|
+
|
|
|
|
+ // merge variables for ejs template usage
|
|
|
|
+ const templateVars = Object.assign({}, pageData, {
|
|
|
|
+ code: code,
|
|
|
|
+ language: opt.lang.substr(0, 2)
|
|
|
|
+ }, config);
|
|
|
|
+
|
|
|
|
+ // apply filter for template variable usage within the config
|
|
|
|
+ const varNames = Object.keys(templateVars);
|
|
|
|
+ const placeholderData = Object.assign({}, templateVars);
|
|
|
|
+ for (const key in templateVars){
|
|
|
|
+ templateVars[key] = templateVars[key] && templateVars[key].replace(/%([\w]+)%/g, function(match, name){
|
|
|
|
+ // name exists ?
|
|
|
|
+ if (varNames.includes(name)){
|
|
|
|
+ return placeholderData[name];
|
|
|
|
+ }else{
|
|
|
|
+ throw new Error(`unknown placeholder "${name}" used in template variable [${key}]`);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
- // inject footer
|
|
|
|
- pconf.footer = pconf.footer || config.footer;
|
|
|
|
-
|
|
|
|
// render page
|
|
// render page
|
|
- const content = await _pageRenderer(tpl, css, pconf);
|
|
|
|
-
|
|
|
|
- // generate filename
|
|
|
|
- let filename = 'HTTP' + code + '.html';
|
|
|
|
|
|
+ const content = await _render(resources.template, resources.stylesheet, templateVars);
|
|
|
|
|
|
- // check filename schema
|
|
|
|
- if(config && config.scheme && config.scheme.indexOf("%code%") !== -1) {
|
|
|
|
- filename = config.scheme.replace('%code%', code);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// write content to file
|
|
// write content to file
|
|
- await _fs.writeFile(_path.join(distPath, filename), content, 'utf8');
|
|
|
|
|
|
+ await _fs.writeFile(_path.join(distPath, templateVars.scheme), content, 'utf8');
|
|
|
|
|
|
- console.log(' |- Page <' + filename + '>');
|
|
|
|
|
|
+ console.log(` |- Page <${templateVars.scheme}>`);
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
|
|
|
|
@@ -89,46 +80,35 @@ _cli
|
|
// static error page generator
|
|
// static error page generator
|
|
.command('static [config]')
|
|
.command('static [config]')
|
|
.description('run http-error-pages generator')
|
|
.description('run http-error-pages generator')
|
|
- .option('-t, --template <path>', 'path to your custom EJS template file', null)
|
|
|
|
- .option('-s, --styles <path>', 'path to your custom stylesheet (precompiled as CSS!)', null)
|
|
|
|
|
|
+ .option('-t, --template <path>', 'path to your custom EJS template file', _defaults.templateFile)
|
|
|
|
+ .option('-s, --styles <path>', 'path to your custom stylesheet (precompiled as CSS!)', _defaults.cssFile)
|
|
.option('-p, --pages <path>', 'path to your custom page definition', null)
|
|
.option('-p, --pages <path>', 'path to your custom page definition', null)
|
|
- .option('-l, --lang <lang>', 'the language of the default page definition', null)
|
|
|
|
- .option('-o, --out <path>', 'output directory', null)
|
|
|
|
|
|
+ .option('-l, --lang <lang>', 'the language of the default page definition', 'en_US')
|
|
|
|
+ .option('-o, --out <path>', 'output directory', _defaults.distDir)
|
|
|
|
+
|
|
.action(function(configFilename, options){
|
|
.action(function(configFilename, options){
|
|
// config set ?
|
|
// config set ?
|
|
- const configPath = configFilename || _path.join(__dirname, '../config.json');
|
|
|
|
-
|
|
|
|
- // template path set ?
|
|
|
|
- templatePath = options.template || _path.join(_assetsPath, 'template.ejs');
|
|
|
|
-
|
|
|
|
- // style path set ?
|
|
|
|
- cssPath = options.styles || _path.join(_assetsPath, 'layout.css');
|
|
|
|
-
|
|
|
|
- // output path set ?
|
|
|
|
- const distPath = options.out || _path.join(__dirname, '../dist');
|
|
|
|
-
|
|
|
|
- // language set ? use en_US as default
|
|
|
|
- const lang = options.lang || 'en_US';
|
|
|
|
|
|
+ const configPath = configFilename || _defaults.configFile;
|
|
|
|
|
|
// custom page definition available ?
|
|
// custom page definition available ?
|
|
- let pageDefinitionFile = options.pages || null;
|
|
|
|
-
|
|
|
|
- // page definition not set ? use lang
|
|
|
|
- if (pageDefinitionFile === null){
|
|
|
|
- pageDefinitionFile = _path.join(_langPath, 'pages-'+ lang + '.json')
|
|
|
|
- }
|
|
|
|
|
|
+ const pageDefinitionFile = options.pages || _path.join(_langPath, 'pages-'+ options.lang + '.json');
|
|
|
|
|
|
// show paths
|
|
// show paths
|
|
console.log('');
|
|
console.log('');
|
|
console.log('Paths');
|
|
console.log('Paths');
|
|
console.log(' |- Config:', configPath);
|
|
console.log(' |- Config:', configPath);
|
|
- console.log(' |- Template:', templatePath);
|
|
|
|
- console.log(' |- Styles:', cssPath);
|
|
|
|
|
|
+ console.log(' |- Template:', options.template);
|
|
|
|
+ console.log(' |- Styles:', options.styles);
|
|
console.log(' |- Pages:', pageDefinitionFile);
|
|
console.log(' |- Pages:', pageDefinitionFile);
|
|
console.log('');
|
|
console.log('');
|
|
|
|
|
|
// run async generator
|
|
// run async generator
|
|
- generator(configPath, pageDefinitionFile, distPath)
|
|
|
|
|
|
+ generator(configPath, options.out, {
|
|
|
|
+ template: options.template,
|
|
|
|
+ stylesheet: options.styles,
|
|
|
|
+ lang: options.lang,
|
|
|
|
+ pages: pageDefinitionFile
|
|
|
|
+ })
|
|
.then(function(){
|
|
.then(function(){
|
|
console.log('Static files generated\n');
|
|
console.log('Static files generated\n');
|
|
})
|
|
})
|