forked from HttpErrorPages/HttpErrorPages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
124 lines (97 loc) · 3.75 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
#!/usr/bin/env node
const _fs = require('fs-magic');
const _path = require('path');
const _assetsPath = _path.join(__dirname, '../assets');
const _langPath = _path.join(__dirname, '../i18n');
const _cli = require('commander');
const _pkg = require('../package.json');
const _pageRenderer = require('../lib/page-renderer');
const _jsonReader = require('../lib/json-reader');
// global paths
let templatePath = null;
let cssPath = null;
async function generator(configFilename, pageDefinitionFile, distPath){
// load config
const config = await _jsonReader(configFilename);
// 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');
console.log('Generating static pages');
// for each errorcode generate custom page
await Promise.all(Object.keys(pages).map(async function(p){
// page config
const pconf = pages[p];
// inject errorcode
pconf.code = p;
// inject foote
pconf.footer = pconf.footer || config.footer;
// render page
const content = await _pageRenderer(tpl, css, pconf);
// generate filename
const filename = 'HTTP' + p + '.html';
// write content to file
await _fs.writeFile(_path.join(distPath, filename), content, 'utf8');
console.log(' |- Page <' + filename + '>');
}));
}
// 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', null)
.option('-s, --styles <path>', 'path to your custom stylesheet (precompiled as CSS!)', 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)
.action(function(configFilename, options){
// 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';
// 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')
}
// show paths
console.log('');
console.log('Paths');
console.log(' |- Config:', configPath);
console.log(' |- Template:', templatePath);
console.log(' |- Styles:', cssPath);
console.log(' |- Pages:', pageDefinitionFile);
console.log('');
// run async generator
generator(configPath, pageDefinitionFile, distPath)
.then(function(){
console.log('Static files generated\n');
})
.catch(function(e){
console.error(e);
});
});
_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();
}