Automate all the things with a team-centric CLI. Abstract away annoying day-to-day tasks and eliminate tribal team knowledge by building your team their very own CLI.
mkdir teamname-cli
cd teamname-cli
git init
npm init
npm install --save team-cli
Then in an index.js
you could write the following:
#!/usr/bin/env node
const { resolve } = require('path');
const cli = require('team-cli');
const commandsDir = resolve(__dirname, 'commands');
cli(commandsDir);
Then customize your package.json
to include a path to the bin:
"bin": {
"NAME_OF_TOOL": "./index.js"
}
Then you may make a commands
directory with files like command-foo.js
:
const run = require('team-cli/terminal');
const { resolve } = require('path');
const script = resolve(__dirname, 'foo.sh'); // also supports bash .ps1 scripts
const action = async param => {
await run(script, param)
// Or, run any Node code you wish
};
module.exports = {
title: 'foo <param>',
description: 'Calls foo',
action,
}
node ./index.js --help
Any command can export the following options:
{
title: 'foo', // or 'foo <required_param>' or 'foo [optional_param]'
action: (param) => {} // function with param as a string or undefined
description: 'Calls foo', // optional
alias: 'f', // optional
option: ['-f, --force', 'Forces something to happen'], // optional, this will become available globally not just per-command
}
At any time a --help
or -h
may be passed to log commands to the console.
Optionally, you may find it useful to walk users through a guided CLI experience with prompts to your users. I suggest prompts for this task, but any tool of your choice will work within an action.
const action = async (cmd) => {
if (!cmd) {
let { value: cmdResponse } = await prompts({
type: 'text',
name: 'value',
message: 'Which git command would you like to run?',
});
cmd = cmdResponse
}
await run(`git ${cmd}`, '~/aCoolRepo');
};
The environment's log level can be changed with process.env.LOG_LEVEL
to any of winston's supported log levels including verbose
.
To customize where logs are saved, pass a second param in your index.js
's cli
call like so:
cli(commandsDir, logsDir)