generateConfig.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * This script automatically generates OperationConfig.json, containing metadata
  3. * for each operation in the src/core/operations directory.
  4. * It also generates modules in the src/core/config/modules directory to separate
  5. * out operations into logical collections.
  6. *
  7. * @author n1474335 [n1474335@gmail.com]
  8. * @copyright Crown Copyright 2018
  9. * @license Apache-2.0
  10. */
  11. /* eslint no-console: ["off"] */
  12. import path from "path";
  13. import fs from "fs";
  14. import process from "process";
  15. import * as Ops from "../../operations/index.mjs";
  16. const dir = path.join(process.cwd() + "/src/core/config/");
  17. if (!fs.existsSync(dir)) {
  18. console.log("\nCWD: " + process.cwd());
  19. console.log("Error: generateConfig.mjs should be run from the project root");
  20. console.log("Example> node --experimental-modules src/core/config/scripts/generateConfig.mjs");
  21. process.exit(1);
  22. }
  23. const operationConfig = {},
  24. modules = {};
  25. /**
  26. * Generate operation config and module lists.
  27. */
  28. for (const opObj in Ops) {
  29. const op = new Ops[opObj]();
  30. operationConfig[op.name] = {
  31. module: op.module,
  32. description: op.description,
  33. infoURL: op.infoURL,
  34. inputType: op.inputType,
  35. outputType: op.presentType,
  36. flowControl: op.flowControl,
  37. manualBake: op.manualBake,
  38. args: op.args,
  39. };
  40. if ("checks" in op) {
  41. if ("input" in op.checks) {
  42. operationConfig[op.name].input = {};
  43. if ("regex" in op.checks.input) {
  44. operationConfig[op.name].input.regex = op.checks.input.regex;
  45. }
  46. if ("entropy" in op.checks.input) {
  47. operationConfig[op.name].input.entropy = op.checks.input.entropy;
  48. }
  49. }
  50. if ("output" in op.checks) {
  51. operationConfig[op.name].output = {};
  52. if ("regex" in op.checks.output) {
  53. operationConfig[op.name].output.regex = op.checks.output.regex;
  54. }
  55. if ("entropy" in op.checks.output) {
  56. operationConfig[op.name].output.entropy = op.checks.output.entropy;
  57. }
  58. if ("mime" in op.checks.output) {
  59. operationConfig[op.name].output.mime = op.checks.output.mime;
  60. }
  61. }
  62. }
  63. if (!(op.module in modules))
  64. modules[op.module] = {};
  65. modules[op.module][op.name] = opObj;
  66. }
  67. /**
  68. * Write OperationConfig.
  69. */
  70. fs.writeFileSync(
  71. path.join(dir, "OperationConfig.json"),
  72. JSON.stringify(operationConfig, null, 4)
  73. );
  74. console.log("Written OperationConfig.json");
  75. /**
  76. * Write modules.
  77. */
  78. if (!fs.existsSync(path.join(dir, "modules/"))) {
  79. fs.mkdirSync(path.join(dir, "modules/"));
  80. }
  81. for (const module in modules) {
  82. let code = `/**
  83. * THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateConfig.mjs
  84. *
  85. * @author n1474335 [n1474335@gmail.com]
  86. * @copyright Crown Copyright ${new Date().getUTCFullYear()}
  87. * @license Apache-2.0
  88. */
  89. `;
  90. for (const opName in modules[module]) {
  91. const objName = modules[module][opName];
  92. code += `import ${objName} from "../../operations/${objName}.mjs";\n`;
  93. }
  94. code += `
  95. const OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
  96. OpModules.${module} = {
  97. `;
  98. for (const opName in modules[module]) {
  99. const objName = modules[module][opName];
  100. code += ` "${opName}": ${objName},\n`;
  101. }
  102. code += `};
  103. export default OpModules;
  104. `;
  105. fs.writeFileSync(
  106. path.join(dir, `modules/${module}.mjs`),
  107. code
  108. );
  109. console.log(`Written ${module} module`);
  110. }
  111. /**
  112. * Write OpModules wrapper.
  113. */
  114. let opModulesCode = `/**
  115. * THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateConfig.mjs
  116. *
  117. * Imports all modules for builds which do not load modules separately.
  118. *
  119. * @author n1474335 [n1474335@gmail.com]
  120. * @copyright Crown Copyright ${new Date().getUTCFullYear()}
  121. * @license Apache-2.0
  122. */
  123. `;
  124. for (const module in modules) {
  125. opModulesCode += `import ${module}Module from "./${module}.mjs";\n`;
  126. }
  127. opModulesCode += `
  128. const OpModules = {};
  129. Object.assign(
  130. OpModules,
  131. `;
  132. for (const module in modules) {
  133. opModulesCode += ` ${module}Module,\n`;
  134. }
  135. opModulesCode += `);
  136. export default OpModules;
  137. `;
  138. fs.writeFileSync(
  139. path.join(dir, "modules/OpModules.mjs"),
  140. opModulesCode
  141. );
  142. console.log("Written OpModules.mjs");