create-docs-images.js 951 B

12345678910111213141516171819202122232425262728293031
  1. /* eslint-disable no-console */
  2. const nomnoml = require('nomnoml');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const basePath = path.resolve(__dirname, '..');
  6. const docImageDir = path.join(basePath, 'docs/images');
  7. const nomnomlSourceDir = path.join(basePath, 'docs/images/sources');
  8. const buildImages = {
  9. build() {
  10. const files = fs.readdirSync(nomnomlSourceDir);
  11. while (files.length > 0) {
  12. const file = path.resolve(nomnomlSourceDir, files.shift());
  13. const basename = path.basename(file, 'txt');
  14. if (/.nomnoml/.test(basename)) {
  15. const fileContents = fs.readFileSync(file, 'utf-8');
  16. const generated = nomnoml.renderSvg(fileContents);
  17. const newFilePath = path.join(docImageDir, basename) + 'svg';
  18. const outFile = fs.createWriteStream(newFilePath);
  19. console.log(`wrote file ${newFilePath}`);
  20. outFile.write(generated);
  21. }
  22. }
  23. }
  24. };
  25. buildImages.build();