command.js 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Command = void 0;
  4. const tslib_1 = require("tslib");
  5. const path_1 = tslib_1.__importDefault(require("path"));
  6. const commander_1 = require("commander");
  7. Object.defineProperty(exports, "Command", { enumerable: true, get: function () { return commander_1.Command; } });
  8. const assert_1 = tslib_1.__importDefault(require("assert"));
  9. const package_json_1 = tslib_1.__importDefault(require("../package.json"));
  10. const core_1 = tslib_1.__importStar(require("./core"));
  11. const utils_1 = require("./utils");
  12. class Commander {
  13. constructor(options) {
  14. this.commands = (0, utils_1.loadModules)(path_1.default.resolve(__dirname, './cmd'), true);
  15. this.tsHelperClazz = (options === null || options === void 0 ? void 0 : options.tsHelperClazz) || core_1.default;
  16. this.program = new commander_1.Command()
  17. .version((options === null || options === void 0 ? void 0 : options.version) || package_json_1.default.version, '-v, --version')
  18. .usage('[commands] [options]')
  19. .option('-w, --watch', 'Watching files, d.ts would recreated while file changed')
  20. .option('-c, --cwd [path]', 'Egg application base dir (default: process.cwd)')
  21. .option('-C, --config [path]', 'Configuration file, The argument can be a file path to a valid JSON/JS configuration file.(default: {cwd}/tshelper.js')
  22. .option('-f, --framework [name]', 'Egg framework(default: egg)')
  23. .option('-o, --oneForAll [path]', 'Create a d.ts import all types (default: typings/ets.d.ts)')
  24. .option('-s, --silent', 'Running without output')
  25. .option('-i, --ignore [dirs]', 'Ignore watchDirs, your can ignore multiple dirs with comma like: -i controller,service')
  26. .option('-e, --enabled [dirs]', 'Enable watchDirs, your can enable multiple dirs with comma like: -e proxy,other')
  27. .option('-E, --extra [json]', 'Extra config, the value should be json string');
  28. }
  29. init(argv) {
  30. const { program, commands } = this;
  31. let executeCmd;
  32. // override executeSubCommand to support async subcommand.
  33. program.addImplicitHelpCommand = () => { };
  34. program.executeSubCommand = async function (argv, args, unknown) {
  35. const cwd = this.cwd || core_1.defaultConfig.cwd;
  36. const command = commands[executeCmd];
  37. (0, assert_1.default)(command, executeCmd + ' does not exist');
  38. await command.run(this, { cwd, argv, args: args.filter(item => item !== this), unknown });
  39. };
  40. if (!argv.slice(2).length) {
  41. this.execute();
  42. }
  43. else {
  44. Object.keys(commands).forEach(cmd => {
  45. const subCommand = commands[cmd];
  46. const cmdName = subCommand.options ? `${cmd} ${subCommand.options}` : cmd;
  47. program.command(cmdName, subCommand.description)
  48. .action(command => executeCmd = command);
  49. });
  50. program.parse(argv);
  51. if (!executeCmd) {
  52. this.execute();
  53. }
  54. }
  55. }
  56. execute() {
  57. const { program } = this;
  58. const watchFiles = program.watch;
  59. const generatorConfig = {};
  60. program.ignore && program.ignore.split(',').forEach(key => (generatorConfig[key] = false));
  61. program.enabled && program.enabled.split(',').forEach(key => (generatorConfig[key] = true));
  62. const tsHelperConfig = Object.assign({ cwd: program.cwd || core_1.defaultConfig.cwd, framework: program.framework, watch: watchFiles, generatorConfig, configFile: program.config }, (program.extra ? JSON.parse(program.extra) : {}));
  63. // silent
  64. if (program.silent) {
  65. tsHelperConfig.silent = true;
  66. }
  67. if ((0, utils_1.checkMaybeIsJsProj)(tsHelperConfig.cwd)) {
  68. // write jsconfig if the project is wrote by js
  69. (0, utils_1.writeJsConfig)(tsHelperConfig.cwd);
  70. }
  71. // create instance
  72. const clazz = this.tsHelperClazz;
  73. const tsHelper = new clazz(tsHelperConfig).build();
  74. if (program.oneForAll) {
  75. // create one for all
  76. tsHelper.createOneForAll(program.oneForAll);
  77. }
  78. }
  79. }
  80. exports.default = Commander;