Make help function use OperationConfig.json file

Tidy up Gruntfile execs
This commit is contained in:
d98762625 2018-07-06 11:13:38 +01:00
parent f101eefc56
commit 2480dca473
8 changed files with 1396 additions and 165 deletions

1
.gitignore vendored
View File

@ -10,5 +10,6 @@ src/core/config/modules/*
src/core/config/OperationConfig.json
src/core/operations/index.mjs
src/node/index.mjs
src/node/config/OperationConfig.json
**/*.DS_Store

View File

@ -22,15 +22,15 @@ module.exports = function (grunt) {
// Tasks
grunt.registerTask("dev",
"A persistent task which creates a development build whenever source files are modified.",
["clean:dev", "exec:generateOpsIndex", "exec:generateConfig", "concurrent:dev"]);
["clean:dev", "exec:generateConfig", "concurrent:dev"]);
grunt.registerTask("node",
"Compiles CyberChef into a single NodeJS module.",
["clean:node", "clean:config", "exec:generateOpsIndex", "exec:generateNodeIndex", "webpack:node", "chmod:build"]);
["clean", "exec:generateConfig", "exec:generateNodeIndex", "webpack:node", "chmod:build"]);
grunt.registerTask("test",
"A task which runs all the tests in test/tests.",
["clean", "exec:generateOpsIndex", "exec:generateNodeIndex", "exec:generateConfig", "exec:tests"]);
["clean", "exec:generateConfig", "exec:generateNodeIndex", "exec:generateConfig", "exec:tests"]);
grunt.registerTask("docs",
"Compiles documentation in the /docs directory.",
@ -38,7 +38,7 @@ module.exports = function (grunt) {
grunt.registerTask("prod",
"Creates a production-ready build. Use the --msg flag to add a compile message.",
["eslint", "clean:prod", "exec:generateOpsIndex", "exec:generateConfig", "webpack:web", "inline", "chmod"]);
["eslint", "clean:prod", "exec:generateConfig", "webpack:web", "inline", "chmod"]);
grunt.registerTask("default",
"Lints the code base",
@ -46,7 +46,7 @@ module.exports = function (grunt) {
grunt.registerTask("inline",
"Compiles a production build of CyberChef into a single, portable web page.",
["exec:generateOpsIndex", "exec:generateConfig", "webpack:webInline", "runInliner", "clean:inlineScripts"]);
["exec:generateConfig", "webpack:webInline", "runInliner", "clean:inlineScripts"]);
grunt.registerTask("runInliner", runInliner);
@ -135,7 +135,7 @@ module.exports = function (grunt) {
dev: ["build/dev/*"],
prod: ["build/prod/*"],
node: ["build/node/*"],
config: ["src/core/config/OperationConfig.json", "src/core/config/modules/*", "src/code/operations/index.mjs", "src/node/index.mjs"],
config: ["src/core/config/OperationConfig.json", "src/core/config/modules/*", "src/code/operations/index.mjs", "src/node/index.mjs", "src/node/config/OperationConfig.json"],
docs: ["docs/*", "!docs/*.conf.json", "!docs/*.ico", "!docs/*.png"],
inlineScripts: ["build/prod/scripts.js"],
},
@ -379,6 +379,7 @@ module.exports = function (grunt) {
generateConfig: {
command: [
"echo '\n--- Regenerating config files. ---'",
"node --experimental-modules src/core/config/scripts/generateOpsIndex.mjs",
"mkdir -p src/core/config/modules",
"echo 'export default {};\n' > src/core/config/modules/OpModules.mjs",
"echo '[]\n' > src/core/config/OperationConfig.json",
@ -386,17 +387,16 @@ module.exports = function (grunt) {
"echo '--- Config scripts finished. ---\n'"
].join(";")
},
generateOpsIndex: {
command: [
"echo '\n--- Regenerating operations index. ---'",
"node --experimental-modules src/core/config/scripts/generateOpsIndex.mjs",
"echo '\n--- Ops index generated. ---'",
].join(";")
},
generateNodeIndex: {
command: [
"echo '\n--- Regenerating node index ---'",
"mkdir -p src/core/config/modules",
"echo 'Copying OperationConfig.json and wiping original'",
"cp src/core/config/OperationConfig.json src/node/config/OperationConfig.json",
"echo 'export default {};\n' > src/core/config/modules/OpModules.mjs",
"echo '[]\n' > src/core/config/OperationConfig.json",
"echo '\n OperationConfig.json copied to src/node/config. Modules wiped.'",
"echo 'export default {};\n' > src/core/config/modules/OpModules.mjs",
"echo '[]\n' > src/core/config/OperationConfig.json",
"node --experimental-modules src/node/config/scripts/generateNodeIndex.mjs",

195
package-lock.json generated
View File

@ -4845,9 +4845,9 @@
"dev": true
},
"grunt": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/grunt/-/grunt-1.0.2.tgz",
"integrity": "sha1-TmpeaVtwRy/VME9fqeNCNoNqc7w=",
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/grunt/-/grunt-1.0.3.tgz",
"integrity": "sha512-/JzmZNPfKorlCrrmxWqQO4JVodO+DVd5XX4DkocL/1WlLlKVLE9+SdEIempOAxDhWPysLle6afvn/hg7Ck2k9g==",
"dev": true,
"requires": {
"coffeescript": "1.10.0",
@ -4858,16 +4858,49 @@
"glob": "7.0.6",
"grunt-cli": "1.2.0",
"grunt-known-options": "1.1.0",
"grunt-legacy-log": "1.0.2",
"grunt-legacy-util": "1.0.0",
"grunt-legacy-log": "2.0.0",
"grunt-legacy-util": "1.1.1",
"iconv-lite": "0.4.23",
"js-yaml": "3.5.5",
"minimatch": "3.0.4",
"mkdirp": "0.5.1",
"nopt": "3.0.6",
"path-is-absolute": "1.0.1",
"rimraf": "2.2.8"
"rimraf": "2.6.2"
},
"dependencies": {
"ansi-styles": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
"integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
"dev": true,
"requires": {
"color-convert": "1.9.1"
}
},
"async": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz",
"integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=",
"dev": true
},
"chalk": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz",
"integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==",
"dev": true,
"requires": {
"ansi-styles": "3.2.1",
"escape-string-regexp": "1.0.5",
"supports-color": "5.4.0"
}
},
"colors": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.1.2.tgz",
"integrity": "sha1-FopHAXVran9RoSzgyXv6KMCE7WM=",
"dev": true
},
"esprima": {
"version": "2.7.3",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-2.7.3.tgz",
@ -4900,6 +4933,49 @@
"resolve": "1.1.7"
}
},
"grunt-legacy-log": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/grunt-legacy-log/-/grunt-legacy-log-2.0.0.tgz",
"integrity": "sha512-1m3+5QvDYfR1ltr8hjiaiNjddxGdQWcH0rw1iKKiQnF0+xtgTazirSTGu68RchPyh1OBng1bBUjLmX8q9NpoCw==",
"dev": true,
"requires": {
"colors": "1.1.2",
"grunt-legacy-log-utils": "2.0.1",
"hooker": "0.2.3",
"lodash": "4.17.10"
}
},
"grunt-legacy-log-utils": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/grunt-legacy-log-utils/-/grunt-legacy-log-utils-2.0.1.tgz",
"integrity": "sha512-o7uHyO/J+i2tXG8r2bZNlVk20vlIFJ9IEYyHMCQGfWYru8Jv3wTqKZzvV30YW9rWEjq0eP3cflQ1qWojIe9VFA==",
"dev": true,
"requires": {
"chalk": "2.4.1",
"lodash": "4.17.10"
}
},
"grunt-legacy-util": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/grunt-legacy-util/-/grunt-legacy-util-1.1.1.tgz",
"integrity": "sha512-9zyA29w/fBe6BIfjGENndwoe1Uy31BIXxTH3s8mga0Z5Bz2Sp4UCjkeyv2tI449ymkx3x26B+46FV4fXEddl5A==",
"dev": true,
"requires": {
"async": "1.5.2",
"exit": "0.1.2",
"getobject": "0.1.0",
"hooker": "0.2.3",
"lodash": "4.17.10",
"underscore.string": "3.3.4",
"which": "1.3.0"
}
},
"has-flag": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
"integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
"dev": true
},
"js-yaml": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.5.5.tgz",
@ -4910,11 +4986,24 @@
"esprima": "2.7.3"
}
},
"rimraf": {
"version": "2.2.8",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.2.8.tgz",
"integrity": "sha1-5Dm+Kq7jJzIZUnMPmaiSnk/FBYI=",
"dev": true
"supports-color": {
"version": "5.4.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz",
"integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==",
"dev": true,
"requires": {
"has-flag": "3.0.0"
}
},
"underscore.string": {
"version": "3.3.4",
"resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-3.3.4.tgz",
"integrity": "sha1-LCo/n4PmR2L9xF5s6sZRQoZCE9s=",
"dev": true,
"requires": {
"sprintf-js": "1.0.3",
"util-deprecate": "1.0.2"
}
}
}
},
@ -5097,82 +5186,6 @@
"integrity": "sha1-pCdO6zL6dl2lp6OxcSYXzjsUQUk=",
"dev": true
},
"grunt-legacy-log": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/grunt-legacy-log/-/grunt-legacy-log-1.0.2.tgz",
"integrity": "sha512-WdedTJ/6zCXnI/coaouzqvkI19uwqbcPkdsXiDRKJyB5rOUlOxnCnTVbpeUdEckKVir2uHF3rDBYppj2p6N3+g==",
"dev": true,
"requires": {
"colors": "1.1.2",
"grunt-legacy-log-utils": "1.0.0",
"hooker": "0.2.3",
"lodash": "4.17.10"
},
"dependencies": {
"colors": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.1.2.tgz",
"integrity": "sha1-FopHAXVran9RoSzgyXv6KMCE7WM=",
"dev": true
}
}
},
"grunt-legacy-log-utils": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/grunt-legacy-log-utils/-/grunt-legacy-log-utils-1.0.0.tgz",
"integrity": "sha1-p7ji0Ps1taUPSvmG/BEnSevJbz0=",
"dev": true,
"requires": {
"chalk": "1.1.3",
"lodash": "4.3.0"
},
"dependencies": {
"lodash": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.3.0.tgz",
"integrity": "sha1-79nEpuxT87BUEkKZFcPkgk5NJaQ=",
"dev": true
}
}
},
"grunt-legacy-util": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/grunt-legacy-util/-/grunt-legacy-util-1.0.0.tgz",
"integrity": "sha1-OGqnjcbtUJhsKxiVcmWxtIq7m4Y=",
"dev": true,
"requires": {
"async": "1.5.2",
"exit": "0.1.2",
"getobject": "0.1.0",
"hooker": "0.2.3",
"lodash": "4.3.0",
"underscore.string": "3.2.3",
"which": "1.2.14"
},
"dependencies": {
"async": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz",
"integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=",
"dev": true
},
"lodash": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.3.0.tgz",
"integrity": "sha1-79nEpuxT87BUEkKZFcPkgk5NJaQ=",
"dev": true
},
"which": {
"version": "1.2.14",
"resolved": "https://registry.npmjs.org/which/-/which-1.2.14.tgz",
"integrity": "sha1-mofEN48D6CfOyvGs31bHNsAcFOU=",
"dev": true,
"requires": {
"isexe": "2.0.0"
}
}
}
},
"grunt-webpack": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/grunt-webpack/-/grunt-webpack-3.1.2.tgz",
@ -5441,7 +5454,7 @@
"integrity": "sha512-xG6E3g2V/huu/WwRcrX3AFRmAUFkU7PWlgF/QFLtuRitI+NxvJcYTFthb24rB7/mhKE3khSIxB11A8IQTJ2N9w==",
"dev": true,
"requires": {
"grunt": "1.0.2",
"grunt": "1.0.3",
"grunt-contrib-copy": "1.0.0",
"grunt-contrib-jshint": "1.1.0",
"grunt-contrib-watch": "1.1.0",
@ -10862,12 +10875,6 @@
}
}
},
"underscore.string": {
"version": "3.2.3",
"resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-3.2.3.tgz",
"integrity": "sha1-gGmSYzZl1eX8tNsfs6hi62jp5to=",
"dev": true
},
"union-value": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.0.tgz",

