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

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