generator.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. console.log('Generating static pages');
  23. // for each errorcode generate custom page
  24. await Promise.all(Object.keys(pages).map(async function(p){
  25. // page config
  26. const pconf = pages[p];
  27. // inject errorcode
  28. pconf.code = p;
  29. // inject foote
  30. pconf.footer = pconf.footer || config.footer;
  31. // render page
  32. const content = await _pageRenderer(tpl, css, pconf);
  33. // generate filename
  34. const filename = 'HTTP' + p + '.html';
  35. // write content to file
  36. await _fs.writeFile(_path.join(distPath, filename), content, 'utf8');
  37. console.log(' |- Page <' + filename + '>');
  38. }));
  39. }
  40. // CLI setup
  41. _cli
  42. // read file version package.json
  43. .version(_pkg.version)
  44. // static error page generator
  45. .command('static [config]')
  46. .description('run http-error-pages generator')
  47. .option('-t, --template <path>', 'path to your custom EJS template file', null)
  48. .option('-s, --styles <path>', 'path to your custom stylesheet (precompiled as CSS!)', null)
  49. .option('-p, --pages <path>', 'path to your custom page definition', null)
  50. .option('-l, --lang <lang>', 'the language of the default page definition', null)
  51. .option('-o, --out <path>', 'output directory', null)
  52. .action(function(configFilename, options){
  53. // config set ?
  54. const configPath = configFilename || _path.join(__dirname, '../config.json');
  55. // template path set ?
  56. templatePath = options.template || _path.join(_assetsPath, 'template.ejs');
  57. // style path set ?
  58. cssPath = options.styles || _path.join(_assetsPath, 'layout.css');
  59. // output path set ?
  60. const distPath = options.out || _path.join(__dirname, '../dist');
  61. // language set ? use en_US as default
  62. const lang = options.lang || 'en_US';
  63. // custom page definition available ?
  64. let pageDefinitionFile = options.pages || null;
  65. // page definition not set ? use lang
  66. if (pageDefinitionFile === null){
  67. pageDefinitionFile = _path.join(_langPath, 'pages-'+ lang + '.json')
  68. }
  69. // show paths
  70. console.log('');
  71. console.log('Paths');
  72. console.log(' |- Config:', configPath);
  73. console.log(' |- Template:', templatePath);
  74. console.log(' |- Styles:', cssPath);
  75. console.log(' |- Pages:', pageDefinitionFile);
  76. console.log('');
  77. // run async generator
  78. generator(configPath, pageDefinitionFile, distPath)
  79. .then(function(){
  80. console.log('Static files generated\n');
  81. })
  82. .catch(function(e){
  83. console.error(e);
  84. });
  85. });
  86. _cli
  87. .command('*')
  88. .action(function(c){
  89. console.error('Unknown command "' + c + '"');
  90. _cli.outputHelp();
  91. });
  92. // run the commander dispatcher
  93. _cli.parse(process.argv);
  94. // default action (no command provided)
  95. if (!process.argv.slice(2).length) {
  96. _cli.outputHelp();
  97. }