Just created my first cli tool in Typescript. Sharing here the experience and steps
I want to create a command line tool that will do something like this
ctc '[{ "index":3, "text":"I would like some gelb bananen bitte", "value":200 }, { "index":4, "text":"I hope batch update is working", "value":300 } ]'
//output
┌───────┬──────────────────────────────────────┬───────┐
│ index │ text │ value │
├───────┼──────────────────────────────────────┼───────┤
│ 3 │ I would like some gelb bananen bitte │ 200 │
│ 4 │ I hope batch update is working │ 300 │
└───────┴──────────────────────────────────────┴───────┘
Lets follow these steps to make it happen
Add this line at the beginning of your code to make your program running by bash command
#!/usr/bin/env node
First add bin field. This declares which commands you can run
{
"bin": {
"ctc": "dist/index.js"
}
}
This is required because you might use process.argv to read the command line arguments
npm install --save-dev @types/node
Also add this in tsconfig files
{
"compilerOptions": {
"types": ["node"]
}
}
First install transpile and install it globally
npx tsc ; npm link
Now run the program
ctc '[{ "index":3, "text":"I would like some gelb bananen bitte", "value":200 }, { "index":4, "text":"I hope batch update is working", "value":300 } ]'
//output
┌───────┬──────────────────────────────────────┬───────┐
│ index │ text │ value │
├───────┼──────────────────────────────────────┼───────┤
│ 3 │ I would like some gelb bananen bitte │ 200 │
│ 4 │ I hope batch update is working │ 300 │
└───────┴──────────────────────────────────────┴───────┘
