Arguments parsing
Native
TheÂ
process.argv
property returns an array containing the command-line arguments passed when the Node.js process was launched.const process = require("node:process"); process.argv.forEach((val, index) => { console.log(`${index}:${val}`); }); //node process-argv.js one two three /* outputs: 0:/opt/nodejs/16.14.2/bin/node 1:/workspaces/MyBlog/a.js 2:one 3:two 4:three */
It returns an arrayăThe first element is process.execPath, and the second is the javascript script file pathďźand others are parametersă
Commander (convenient cli arguments handles)
- Method chaining
- process arguments
- autohelp
#!/usr/bin/env node const program = require("commander"); program .name("better-clone") // CLI name .version("0.0.1") // version .option("-v,--verbose", "verposity that can be increased"); // add commands program .command("clone <source> [destination]") // clone is the commandďźsource is a required argumentďźdestination is optional .option("-d,--depths <level>", "git clone depths") .description("cloe a repository into a newly created directory") .action((source, destination, cmdObj) => { console.log( `start cloning from ${source} to ${destination} with depth ${cmdObj.depths}` ); }); program.parse(process.argv); // need pass process.argv to get arguments // node ./commander.js clone ./src ./to --depths=2 // start cloning from ./src to ./to with depth 2
yargs
Build interactive command line tools, by parsing arguments and generating an elegant user interface.
yargs
yargs ⢠Updated Oct 18, 2024
Â
Iterative commands
inquirer.js (Friendly inputting)
- flexible CLI iteraction
inputănumberăconfirmălistărawlistăexpandăcheckboxăpasswordăEditor......
- compatible with Windows/ OSX /Linux
const inquirer = require('inquirer') inquirer .prompt([ /* Pass your questions in here */ { type: 'input', name: 'username', message: "What's ur name?" }, { type: 'checkbox', name: 'gender', message: "What's ur gender?", choices: [ 'male', 'female' ] }, { type: 'number', name: 'age', message: 'How old are u?', validate: input => Number.isNaN(Number(input)) ? 'Number Required!' : true }, { type: 'password', name: 'secret', message: 'Tell me a secret.', mask: 'x' } ]) .then(answers => { console.log(`Answers are:\n ${JSON.stringify(answers)}`) }) .catch(error => { if (error.isTtyError) { // Prompt couldn't be rendered in the current environment } else { // Something else when wrong } }) // node inquirer.js
React for interactive command-line apps
ink
vadimdemedes ⢠Updated Oct 18, 2024
Â
chalk - friendly outputs
- easy to use
why chalk can output color texts on commandďź
ANSI Escape Code
const chalk=require('chalk') const log=console.log const chalk=require('chalk') const log=console.log log(chalk.blue('\nhello')+'world'+chalk.red('!\n')) log(chalk.blue.bgRed.bold('Hello world!\n')) log(chalk.blue('Hello','word','Foo','bar','biz','baz\n')) log(chalk.red('Hello',chalk.underline.bgBlue('word')+'!\n'))
Ink
It provides the same component-based UI building experience that React offers in the browser, but for command-line apps
import React, {useState, useEffect} from 'react'; import {render, Text} from 'ink'; const Counter = () => { const [counter, setCounter] = useState(0); useEffect(() => { const timer = setInterval(() => { setCounter(previousCounter => previousCounter + 1); }, 100); return () => { clearInterval(timer); }; }, []); return <Text color="green">{counter} tests passed</Text>; }; render(<Counter />);
Node template rendering
Invoke other programs (shell.js
ăexeca
)
- CLI reuse
donât need to reinvent git/ npm /yarn etc.
- async operations to use computational intense operations
- Node use child_process module to create new process
cp.exec ă cp.spawn
shell.js
const shell = require('shelljs'); if(!shell.which('git')){ shell.echo('Sorry, this script requires git') shell.exit(1) } shell.rm('-rf','out/Release') shell.ls('*.js').forEach(function(file)=>{ shell.sed('-i','BUILD_VSRSION','v0.1.2',file) }) shell.cd('..') if(shell.exec('git commit -am "Auto-commit"').code !==0){ shell.echo('Error: Git commit failes') shell.exit(1) }
- support bash commands cross different platforms
- get results synchronously
execa
const execa =require('execa'); (async ()=>{ const {stdout}=await execa() console.log(stdout) })()
const execa =require('execa'); execa('echo',['unicorns']).stdout.pipe(process.stdout)
- promisify
- support Shebang cross different platforms
- acquire process exit signal
- support windows
Â
Other packages
Run multiple npm-scripts in parallel or sequential
check config schema
Event emitter working in browser
Hotkey configuration
hotkeys-js
jaywcjlove ⢠Updated Oct 17, 2024