CyberChef/src/core/config/scripts/generateConfig.mjs

148 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-03-27 00:14:23 +02:00
/**
* This script automatically generates OperationConfig.json, containing metadata
* for each operation in the src/core/operations directory.
2018-03-27 00:14:23 +02:00
* It also generates modules in the src/core/config/modules directory to separate
* out operations into logical collections.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
2019-11-14 09:55:27 +01:00
/* eslint no-console: ["off"] */
2018-03-27 00:14:23 +02:00
import path from "path";
import fs from "fs";
import process from "process";
import * as Ops from "../../operations/index.mjs";
2018-03-27 00:14:23 +02:00
const dir = path.join(process.cwd() + "/src/core/config/");
if (!fs.existsSync(dir)) {
console.log("\nCWD: " + process.cwd());
console.log("Error: generateConfig.mjs should be run from the project root");
console.log("Example> node --experimental-modules src/core/config/scripts/generateConfig.mjs");
2018-03-27 00:14:23 +02:00
process.exit(1);
}
const operationConfig = {},
2018-03-27 00:14:23 +02:00
modules = {};
2016-11-28 11:42:58 +01:00
/**
2018-03-27 00:14:23 +02:00
* Generate operation config and module lists.
2016-11-28 11:42:58 +01:00
*/
for (const opObj in Ops) {
const op = new Ops[opObj]();
2018-03-27 00:14:23 +02:00
operationConfig[op.name] = {
module: op.module,
2018-03-27 00:14:23 +02:00
description: op.description,
infoURL: op.infoURL,
inputType: op.inputType,
outputType: op.presentType,
2018-03-27 00:14:23 +02:00
flowControl: op.flowControl,
manualBake: op.manualBake,
2020-02-25 12:27:03 +01:00
args: op.args,
2020-03-24 12:06:37 +01:00
checks: op.checks
2018-03-27 00:14:23 +02:00
};
2019-07-05 13:22:52 +02:00
if (!(op.module in modules))
2018-03-27 00:14:23 +02:00
modules[op.module] = {};
modules[op.module][op.name] = opObj;
}
2016-11-28 11:42:58 +01:00
/**
2018-03-27 00:14:23 +02:00
* Write OperationConfig.
2016-11-28 11:42:58 +01:00
*/
fs.writeFileSync(
2018-03-27 00:14:23 +02:00
path.join(dir, "OperationConfig.json"),
JSON.stringify(operationConfig, null, 4)
2018-03-27 00:14:23 +02:00
);
console.log("Written OperationConfig.json");
2016-11-28 11:42:58 +01:00
/**
2018-03-27 00:14:23 +02:00
* Write modules.
2016-11-28 11:42:58 +01:00
*/
if (!fs.existsSync(path.join(dir, "modules/"))) {
fs.mkdirSync(path.join(dir, "modules/"));
}
for (const module in modules) {
2018-03-27 00:14:23 +02:00
let code = `/**
* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateConfig.mjs
2018-03-27 00:14:23 +02:00
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright ${new Date().getUTCFullYear()}
* @license Apache-2.0
*/
`;
for (const opName in modules[module]) {
2018-03-27 00:14:23 +02:00
const objName = modules[module][opName];
code += `import ${objName} from "../../operations/${objName}.mjs";\n`;
2018-03-27 00:14:23 +02:00
}
code += `
const OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
2018-03-27 00:14:23 +02:00
OpModules.${module} = {
`;
for (const opName in modules[module]) {
2018-03-27 00:14:23 +02:00
const objName = modules[module][opName];
code += ` "${opName}": ${objName},\n`;
}
code += `};
export default OpModules;
`;
fs.writeFileSync(
2018-03-27 00:14:23 +02:00
path.join(dir, `modules/${module}.mjs`),
code
2018-03-27 00:14:23 +02:00
);
console.log(`Written ${module} module`);
2018-03-27 00:14:23 +02:00
}
/**
* Write OpModules wrapper.
*/
let opModulesCode = `/**
* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateConfig.mjs
2018-03-27 00:14:23 +02:00
*
* Imports all modules for builds which do not load modules separately.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright ${new Date().getUTCFullYear()}
* @license Apache-2.0
*/
`;
for (const module in modules) {
opModulesCode += `import ${module}Module from "./${module}.mjs";\n`;
2018-03-27 00:14:23 +02:00
}
opModulesCode += `
const OpModules = {};
2018-03-27 00:14:23 +02:00
Object.assign(
OpModules,
`;
for (const module in modules) {
2018-03-27 00:14:23 +02:00
opModulesCode += ` ${module}Module,\n`;
}
opModulesCode += `);
export default OpModules;
`;
fs.writeFileSync(
2018-03-27 00:14:23 +02:00
path.join(dir, "modules/OpModules.mjs"),
opModulesCode
2018-03-27 00:14:23 +02:00
);
console.log("Written OpModules.mjs");