03-read-other-variables.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module.exports = plugin;
  2. const utils = require('./utils');
  3. const fs = require('fs');
  4. let initial_variables = JSON.parse(fs.readFileSync(utils.files.initial_variables));
  5. let derived_variables = JSON.parse(fs.readFileSync(utils.files.derived_variables));
  6. function plugin() {
  7. return (files, metalsmith, done) => {
  8. setImmediate(done);
  9. Object.keys(files).forEach(file_path => {
  10. // if (file_path.startsWith('utilities')) {
  11. // return;
  12. // }
  13. const {file_name, lines} = utils.getLines(files, file_path);
  14. let variables = {
  15. by_name: {},
  16. list: [],
  17. file_path,
  18. };
  19. lines.forEach(line => {
  20. const variable = utils.parseLine(line);
  21. if (variable != false) {
  22. const computed_data = utils.getComputedData(variable.name, variable.value, variable.type, initial_variables, derived_variables);
  23. if (Object.keys(computed_data).length > 0) {
  24. variable.computed_type = computed_data.computed_type;
  25. variable.computed_value = computed_data.computed_value;
  26. }
  27. variables.by_name[variable.name] = variable;
  28. variables.list.push(variable.name);
  29. }
  30. });
  31. if (variables.list.length > 0) {
  32. utils.writeFile(file_path, variables);
  33. }
  34. });
  35. };
  36. }