Initialize a Node.js command line tool project
npm create @guoyunhe/cli my-clicd my-cli
npm init @guoyunhe/cli├── dist # Build output
│ ├── cjs # CJS format
│ │ ├── bin # CJS CLI scripts
│ │ │ └── my-cli.js
│ │ └── index.js # CJS API entry
│ ├── esm # ESM module format
│ │ ├── bin # ESM CLI scripts
│ │ │ └── my-cli.js
│ │ └── index.js # ESM API entry
│ └── dts # TypeScript types
│ └── index.d.ts
├── src # Source code
│ ├── bin # CLI scripts
│ │ └── my-cli.ts # CLI script
│ ├── index.test.ts # API unit test
│ └── index.ts # API entry (add all exports here)
├── .editorconfig
├── .gitignore
├── CHANGELOG.md
├── package.json
├── README.md
└── tsconfig.json# Build output
npm run build
# Build output in watch mode
npm run watch
# Format source code
npm run format
# Check lint issues
npm run lint
# Run unit tests (support all jest command options)
npm test
# Run unit tests in watch mode
npm test -- --watch
# Update unit test snapshots
npm test -- -u1.0.0 by default.
npm create @guoyunhe/cli my-cli --package-version 0.1.0Options: 12, 14, 16(default), 18.
npm create @guoyunhe/cli my-cli --node-version 14npm create @guoyunhe/cli my-cli --strictLet's say, you want to add a new bin called perform_health_check.
First, create src/bin/perform_health_check.ts, with shebang #!/usr/bin/env node:
#!/usr/bin/env node
console.log('Doing health check...');
// Add your code...Then, add bin entry to your package.json:
{
"bin": {
"perform_health_check": "dist/cjs/bin/perform_health_check.js"
}
}If you want your package to be pure ESM, you should modify the following attributes in package.json:
{
"type": "module",
"bin": {
"my-cli": "dist/esm/bin/my-cli.js"
},
"main": "dist/esm/index.js"
}