generator.js 4.4 KB

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