Henning’s Blog.

Writing modern CLI tools

I'm a huge fan of automation – where it makes sense. Normally, I'd either use our CI (GitHub Actions) for everything code related and small node scripts for everything else. Since we're betting on TypeScript I spent some time upgrading my tech stack for these small scripts.

With ts-node we can run node scripts in TypeScript.

commander.js enables us to write interactive scripts with ease. Adding options, shorthands and documentation for scripts becomes a breeze.

The final thing added to our toolchain is inquirer.js which allows us to ask for user inputs and confirmation in scripts.

Tutorial

To use this tech stack in your next mini-projec. Initialize your new project with npm init and then run

npm install --save typescript ts-node commander inquirer

to install all required dependencies. In order to use modern syntax that you're used to from Frontend development (ie. ES modules) add the following to your tsconfig.json:

{
    "compilerOptions": {
        "esModuleInterop": true,
        "resolveJsonModule": true
    },
    "include": ["your-script"],
    "exclude": ["node_modules"]
}