UNPKG

3.85 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __exportStar = (this && this.__exportStar) || function(m, exports) {
10 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.CLI = void 0;
14const command_core_1 = require("@midwayjs/command-core");
15const child_process_1 = require("child_process");
16const plugins_1 = require("./plugins");
17__exportStar(require("./utils"), exports);
18const enquirer = require('enquirer');
19class CLI extends command_core_1.CoreBaseCLI {
20 async loadDefaultPlugin() {
21 const command = this.commands && this.commands[0];
22 // // version not load plugin
23 if (this.argv.v || this.argv.version) {
24 return;
25 }
26 await super.loadDefaultPlugin();
27 let needLoad = plugins_1.PluginList;
28 const req = this.argv.require || require;
29 if (!this.argv.h && command) {
30 needLoad = command_core_1.filterPluginByCommand(plugins_1.PluginList, {
31 command,
32 cwd: this.core.cwd,
33 load: name => req(name),
34 });
35 }
36 if (this.argv.isFaaS) {
37 delete this.argv.isFaaS;
38 needLoad.push({ mod: '@midwayjs/cli-plugin-faas', name: 'FaaSPlugin' });
39 }
40 this.debug('Plugin load list', needLoad);
41 const allPluginClass = await command_core_1.getPluginClass(needLoad, {
42 cwd: this.core.cwd,
43 load: name => req(name),
44 npm: this.argv.npm,
45 notAutoInstall: this.argv.h,
46 });
47 allPluginClass.forEach(pluginClass => {
48 this.core.addPlugin(pluginClass);
49 });
50 }
51 // cli 扩展
52 loadExtensions() {
53 return {
54 debug: this.debug.bind(this),
55 enquirer,
56 };
57 }
58 error(err) {
59 if (err && err.message) {
60 console.log(err.message);
61 throw err;
62 }
63 else {
64 console.log(err);
65 throw new Error(err);
66 }
67 }
68 async loadPlugins() {
69 this.debug('command & options', this.argv);
70 await super.loadPlugins();
71 await this.loadDefaultOptions();
72 }
73 async loadDefaultOptions() {
74 if (this.commands.length) {
75 return;
76 }
77 if (this.argv.v || this.argv.version) {
78 this.displayVersion();
79 }
80 else {
81 // 默认没有command的时候展示帮助
82 this.argv.h = true;
83 }
84 }
85 debug(...args) {
86 if (!this.argv.V && !this.argv.verbose && !process.env.FAAS_CLI_VERBOSE) {
87 return;
88 }
89 const log = this.loadLog();
90 log.log('[Verbose] ', ...args);
91 }
92 displayVersion() {
93 const log = this.loadLog();
94 try {
95 const nodeVersion = child_process_1.execSync('node -v').toString().replace('\n', '');
96 log.log('Node.js'.padEnd(20) + nodeVersion);
97 }
98 catch (_a) {
99 //
100 }
101 try {
102 // midway-faas version
103 const cliVersion = require('../package.json').version;
104 log.log('@midwayjs/cli'.padEnd(20) + `v${cliVersion}`);
105 }
106 catch (_b) {
107 //
108 }
109 }
110 displayUsage(commandsArray, usage, coreInstance, commandInfo) {
111 this.displayVersion();
112 super.displayUsage(commandsArray, usage, coreInstance, commandInfo);
113 }
114}
115exports.CLI = CLI;
116//# sourceMappingURL=index.js.map
\No newline at end of file