0.0.1 • Published 4 years ago

ts-cli-creator v0.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

ts-cli-creator

Yet another cli generator based TypeScript code

npm i -g ts-cli-creator

Usage

ts-cli-creator <entry>

Given the entry file:

export interface Options {
    foo: string
}

function command(param: string, options: Options): void {
    /* your code here */
}

export default command

Will generate:

import * as yargs from 'yargs'
import handler from './handler'

export default async function main(): Promise<void> {
    yargs
        .strict()
        .command(
            '$0 <param> [...options]', '', 
            yargs => {
                return yargs
                    .positional('param', { type: string, demandOption: "true" })
                    .option('foo', { type: string })
            },
            args => {
                const { _, $0, param, ...options } = args
                handler(param, options)
            })
        .help()
        .alias('help', 'h')
        .argv
}

Generator

Genreate command

ts-cli-creator generate a yargs commander from function declaration.

1. Transform function parameters to commander required positional arguments

function command(foo: string, bar?: number) {}

Transform to:

yargs.command(`$0 <foo> <bar>`, ``, 
    yargs => {
        return yargs
            .positional(`foo`, { type: string, demandOption: "true" })
            .positional(`bar`, { type: number })
    },
    args => {
        const { _, $0, foo, bar } = args
        handler(foo, bar)
    }
)

Supports positional argument types:

Typescript TypesYargs Positional Options
string{ type: 'string' }
number{ type: 'number' }
boolean{ type: 'boolean' }
enum E(string iterial only){ choices: E[] }

2. Transform JSDoc parameter description to commander description

/**
 * Description for commander
 */
function command() {}

Transform to:

yargs.command(`$0`, `Description for command`)

Generate options

When the name of function last param matched /^options?$/, and type was interface declaration. like:

interface Options {}
function command(param: string, options: Options) {}

ts-cli-creator will generate yargs options for you:

1. Transform interface properties to commander options

interface Options {
    foo: string
}

Transform to:

yargs.option(`foo`, { type: `string` })

Supports options types:

Typescript TypesYargs Options
string{ type: 'string' }
number{ type: 'number' }
boolean{ type: 'boolean' }
string[]{ type: 'string', array: true }
number[]{ type: 'number', array: true }
boolean[]{ type: 'boolean', array: true }
enum E(string iterial only){ choices: E[] }

2. Transform JSDoc interface properties comments to options description

interface Options {
    /** A description for foo */
    foo: string
}

Transform to:

.option('foo', {
    type: 'string',
    description: 'A description for foo'
})

3. Transform JSDoc custom tags @alias, @default, @demandOption to options properties

interface Options {
    /** 
     * @default 42
     * @demandOption
     */
    foo: number,
    /**
     * @alias b
     * @default 'baz'
     */
    bar: string
}

Transform to:

.option(`foo`, {
    type: `number`,
    default: 42,
    demandOption: true
})
.option(`bar`, {
    type: `string`,
    alias: `b`,
    default: `baz`
})

Supports options properties:

JSDoc tagYargs option
@aliasalias
@defaultdefault
@demandOptiondemandOption
@requiredemandOption
@requireddemandOption

Cli usage

output content to terminal

ts-cli-creator ./src/handler.ts

write to file

ts-cli-creator ./src/handler.ts -o ./cli.ts

Generate file to ./src/cli.ts. The output path relative entry directory path when not use absolute path.

read data from pipe

cat ./src/handler.ts | ts-cli-creator

or

echo function add(a:number,b:number){} | ts-cli-creator

Warning. this mode will inline the code to output content replace require the entry module

preview cli

You can preview cli message via --runnable option and pass raw arguments:

ts-cli-creator ./src/handler.ts --no-color --runnable --js -- -h | node

Or use ts-node:

ts-cli-creator ./src/handler.ts --no-color --runnable  -- -h | ts-node -T --skip-project ./cli.ts

See the simple example:

echo function add(a:number,b:number){console.log(a+b)} | ts-cli-creator --no-color --js -- 1 2

## will output 3

 

Preview

 

Cli Options

NameDescriptionTypeDefault
--output, -oOutput file path, output to stdout when not setstringundefined
--jsGenerate js file, base on your tsconfigbooleanfalse
--jsonOutput json databooleanfalse
--colorColourful output with write to stdoutbooleantrue
--verboseOutput full infomationsbooleanfalse
--function-nameGenerate Wrapper function namestringcli
--async-functionUse async functionbooleanfalse
--runnableAdd main function call at last, default to falsebooleanfalse
--strictenable strict modebooleantrue
--helperglobal helper options to show helper messagesbooleantrue
--helper-aliashelper options short for 'h'booleantrue
--versionglobal version options, show current versionbooleantrue

TODOS

  • Sub commander
  • Custom type parser
  • Other cli provider, like commander