-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.d.ts
188 lines (163 loc) · 5.54 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Arguments, Argv, Options } from 'yargs';
import { ForkOptions, SpawnOptions } from 'child_process';
import * as dargs from 'dargs';
interface PlainObject {
[key: string]: any;
}
// migrating to common-bin later
declare class CommonBin {
usage: string;
version: string;
/**
* original argument
* @type {Array}
*/
rawArgv: string[];
/**
* yargs
* @type {Object}
*/
yargs: Argv;
/**
* helper function
* @type {Object}
*/
helper: CommonBin.Helper;
/**
* parserOptions
* @type {Object}
* @property {Boolean} execArgv - whether extract `execArgv` to `context.execArgv`
* @property {Boolean} removeAlias - whether remove alias key from `argv`
* @property {Boolean} removeCamelCase - whether remove camel case key from `argv`
*/
parserOptions: {
execArgv: boolean;
removeAlias: boolean;
removeCamelCase: boolean;
};
/**
* getter of context, default behavior is remove `help` / `h` / `version`
* @return {Object} context - { cwd, env, argv, rawArgv }
* @protected
*/
protected context: CommonBin.Context;
constructor(rawArgv?: string[]);
/**
* shortcut for yargs.options
* @param {Object} opt - an object set to `yargs.options`
*/
set options(opt: { [key: string]: Options });
/**
* command handler, could be generator / async function / normal function which return promise
* @param {Object} context - context object
* @param {String} context.cwd - process.cwd()
* @param {Object} context.argv - argv parse result by yargs, `{ _: [ 'start' ], '$0': '/usr/local/bin/common-bin', baseDir: 'simple'}`
* @param {Array} context.rawArgv - the raw argv, `[ "--baseDir=simple" ]`
* @protected
*/
protected run(context?: CommonBin.Context): any;
/**
* load sub commands
* @param {String} fullPath - the command directory
* @example `load(path.join(__dirname, 'command'))`
*/
load(fullPath: string): void;
/**
* add sub command
* @param {String} name - a command name
* @param {String|Class} target - special file path (must contains ext) or Command Class
* @example `add('test', path.join(__dirname, 'test_command.js'))`
*/
add(name: string, target: string | typeof CommonBin): void;
/**
* alias an existing command
* @param {String} alias - alias command
* @param {String} name - exist command
*/
alias(alias: string, name: string): void;
/**
* start point of bin process
*/
start(): void;
/**
* default error hander
* @param {Error} err - error object
* @protected
*/
protected errorHandler(err: Error): void;
/**
* print help message to console
* @param {String} [level=log] - console level
*/
showHelp(level?: string): void;
}
declare namespace CommonBin {
export interface Helper {
/**
* fork child process, wrap with promise and gracefull exit
* @method helper#forkNode
* @param {String} modulePath - bin path
* @param {Array} [args] - arguments
* @param {Object} [options] - options
* @return {Promise} err or undefined
* @see https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options
*/
forkNode(modulePath: string, args?: string[], options?: ForkOptions): Promise<void>;
/**
* spawn a new process, wrap with promise and gracefull exit
* @method helper#forkNode
* @param {String} cmd - command
* @param {Array} [args] - arguments
* @param {Object} [options] - options
* @return {Promise} err or undefined
* @see https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
*/
spawn(cmd: string, args?: string[], options?: SpawnOptions): Promise<void>;
/**
* exec npm install
* @method helper#npmInstall
* @param {String} npmCli - npm cli, such as `npm` / `cnpm` / `npminstall`
* @param {String} name - node module name
* @param {String} cwd - target directory
* @return {Promise} err or undefined
*/
npmInstall(npmCli: string, name: string, cwd?: string): Promise<void>;
/**
* call fn
* @method helper#callFn
* @param {Function} fn - support generator / async / normal function return promise
* @param {Array} [args] - fn args
* @param {Object} [thisArg] - this
* @return {Object} result
*/
callFn<T = any, U extends any[] = any[]>(fn: (...args: U) => IterableIterator<T> | Promise<T> | T, args?: U, thisArg?: any): IterableIterator<T>;
/**
* unparse argv and change it to array style
* @method helper#unparseArgv
* @param {Object} argv - yargs style
* @param {Object} [options] - options, see more at https://github.com/sindresorhus/dargs
* @param {Array} [options.includes] - keys or regex of keys to include
* @param {Array} [options.excludes] - keys or regex of keys to exclude
* @return {Array} [ '--debug=7000', '--debug-brk' ]
*/
unparseArgv(argv: object, options?: dargs.Options): string[];
/**
* extract execArgv from argv
* @method helper#extractExecArgv
* @param {Object} argv - yargs style
* @return {Object} { debugPort, debugOptions: {}, execArgvObj: {} }
*/
extractExecArgv(argv: object): { debugPort?: number; debugOptions?: PlainObject; execArgvObj: PlainObject };
}
export interface Context extends PlainObject {
cwd: string;
rawArgv: string[];
env: PlainObject;
argv: Arguments<PlainObject>;
execArgvObj: PlainObject;
readonly execArgv: string[];
debugPort?: number;
debugOptions?: PlainObject;
}
}
export = CommonBin;