step 1: mkdir my-cli && cd my-cli && npm init -y

step 2: npm i -S commander

step 3: mkdir bin && mkdir lib

step 4:

// lib/index.js

const { program } = require('commander');
const { resolve } = require('path');
const { spawnSync } = require('child_process');

program
.version('0.01')
.description('My CLI')
.option('-n, --name <name>'.'your name'.'cwy') // <opt> mandatory opt 
.option('-a, --age [age]'.'your name'.'cwy') // [opt] optional opt 


program
.command('create)
.description('create my app')
.argument('<appName>') .action((appName) => { spawnSync('sh'['-c', `npx create-react-app ${appName} --template typescript`], { cwd: process.cwd(), stdio: [process.stdin, process.stdout, process.stderr] }); spawnSync('sh'['-c', 'echo y | npm run eject'], { cwd: resolve(process.cwd(), appName), stdio: [process.stdin, process.stdout, process.stderr] }) }) program.parse(process.argv); // const options = program.opts();Copy the code

step 5:

//bin/index.js

#!/user/bin/env node
require('.. /lib/index');
Copy the code

step 6:

//package.json

"bin": {
    "my-cli": "./bin/index.js" // this cmd will link to node_modules after npm install
}
Copy the code