generator.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env node
  2. const _fs = require('fs-magic');
  3. const _path = require('path');
  4. const _assetsPath = _path.join(__dirname, '../assets');
  5. const _langPath = _path.join(__dirname, '../i18n');
  6. const _cli = require('commander');
  7. const _pkg = require('../package.json');
  8. const _pageRenderer = require('../lib/page-renderer');
  9. const _jsonReader = require('../lib/json-reader');
  10. // global paths
  11. let templatePath = null;
  12. let cssPath = null;
  13. async function generator(configFilename, pageDefinitionFile, distPath){
  14. // load config
  15. const config = await _jsonReader(configFilename);
  16. // load page definitions
  17. const pages = await _jsonReader(pageDefinitionFile);
  18. // load template
  19. const tpl = await _fs.readFile(templatePath, 'utf8');
  20. // load css
  21. const css = await _fs.readFile(cssPath, 'utf8');
  22. const lang = pageDefinitionFile.substr(-10, 2);
  23. console.log('Create/Empty distribution folder: "'+distPath+'"\n');
  24. if(await _fs.exists(distPath)) {
  25. await _fs.rmrf(distPath);
  26. }
  27. await _fs.mkdirp(distPath);
  28. console.log('Generating static pages');
  29. // for each errorcode generate custom page
  30. await Promise.all(Object.keys(pages).map(async function(p){
  31. // page config
  32. const pconf = pages[p];
  33. // inject errorcode
  34. pconf.code = p;
  35. // inject footer
  36. pconf.footer = pconf.footer || config.footer;
  37. // inject footer
  38. pconf.lang = lang || 'en';
  39. // render page
  40. const content = await _pageRenderer(tpl, css, pconf);
  41. // generate filename
  42. let filename = 'HTTP' + p + '.html';
  43. // check filename schema
  44. if(config && config.scheme && config.scheme.indexOf("%d") !== -1) {
  45. filename = config.scheme.replace('%d', p);
  46. }
  47. // write content to file
  48. await _fs.writeFile(_path.join(distPath, filename), content, 'utf8');
  49. console.log(' |- Page <' + filename + '>');
  50. }));
  51. }
  52. // CLI setup
  53. _cli
  54. // read file version package.json
  55. .version(_pkg.version)
  56. // static error page generator
  57. .command('static [config]')
  58. .description('run http-error-pages generator')
  59. .option('-t, --template <path>', 'path to your custom EJS template file', null)
  60. .option('-s, --styles <path>', 'path to your custom stylesheet (precompiled as CSS!)', null)
  61. .option('-p, --pages <path>', 'path to your custom page definition', null)
  62. .option('-l, --lang <lang>', 'the language of the default page definition', null)
  63. .option('-o, --out <path>', 'output directory', null)
  64. .action(function(configFilename, options){
  65. // config set ?
  66. const configPath = configFilename || _path.join(__dirname, '../config.json');
  67. // template path set ?
  68. templatePath = options.template || _path.join(_assetsPath, 'template.ejs');
  69. // style path set ?
  70. cssPath = options.styles || _path.join(_assetsPath, 'layout.css');
  71. // output path set ?
  72. const distPath = options.out || _path.join(__dirname, '../dist');
  73. // language set ? use en_US as default
  74. const lang = options.lang || 'en_US';
  75. // custom page definition available ?
  76. let pageDefinitionFile = options.pages || null;
  77. // page definition not set ? use lang
  78. if (pageDefinitionFile === null){
  79. pageDefinitionFile = _path.join(_langPath, 'pages-'+ lang + '.json')
  80. }
  81. // show paths
  82. console.log('');
  83. console.log('Paths');
  84. console.log(' |- Config:', configPath);
  85. console.log(' |- Template:', templatePath);
  86. console.log(' |- Styles:', cssPath);
  87. console.log(' |- Pages:', pageDefinitionFile);
  88. console.log('');
  89. // run async generator
  90. generator(configPath, pageDefinitionFile, distPath)
  91. .then(function(){
  92. console.log('Static files generated\n');
  93. })
  94. .catch(function(e){
  95. console.error(e);
  96. });
  97. });
  98. _cli
  99. .command('*')
  100. .action(function(c){
  101. console.error('Unknown command "' + c + '"');
  102. _cli.outputHelp();
  103. });
  104. // run the commander dispatcher
  105. _cli.parse(process.argv);
  106. // default action (no command provided)
  107. if (!process.argv.slice(2).length) {
  108. _cli.outputHelp();
  109. }