Library to execute command line tasks in nodejs and typescript
# with npm
npm install shell-ts
# with yarn
yarn add shell-ts
Read Typescript starter project
Minimal shell script
import { Shell, ShellInterface } from "shell-ts";
export default class YoloShell extends Shell implements ShellInterface {
public execute(): void {
console.log('I run my yolo script')
}
}
Shell.ts
is the abstract class that you need to extend.
List the options that are required. If an option is missing a MissingOptionException
is thrown.
protected requiredOptions: string[] = [];
Usage :
protected requiredOptions: string[] = ["id"];
// mycommand --id 3
// --- OK
// mycommand
// --- Exception
constructor({ args, options, config }: ParamsI)
-
args
: list of arguments of your command (string[]
) see the starter -
options
: options of your command (object
) see the starter -
config
: config of your project (object
) see the starter
put your script code here
List all arguments of your command
public getArguments(): string[]
List all options of your command
public getOptions(): OptionsI
Get an option by key
public getOption(key: string): string | undefined
Get all config object
public getConfigs(): ConfigI | undefined
Get a config by key
public getConfig(key: string): string | number | boolean | ConfigI | undefined
Usages :
/* config = {
db: {
user: "root"
},
debug: true
} */
this.getConfig('debug'); // true
this.getConfig('db.user'); // "root"
Check if an option is missing in your command
public isMissingOption(key: string): boolean
Usages :
// mycommand --id 3
this.isMissingOption('id'); // false
this.isMissingOption('unknow'); // true
Log message only if verbose option passed in your command
protected verbose(message: string): void
Clone repository and run npm install
.
Run tests with command npm run test
Commit your modifications with commitizen : npm run commit