From fbb3a023157dc759427065889e3de77b32222897 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Mon, 2 Apr 2018 19:24:25 +0100 Subject: [PATCH] ESM: The operations index is now generated automatically --- Gruntfile.js | 2 +- src/core/config/modules/Default.mjs | 2 +- src/core/config/modules/OpModules.mjs | 2 +- .../config/{ => scripts}/generateConfig.mjs | 10 ++-- src/core/config/scripts/generateOpsIndex.mjs | 59 +++++++++++++++++++ src/core/operations/index.mjs | 25 +++++--- webpack.config.js | 5 +- 7 files changed, 87 insertions(+), 18 deletions(-) rename src/core/config/{ => scripts}/generateConfig.mjs (99%) create mode 100644 src/core/config/scripts/generateOpsIndex.mjs diff --git a/Gruntfile.js b/Gruntfile.js index f3a2e2bd..f81d508b 100755 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -133,7 +133,7 @@ module.exports = function (grunt) { dev: ["build/dev/*"], prod: ["build/prod/*"], node: ["build/node/*"], - config: ["src/core/config/OperationConfig.json", "src/core/config/modules/*"], + config: ["src/core/config/OperationConfig.json", "src/core/config/modules/*", "src/code/operations/index.mjs"], docs: ["docs/*", "!docs/*.conf.json", "!docs/*.ico", "!docs/*.png"], inlineScripts: ["build/prod/scripts.js"], }, diff --git a/src/core/config/modules/Default.mjs b/src/core/config/modules/Default.mjs index 46b35030..34043043 100644 --- a/src/core/config/modules/Default.mjs +++ b/src/core/config/modules/Default.mjs @@ -1,5 +1,5 @@ /** -* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/generateConfig.mjs +* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateConfig.mjs * * @author n1474335 [n1474335@gmail.com] * @copyright Crown Copyright 2018 diff --git a/src/core/config/modules/OpModules.mjs b/src/core/config/modules/OpModules.mjs index 58dfb09e..8ac4ac6d 100644 --- a/src/core/config/modules/OpModules.mjs +++ b/src/core/config/modules/OpModules.mjs @@ -1,5 +1,5 @@ /** -* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/generateConfig.mjs +* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateConfig.mjs * * Imports all modules for builds which do not load modules separately. * diff --git a/src/core/config/generateConfig.mjs b/src/core/config/scripts/generateConfig.mjs similarity index 99% rename from src/core/config/generateConfig.mjs rename to src/core/config/scripts/generateConfig.mjs index 1ccab8e1..b8905d50 100644 --- a/src/core/config/generateConfig.mjs +++ b/src/core/config/scripts/generateConfig.mjs @@ -1,6 +1,6 @@ /** * This script automatically generates OperationConfig.json, containing metadata - * for each opeartion in the src/core/operations directory. + * for each operation in the src/core/operations directory. * It also generates modules in the src/core/config/modules directory to separate * out operations into logical collections. * @@ -14,13 +14,13 @@ import path from "path"; import fs from "fs"; import process from "process"; -import * as Ops from "../operations/index"; +import * as Ops from "../../operations/index"; 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/generateConfig.mjs"); + console.log("Example> node --experimental-modules src/core/config/scripts/generateConfig.mjs"); process.exit(1); } @@ -69,7 +69,7 @@ fs.writeFile( */ for (const module in modules) { let code = `/** -* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/generateConfig.mjs +* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateConfig.mjs * * @author n1474335 [n1474335@gmail.com] * @copyright Crown Copyright ${new Date().getUTCFullYear()} @@ -113,7 +113,7 @@ export default OpModules; * Write OpModules wrapper. */ let opModulesCode = `/** -* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/generateConfig.mjs +* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateConfig.mjs * * Imports all modules for builds which do not load modules separately. * diff --git a/src/core/config/scripts/generateOpsIndex.mjs b/src/core/config/scripts/generateOpsIndex.mjs new file mode 100644 index 00000000..6a3b6f14 --- /dev/null +++ b/src/core/config/scripts/generateOpsIndex.mjs @@ -0,0 +1,59 @@ +/** + * This script automatically generates src/core/operations/index.mjs, containing + * imports for all operations in src/core/operations. + * + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ + +/*eslint no-console: ["off"] */ + +import path from "path"; +import fs from "fs"; +import process from "process"; + +const dir = path.join(process.cwd() + "/src/core/config/"); +if (!fs.existsSync(dir)) { + console.log("\nCWD: " + process.cwd()); + console.log("Error: generateOpsIndex.mjs should be run from the project root"); + console.log("Example> node --experimental-modules src/core/config/scripts/generateOpsIndex.mjs"); + process.exit(1); +} + +// Find all operation files +const opObjs = []; +fs.readdirSync(path.join(dir, "../operations")).forEach(file => { + if (!file.endsWith(".mjs") || file === "index.mjs") return; + opObjs.push(file.split(".mjs")[0]); +}); + +// Construct index file +let code = `/** +* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateOpsIndex.mjs +* +* @author n1474335 [n1474335@gmail.com] +* @copyright Crown Copyright ${new Date().getUTCFullYear()} +* @license Apache-2.0 +*/ +`; + +opObjs.forEach(obj => { + code += `import ${obj} from "./${obj}";\n`; +}); + +code += ` +export { +`; + +opObjs.forEach(obj => { + code += ` ${obj},\n`; +}); + +code += "};\n"; + +// Write file +fs.writeFileSync( + path.join(dir, "../operations/index.mjs"), + code +); diff --git a/src/core/operations/index.mjs b/src/core/operations/index.mjs index 45ba1e22..81c24acc 100644 --- a/src/core/operations/index.mjs +++ b/src/core/operations/index.mjs @@ -1,17 +1,24 @@ -import ToBase64 from "./ToBase64"; -import FromBase64 from "./FromBase64"; -import ToBase32 from "./ToBase32"; +/** +* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateOpsIndex.mjs +* +* @author n1474335 [n1474335@gmail.com] +* @copyright Crown Copyright 2018 +* @license Apache-2.0 +*/ import FromBase32 from "./FromBase32"; -import ShowBase64Offsets from "./ShowBase64Offsets"; -import ToHex from "./ToHex"; +import FromBase64 from "./FromBase64"; import FromHex from "./FromHex"; +import ShowBase64Offsets from "./ShowBase64Offsets"; +import ToBase32 from "./ToBase32"; +import ToBase64 from "./ToBase64"; +import ToHex from "./ToHex"; export { - ToBase64, - FromBase64, - ToBase32, FromBase32, + FromBase64, + FromHex, ShowBase64Offsets, + ToBase32, + ToBase64, ToHex, - FromHex }; diff --git a/webpack.config.js b/webpack.config.js index 91ede0be..72d31f4b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -45,7 +45,10 @@ module.exports = { }), new ExtractTextPlugin("styles.css"), new WebpackShellPlugin({ - onBuildStart: ["node --experimental-modules src/core/config/generateConfig.mjs"], + onBuildStart: [ + "node --experimental-modules src/core/config/scripts/generateOpsIndex.mjs", + "node --experimental-modules src/core/config/scripts/generateConfig.mjs", + ], }) ], resolve: {