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, short hands and documentation for scripts becomes a breeze.

The final thing added to our tool chain 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-project. Initialize your new project with npm init and then run

npm install --save typescript ts-node commander inquirer

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

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