View File

@ -39,7 +39,7 @@
"exports-loader": "^0.7.0",
"extract-text-webpack-plugin": "^4.0.0-alpha0",
"file-loader": "^1.1.11",
"grunt": ">=1.0.2",
"grunt": "^1.0.3",
"grunt-accessibility": "~6.0.0",
"grunt-chmod": "~1.1.1",
"grunt-concurrent": "^2.3.1",

View File

@ -7,6 +7,7 @@
*/
import SyncDish from "./SyncDish";
import OperationConfig from "./config/OperationConfig.json";
/**
* Extract default arg value from operation argument
@ -134,56 +135,31 @@ export function decapitalise(name) {
}
/**
* Extract properties from an operation by instantiating it and
* returning some of its properties for reference.
* @param {Operation} Operation - the operation to extract info from
* @returns {Object} operation properties
*/
function extractOperationInfo(Operation) {
const operation = new Operation();
return {
name: decapitalise(operation.name).replace(/ /g, ""),
module: operation.module,
description: operation.description,
inputType: operation.inputType,
outputType: operation.outputType,
// Make arg names lowercase, no spaces to encourage non-sentence
// caps in repl
args: Object.assign([], operation.args).map((s) => {
s.name = decapitalise(s.name).replace(/ /g, "");
return s;
})
};
}
/**
* @namespace Api
* @param {Operation[]} operations - an object filled with operations.
* @param {String} searchTerm - the name of the operation to get help for.
* Case and whitespace are ignored in search.
* @returns {Function} taking search term and outputting description.
* @returns {Object} Describe function matching searchTerm.
*/
export function help(operations) {
return function(searchTerm) {
let sanitised = false;
if (typeof searchTerm === "string") {
sanitised = searchTerm;
} else if (typeof searchTerm === "function") {
sanitised = searchTerm.opName;
}
export function help(searchTerm) {
let sanitised = false;
if (typeof searchTerm === "string") {
sanitised = searchTerm;
} else if (typeof searchTerm === "function") {
sanitised = searchTerm.opName;
}
if (!sanitised) {
return null;
}
const operation = operations
.find(o => o.name.toLowerCase() === sanitised.replace(/ /g, "").toLowerCase());
if (operation) {
return extractOperationInfo(operation);
}
if (!sanitised) {
return null;
};
}
const key = Object.keys(OperationConfig)
.find(o => o.replace(/ /g, "").toLowerCase() === sanitised.replace(/ /g, "").toLowerCase());
if (key) {
const result = OperationConfig[key];
result.name = key;
return result;
}
return null;
}

View File

@ -28,7 +28,7 @@ if (!fs.existsSync(dir)) {
}
let code = `/**
* THIS FILE IS AUTOMATICALLY GENERATED BY src/core/config/scripts/generateOpsIndex.mjs
* THIS FILE IS AUTOMATICALLY GENERATED BY src/node/config/scripts/generateNodeIndex.mjs
*
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright ${new Date().getUTCFullYear()}
@ -79,14 +79,7 @@ code += ` };
}
const chef = generateChef();
chef.help = help([\n`;
includedOperations.forEach((op) => {
code += ` core_${op},\n`;
});
code +=`]);
chef.help = help;
`;
includedOperations.forEach((op) => {
@ -108,6 +101,6 @@ code += "};\n";
fs.writeFileSync(
path.join(dir, "/index.mjs"),
path.join(dir, "./index.mjs"),
code
);

1254
src/node/repl-index.mjs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -115,7 +115,7 @@ TestRegister.addApiTests([
it("chef.help: should describe a operation", () => {
const result = chef.help("tripleDESDecrypt");
assert.strictEqual(result.name, "tripleDESDecrypt");
assert.strictEqual(result.name, "Triple DES Decrypt");
assert.strictEqual(result.module, "Ciphers");
assert.strictEqual(result.inputType, "string");
assert.strictEqual(result.outputType, "string");
@ -130,7 +130,7 @@ TestRegister.addApiTests([
it("chef.help: takes a wrapped operation as input", () => {
const result = chef.help(chef.toBase32);
assert.strictEqual(result.name, "toBase32");
assert.strictEqual(result.name, "To Base32");
assert.strictEqual(result.module, "Default");
})
]);