index.d.ts 5.5 KB

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