npm install -g my-cli
my-cli greet John
MIT
-
Initialize a new npm project:
npm init -y
-
Install TypeScript and necessary dependencies:
npm install typescript ts-node @types/node --save-dev
-
Create a
tsconfig.json
file:{ "compilerOptions": { "target": "ES6", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true }, "include": ["src/**/*"] }
-
Create the project structure:
mkdir src touch src/index.ts
-
Write your TypeScript code in
src/index.ts
:console.log('Hello, TypeScript CLI!');
-
Add a script to
package.json
to run the TypeScript code:"scripts": { "start": "ts-node src/index.ts" }
-
Run the CLI program:
npm start
-
Project Structure:
- Organize your project files in a logical structure.
- Example:
/project-root ├── /src │ ├── /commands │ ├── /utils │ └── index.ts ├── /dist ├── package.json ├── tsconfig.json └── README.md
-
TypeScript Configuration:
- Ensure
tsconfig.json
is properly configured for your project needs. - Example:
{ "compilerOptions": { "target": "ES6", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true }, "include": ["src/**/*"] }
- Ensure
-
Scripts in
package.json
:- Add useful scripts for development and production.
- Example:
"scripts": { "start": "ts-node src/index.ts", "build": "tsc", "lint": "eslint . --ext .ts", "test": "jest" }
-
Dependencies:
- Separate dependencies and devDependencies appropriately.
- Example:
"dependencies": { "commander": "^9.0.0" }, "devDependencies": { "@types/node": "^22.5.0", "ts-node": "^10.9.2", "typescript": "^5.5.4", "eslint": "^8.0.0", "jest": "^29.0.0" }
-
Command Handling:
- Use a library like
commander
to handle CLI commands. - Example:
// src/index.ts import { Command } from 'commander'; const program = new Command(); program .name('my-cli') .description('My CLI tool') .version('1.0.0'); program .command('greet <name>') .description('Greet a person') .action((name) => { console.log(`Hello, ${name}!`); }); program.parse(process.argv);
- Use a library like
-
Error Handling:
- Implement proper error handling and user-friendly messages.
- Example:
// src/utils/errorHandler.ts export function handleError(error: Error): void { console.error(`Error: ${error.message}`); process.exit(1); }
-
Documentation:
-
Maintain a
README.md
with clear instructions on how to use and develop the CLI. -
Example:
# My CLI Tool ## Installation ```sh npm install -g my-cli
my-cli greet John
-
-
Testing:
- Write tests for your CLI commands.
- Example:
// src/__tests__/index.test.ts import { execSync } from 'child_process'; test('greet command', () => { const output = execSync('ts-node src/index.ts greet John').toString(); expect(output).toContain('Hello, John!'); });
-
commander: A popular library for building command-line interfaces.
npm install commander
-
yargs: Another powerful library for building CLI tools.
npm install yargs
-
inquirer: A library for creating interactive command-line prompts.
npm install inquirer
-
chalk: Used for styling terminal string output.
npm install chalk
-
ora: A library for creating elegant terminal spinners.
npm install ora
-
figlet: A library for creating ASCII art from text.
npm install figlet
-
dotenv: For loading environment variables from a
.env
file.npm install dotenv
-
log4js: A logging library for logging messages to the console and files.
npm install log4js
To import the correct Jest files for testing in a TypeScript project, follow these steps:
-
Install Jest and its TypeScript dependencies:
npm install --save-dev jest ts-jest @types/jest
-
Create a Jest configuration file:
npx ts-jest config:init
-
Update the Jest configuration in
jest.config.js
:module.exports = { preset: 'ts-jest', testEnvironment: 'node', testMatch: ['**/__tests__/**/*.test.ts'], };
-
Update your test file to use Jest:
// src/__tests__/index.test.ts import { execSync } from 'child_process'; test('greet command', () => { const output = execSync('ts-node src/index.ts greet John').toString(); expect(output).toContain('Hello, John!'); });
-
Add a test script to
package.json
:"scripts": { "test": "jest" }
-
Run the tests:
npm test
- Install ESLint and necessary plugins.
- Initialize ESLint configuration.
- Configure ESLint for TypeScript.
- Add ESLint scripts to
package.json
. - Create an ESLint ignore file.
-
Install ESLint and necessary plugins:
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
-
Initialize ESLint configuration:
npx eslint --init
-
Configure ESLint for TypeScript: Update the
.eslintrc.json
file:{ "env": { "browser": true, "es2021": true, "node": true }, "extends": [ "eslint:recommended", "plugin:@typescript-eslint/recommended" ], "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": "latest", "sourceType": "module" }, "plugins": [ "@typescript-eslint" ], "rules": { // Add custom rules here } }
-
Add ESLint scripts to
package.json
:"scripts": { "lint": "eslint . --ext .ts", "lint:fix": "eslint . --ext .ts --fix" }
-
Create an ESLint ignore file: Create a
.eslintignore
file:node_modules/ dist/
/project-root
├── /src
│ ├── /commands
│ ├── /utils
│ └── index.ts
├── /dist
├── package.json
├── tsconfig.json
├── .eslintrc.json
├── .eslintignore
└── README.md
- Use the
npm-check-updates
(ncu) package to update the dependencies inpackage.json
. - Add a script in
package.json
to automate the dependency update process.
-
Install
npm-check-updates
:npm install -g npm-check-updates
-
Add a script in
package.json
:{ "scripts": { "update-deps": "ncu -u && npm install" } }
-
Run the script:
npm run update-deps
This will update the dependencies in package.json
to their latest versions and install them.