npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typescript-to-cli

v0.5.1

Published

Create a cli from a typescript module.

Downloads

8

Readme

typescript-to-cli

Transform your typescript module into a CLI

typescript-to-cli leverages the typescript type system to generate a CLI based on the exported function signature of your module.

  • Parses the program options
  • Validates if the options have the right type
  • Suggest correction if the options contain a typo
  • Generates help based on the documentation

:warning:

This is an experimental project! A lot of features are missing and it probably contains some bugs. If one of your use case is not supported, please create an issue or contact me on twitter.

:warning:

Usage

Let's take the following module as an example.

send-ships.ts

/**
 * Send ships to the specified destination
 *
 * @param destination the ships target
 * @param numberOfShips the number of ships to send
 * @param armed whether the ships are equipped with weapons or not
 */
export default function(
  destination: string,
  numberOfShips: number,
  armed: boolean
) {
  console.log(
    `You sent ${numberOfShips} ${armed ? "armed" : ""} ships to ${destination}.`
  );
}

Generate the CLI

$ npx typescript-to-cli ./send-ships.ts
send-ships.js CLI has been generated.

tsconfig.json resolution

If typescript-to-cli detects a tsconfig.json in the current folder, it will use it to generate the CLI. If you want to use a custom tsconfig.json, you can use the command line option --project (or just -p) that specifies the path of a directory containing a tsconfig.json file, or a path to a valid .json file containing the configurations.

$ npx typescript-to-cli ./send-ships.ts --project ./path-to-config/tsconfig.json
send-ships.js CLI has been generated.

Execute the CLI

$ ./send-ships.js --destination Mars --number-of-ships 5 --armed
You sent 5 armed ships to Mars.

Or with the = symbol between options and values

$ ./send-ships.js --destination=Europa --number-of-ships=2
You sent 2 ships to Europa.

Examples

Missing option

$ ./send-ships.js --number-of-ships 5 --armed
Missing option --destination

Invalid option type

$ ./send-ships.js --destination Mars --number-of-ships X --armed
--number-of-ships should be a number

Non required option

export default function(option: string | undefined) {}

or

export default function(option?: string) {}

Restrict option with string literal types

// cli.ts

export default function(primaryColor: "cyan" | "magenta" | "yellow") {}
$ npx typescript-to-cli ./cli.ts
cli.js CLI has been generated.

$ ./cli.js --primary-color green
--primary-color does not accept the value "green". Allowed values: cyan, magenta, yellow

Display help

$ ./send-ships.js --help
Usage send-ships.js [options]

Send ships to the specified destination

Options:

--help                     output usage information
--destination <string>     the ships destination
--number-of-ship <number>  the number of ships to send
--armed                    whether the ships are equipped with weapons or not

Generate the CLI from a js module and JSDoc type annotations

typescript-to-cli can also infer the parameters types from JSDoc annotations. However, your tsconfig.json should allow js files with "allowJs": true and an outDir to avoid overriding the input file. Let's take the same previous example but with JSDoc type annotations instead.

send-ships.js

/**
 * Send ships to the specified destination
 *
 * @param {string} destination the ships target
 * @param {number} numberOfShips the number of ships to send
 * @param {boolean} armed whether the ships are equipped with weapons or not
 */
export default function(destination, numberOfShips, armed) {
  console.log(
    `You sent ${numberOfShips} ${armed ? "armed" : ""} ships to ${destination}.`
  );
}

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "outDir": "dist"
  }
}
$ npx typescript-to-cli ./send-ships.js
./dist/send-ships.js CLI has been generated.

Limitations

  • Supports only boolean, number, string, string literals
  • No option shorcut