forked from HttpErrorPages/HttpErrorPages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
134 lines (112 loc) · 4.47 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
const _fs = require('fs-magic');
const _path = require('path');
const _cli = require('commander');
const _pkg = require('../package.json');
const _render = require('../lib/page-renderer');
const _jsonReader = require('../lib/json-reader');
const _getResources = require('../lib/resources');
// paths
const _assetsPath = _path.join(__dirname, '../assets');
const _langPath = _path.join(__dirname, '../i18n');
// 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
const config = await _jsonReader(configFilename);
// load page definitions
const pageDefinitions = await _jsonReader(opt.pages);
// load resources
const resources = await _getResources({
template: opt.template,
stylesheet: opt.stylesheet,
lang: null
});
console.log('Generating static pages');
// for each errorcode generate custom page
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}]`);
}
});
}
// render page
const content = await _render(resources.template, resources.stylesheet, templateVars);
// write content to file
await _fs.writeFile(_path.join(distPath, templateVars.scheme), content, 'utf8');
console.log(` |- Page <${templateVars.scheme}>`);
}));
}
// CLI setup
_cli
// read file version package.json
.version(_pkg.version)
// static error page generator
.command('static [config]')
.description('run http-error-pages generator')
.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('-l, --lang <lang>', 'the language of the default page definition', 'en_US')
.option('-o, --out <path>', 'output directory', _defaults.distDir)
.action(function(configFilename, options){
// config set ?
const configPath = configFilename || _defaults.configFile;
// custom page definition available ?
const pageDefinitionFile = options.pages || _path.join(_langPath, 'pages-'+ options.lang + '.json');
// show paths
console.log('');
console.log('Paths');
console.log(' |- Config:', configPath);
console.log(' |- Template:', options.template);
console.log(' |- Styles:', options.styles);
console.log(' |- Pages:', pageDefinitionFile);
console.log('');
// run async generator
generator(configPath, options.out, {
template: options.template,
stylesheet: options.styles,
lang: options.lang,
pages: pageDefinitionFile
})
.then(function(){
console.log('Static files generated\n');
})
.catch(function(e){
console.error(e);
console.log('\nStatic files not generated\n');
});
});
_cli
.command('*')
.action(function(c){
console.error('Unknown command "' + c + '"');
_cli.outputHelp();
});
// run the commander dispatcher
_cli.parse(process.argv);
// default action (no command provided)
if (!process.argv.slice(2).length) {
_cli.outputHelp();
}