Skip to content

Commit d0cef7b

Browse files
committed
feat: 增加配置可通过对象 registerCommand 进行注册
1 parent 1a0e77b commit d0cef7b

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/core/Constants/SharedProps.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const MethodServiceKeys = [
2727
'microsExtraConfig',
2828
'parseConfig',
2929
// method
30-
'registerCommand',
3130
'changeCommandOption',
3231
// new v0.3
3332
'resolve',
@@ -44,6 +43,7 @@ const MethodServiceKeys = [
4443

4544
// 关于注册的方法
4645
const registerKeys = [
46+
'registerCommand',
4747
'registerMethod',
4848
'extendConfig',
4949
'extendMethod',

src/core/Service/libs/MethodService.js

+8
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ class MethodService extends BaseService {
224224
return this.resolve(this.tempDir, ..._paths);
225225
}
226226

227+
// 断言是否存在
227228
assertExtendOptions(name, opts, fn) {
228229
assert(typeof name === 'string', 'name must be string.');
229230
assert(name || /^_/i.test(name), `${name} cannot begin with '_'.`);
@@ -249,6 +250,13 @@ class MethodService extends BaseService {
249250
opts = null;
250251
}
251252
opts = opts || {};
253+
if (typeof fn !== 'function') {
254+
if (typeof opts.fn === 'function') {
255+
fn = opts.fn;
256+
delete opts.fn;
257+
}
258+
}
259+
assert(typeof fn === 'function', `registerCommand ${name} params Error!!! please check 'fn'.`);
252260
this.commands[name] = { fn, opts };
253261
logger.debug('[Plugin]', `registerCommand( ${name} ); Success!`);
254262
}

src/plugins/commands/init/index.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module.exports = function initCommand(api, opts) {
44

55
const { _, chalk, fs, prompt, smartMerge, path } = require('@micro-app/shared-utils');
6+
const logger = api.logger;
67

78
const details = `
89
Examples:
@@ -23,15 +24,16 @@ Examples:
2324

2425
// default config
2526
api.otherCommandInit(async ({ args }) => {
26-
if (args.name) return {}; // default, name is empty!!!
27+
if (args.name) {
28+
logger.info('[Init]', `initialize ${chalk.yellow(args.name)} config`);
29+
return {}; // default, name is empty!!!
30+
}
2731
// must be return info
2832
return defaultInit(args);
2933
});
3034

3135
// start
3236
api.registerCommand('init', cmdOpt, args => {
33-
const logger = api.logger;
34-
3537
let chain = Promise.resolve();
3638

3739
chain = chain.then(() => {
@@ -59,7 +61,6 @@ Examples:
5961
});
6062

6163
function checkInitConfig(args) {
62-
const logger = api.logger;
6364
const configDir = api.configDir;
6465

6566
const filename = (args.name || 'index');
@@ -84,7 +85,6 @@ Examples:
8485
}
8586

8687
function saveInitConfig(args, info) {
87-
const logger = api.logger;
8888
const configDir = api.configDir;
8989

9090
let chain = Promise.resolve(info);
@@ -144,7 +144,6 @@ ${Object.entries(finalInfo).map(([ key, vlaue ]) => {
144144
}
145145

146146
function defaultInit(args) {
147-
const logger = api.logger;
148147
const pkg = api.pkg;
149148

150149
const info = {};

0 commit comments

Comments
 (0)