CLI Task Runner
Requirements
Usage
Create an entry point for the cli tool. Usually it will be index.js at the root of your package
in your package.json file, specify the "bin" property, with the name of your cli tool, and the file to run.
Typically it would be
"bin": {
"my-cli": "node ./index.js"
}
then you can invoke your tool with my-cli [command] <arg> -o ...
In your index.js file, do the following :
const myTask = {
name: "My Task",
startMessage: "Running my task. Bear with us",
prerequisitesChecker({ cliArgs, cliOptions }) {
},
configurator({ cliArgs, cliOptions }) {
},
steps: [
{
start: "Running first step",
run(config) {
},
error: "Could not run the step ! 😱",
completion: "step done !",
},
{
start: "Running step 2",
run(config) {
},
error: "Nope, didn't work",
completion: "Yay !",
},
],
};
const commands = [
{
syntax: "my task <arg1>",
options: [
["-o, --cli-option", "This one has no type so it will be a boolean"],
[
"-d, --destination-path [path]",
"Define a custom output path for the template project",
],
],
action: myTask,
},
];
const runCLI = require("cli-task-runner");
const { version, name } = require("./package.json");
runCLI(commands, name, version);
Cherry on top, your CLI tool can use some of the utility packages :
AWS :
const s3 = require("cli-task-runner/utils/aws");
const Contents = await s3.list("s3Bucket/Path/To/List");
const Body = await s3.getContent("s3Bucket/Path/To/File.json");
const Etag = await s3.getEtag("s3Bucket/Path/To/File.json");
const result = await s3.set("pathToPut", data, ACL = "public-read");
const resultCurried = await s3.set("pathToPut")(data);
const resultRemove = await s3.remove("pathToRemove");
File :
const file = require("cli-task-runner/utils/file");
const result = await file.writeFileAsync(path, data);
const result = await file.writeJsonToFile(path, jsonObject);
const result = file.copyFiles(source, files, destination, fileNameMapper = R.identity);
const result = await file.copyFolder(source, destination, fileNameMapper = R.identity);
Logger:
const logger = require("cli-task-runner/utils/logger");
logger.welcome({ packageName, version, scriptName }, message);
logger.log(...messages);
logger.error(message, error);
logger.warn(...messages);
logger.error(message)(error);
logger.success(...messages);
Render (EJS);
const renderFile = require("cli-task-runner/utils/render");
const result = await renderFile(templatePath, filePath, data);
Shell :
const shell = require("cli-task-runner/utils/shell");
try {
const stout = await shell.exec(cmd, options);
} catch (e) {
console.log(e)
}
shell.npm("add react@16.0.0 react-native@0.50.4", process.cwd(), options);