generator.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env node
  2. const _fs = require('fs-magic');
  3. const _path = require('path');
  4. const _cli = require('commander');
  5. const _pkg = require('../package.json');
  6. const _render = require('../lib/page-renderer');
  7. const _jsonReader = require('../lib/json-reader');
  8. const _getResources = require('../lib/resources');
  9. // paths
  10. const _assetsPath = _path.join(__dirname, '../assets');
  11. const _langPath = _path.join(__dirname, '../i18n');
  12. // default files
  13. const _defaults = {
  14. distDir: _path.join(__dirname, '../dist'),
  15. configFile: _path.join(__dirname, '../config.json'),
  16. templateFile: _path.join(_assetsPath, 'template.ejs'),
  17. cssFile: _path.join(_assetsPath, 'layout.css')
  18. };
  19. // generate static pages
  20. async function generator(configFilename, distPath, opt){
  21. // load config
  22. const config = await _jsonReader(configFilename);
  23. // load page definitions
  24. const pageDefinitions = await _jsonReader(opt.pages);
  25. // load resources
  26. const resources = await _getResources({
  27. template: opt.template,
  28. stylesheet: opt.stylesheet,
  29. lang: null
  30. });
  31. console.log('Generating static pages');
  32. // for each errorcode generate custom page
  33. await Promise.all(Object.keys(pageDefinitions).map(async function(code){
  34. // get page config. title+message available
  35. const pageData = pageDefinitions[code];
  36. // merge variables for ejs template usage
  37. const templateVars = Object.assign({}, pageData, {
  38. code: code,
  39. language: opt.lang.substr(0, 2)
  40. }, config);
  41. // apply filter for template variable usage within the config
  42. const varNames = Object.keys(templateVars);
  43. const placeholderData = Object.assign({}, templateVars);
  44. for (const key in templateVars){
  45. templateVars[key] = templateVars[key] && templateVars[key].replace(/%([\w]+)%/g, function(match, name){
  46. // name exists ?
  47. if (varNames.includes(name)){
  48. return placeholderData[name];
  49. }else{
  50. throw new Error(`unknown placeholder "${name}" used in template variable [${key}]`);
  51. }
  52. });
  53. }
  54. // render page
  55. const content = await _render(resources.template, resources.stylesheet, templateVars);
  56. // write content to file
  57. await _fs.writeFile(_path.join(distPath, templateVars.scheme), content, 'utf8');
  58. console.log(` |- Page <${templateVars.scheme}>`);
  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', _defaults.templateFile)
  69. .option('-s, --styles <path>', 'path to your custom stylesheet (precompiled as CSS!)', _defaults.cssFile)
  70. .option('-p, --pages <path>', 'path to your custom page definition', null)
  71. .option('-l, --lang <lang>', 'the language of the default page definition', 'en_US')
  72. .option('-o, --out <path>', 'output directory', _defaults.distDir)
  73. .action(function(configFilename, options){
  74. // config set ?
  75. const configPath = configFilename || _defaults.configFile;
  76. // custom page definition available ?
  77. const pageDefinitionFile = options.pages || _path.join(_langPath, 'pages-'+ options.lang + '.json');
  78. // show paths
  79. console.log('');
  80. console.log('Paths');
  81. console.log(' |- Config:', configPath);
  82. console.log(' |- Template:', options.template);
  83. console.log(' |- Styles:', options.styles);
  84. console.log(' |- Pages:', pageDefinitionFile);
  85. console.log('');
  86. // run async generator
  87. generator(configPath, options.out, {
  88. template: options.template,
  89. stylesheet: options.styles,
  90. lang: options.lang,
  91. pages: pageDefinitionFile
  92. })
  93. .then(function(){
  94. console.log('Static files generated\n');
  95. })
  96. .catch(function(e){
  97. console.error(e);
  98. console.log('\nStatic files not generated\n');
  99. });
  100. });
  101. _cli
  102. .command('*')
  103. .action(function(c){
  104. console.error('Unknown command "' + c + '"');
  105. _cli.outputHelp();
  106. });
  107. // run the commander dispatcher
  108. _cli.parse(process.argv);
  109. // default action (no command provided)
  110. if (!process.argv.slice(2).length) {
  111. _cli.outputHelp();
  112. }