Home > Web Front-end > JS Tutorial > Creating a TypeScript CLI for Your Monorepo

Creating a TypeScript CLI for Your Monorepo

DDD
Release: 2024-12-10 07:48:10
Original
739 people have browsed it

Creating a TypeScript CLI for Your Monorepo

I like to create local CLIs for my Monorepo to automate tasks like build and deploy. These tasks often require more than just chaining a few commands in an npm script (like rimraf dist && tsc).

Using commander.js and tsx, we can create executable programs written in TypeScript that run from the command line like any other CLI tool.

#!/usr/bin/env -S pnpm tsx
import { Command } from 'commander';

const program = new Command()
  .name('monorepo')
  .description('CLI for Monorepo')
  .version('1.0.0');

program
  .command('build')
  .description('Build the monorepo')
  .action(async () => {
    console.log('Building...');
    // run your build steps ...
  });

program
  .command('deploy')
  .description('Deploy the monorepo')
  .action(async () => {
    console.log('Deploying...');
    // run your deploy steps ...
  });

await program.parseAsync(process.argv);
Copy after login

Save this script as cli (or any name you prefer) in your project root and make it executable with chmod x cli. You can then run it directly using ./cli:

$ ./cli
Usage: monorepo [options] [command]

CLI for Monorepo

Options:
  -V, --version   output the version number
  -h, --help      display help for command

Commands:
  build           Build the monorepo
  deploy          Deploy the monorepo
  help [command]  display help for command
Copy after login

The magic that allows you to run this without node, npx, or even a .ts extension is in the first line - the shebang:

#!/usr/bin/env -S pnpm tsx
Copy after login

This shebang tells your shell which program should execute this file. Behind the scenes, it translates your ./cli command into pnpm tsx cli. This works with other package managers too - you can use npm or yarn instead of pnpm.

The above is the detailed content of Creating a TypeScript CLI for Your Monorepo. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template