generator.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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(code){
  31. // page config
  32. const pconf = pages[code];
  33. // inject errorcode
  34. pconf.code = code;
  35. // inject lang
  36. pconf.lang = lang || 'en';
  37. // inject page title
  38. if(config && config.page_title) {
  39. pconf.page_title = config.page_title.replace('%code%', code);
  40. pconf.page_title = pconf.page_title.replace('%title%', pconf.title);
  41. }
  42. // inject error
  43. pconf.error = 'Error '+ pconf.code
  44. if(config && config.error) {
  45. pconf.error = config.error.replace('%code%', code);
  46. }
  47. // inject footer
  48. pconf.footer = pconf.footer || config.footer;
  49. // render page
  50. const content = await _pageRenderer(tpl, css, pconf);
  51. // generate filename
  52. let filename = 'HTTP' + code + '.html';
  53. // check filename schema
  54. if(config && config.scheme && config.scheme.indexOf("%code%") !== -1) {
  55. filename = config.scheme.replace('%code%', code);
  56. }
  57. // write content to file
  58. await _fs.writeFile(_path.join(distPath, filename), content, 'utf8');
  59. console.log(' |- Page <' + filename + '>');
  60. }));
  61. }
  62. // CLI setup
  63. _cli
  64. // read file version package.json
  65. .version(_pkg.version)
  66. // static error page generator
  67. .command('static [config]')
  68. .description('run http-error-pages generator')
  69. .option('-t, --template <path>', 'path to your custom EJS template file', null)
  70. .option('-s, --styles <path>', 'path to your custom stylesheet (precompiled as CSS!)', null)
  71. .option('-p, --pages <path>', 'path to your custom page definition', null)
  72. .option('-l, --lang <lang>', 'the language of the default page definition', null)
  73. .option('-o, --out <path>', 'output directory', null)
  74. .action(function(configFilename, options){
  75. // config set ?
  76. const configPath = configFilename || _path.join(__dirname, '../config.json');
  77. // template path set ?
  78. templatePath = options.template || _path.join(_assetsPath, 'template.ejs');
  79. // style path set ?
  80. cssPath = options.styles || _path.join(_assetsPath, 'layout.css');
  81. // output path set ?
  82. const distPath = options.out || _path.join(__dirname, '../dist');
  83. // language set ? use en_US as default
  84. const lang = options.lang || 'en_US';
  85. // custom page definition available ?
  86. let pageDefinitionFile = options.pages || null;
  87. // page definition not set ? use lang
  88. if (pageDefinitionFile === null){
  89. pageDefinitionFile = _path.join(_langPath, 'pages-'+ lang + '.json')
  90. }
  91. // show paths
  92. console.log('');
  93. console.log('Paths');
  94. console.log(' |- Config:', configPath);
  95. console.log(' |- Template:', templatePath);
  96. console.log(' |- Styles:', cssPath);
  97. console.log(' |- Pages:', pageDefinitionFile);
  98. console.log('');
  99. // run async generator
  100. generator(configPath, pageDefinitionFile, distPath)
  101. .then(function(){
  102. console.log('Static files generated\n');
  103. })
  104. .catch(function(e){
  105. console.error(e);
  106. console.log('\nStatic files not generated\n');
  107. });
  108. });
  109. _cli
  110. .command('*')
  111. .action(function(c){
  112. console.error('Unknown command "' + c + '"');
  113. _cli.outputHelp();
  114. });
  115. // run the commander dispatcher
  116. _cli.parse(process.argv);
  117. // default action (no command provided)
  118. if (!process.argv.slice(2).length) {
  119. _cli.outputHelp();
  120. }