Skip to content

Commit c70e849

Browse files
committed
chore: update config & dependencies
1 parent da9b0b8 commit c70e849

File tree

12 files changed

+154
-216
lines changed

12 files changed

+154
-216
lines changed

microapp/config.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ module.exports = {
3838
plugins: [
3939
'@micro-app/plugin-deploy',
4040
],
41+
42+
options: {}, // 合并
4143
};

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757
"jest": "^24.9.0"
5858
},
5959
"dependencies": {
60-
"@micro-app/core": "^0.3.12",
61-
"@zkochan/cmd-shim": "^3.1.0",
62-
"read-cmd-shim": "^1.0.5",
63-
"update-notifier": "^3.0.1"
60+
"@micro-app/core": "^0.3.15",
61+
"@zkochan/cmd-shim": "^4.3.0",
62+
"read-cmd-shim": "^2.0.0",
63+
"update-notifier": "^4.1.0"
6464
},
6565
"engines": {
6666
"node": ">=8"

src/index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ const { _, logger, fs, tryRequire, path, yParser } = require('@micro-app/shared-
1010
const cmd = process.argv[2];
1111
const argv = yParser(process.argv.slice(3));
1212

13-
// create instance
14-
function createService(_argv) {
13+
const Service = require('@micro-app/core');
1514

16-
const Service = require('@micro-app/core');
15+
/**
16+
* create instance
17+
* @param {Object} _argv context
18+
* @return {Service} service instance
19+
*/
20+
function createService(_argv) {
1721

1822
const service = new Service(_argv || _.cloneDeep(argv));
1923

src/plugins/commands/bootstrap/index.js

+85-110
Original file line numberDiff line numberDiff line change
@@ -2,158 +2,133 @@
22

33
// TODO 需要整理目录结构,需要保留 package.json, 但是要更改内容
44

5-
const { Command } = require('@micro-app/core');
6-
7-
class BootstrapCommand extends Command {
8-
9-
initialize(api) {
10-
const { chalk, dedent } = require('@micro-app/shared-utils');
11-
12-
api.registerMethod('beforeCommandBootstrap', {
13-
type: api.API_TYPE.EVENT,
14-
description: 'bootstrap 前事件',
15-
});
16-
api.registerMethod('afterCommandBootstrap', {
17-
type: api.API_TYPE.EVENT,
18-
description: 'bootstrap 完毕后事件',
19-
});
20-
21-
// start
22-
api.registerCommand('bootstrap', {
23-
description: 'bootstrap micro app project.',
24-
usage: 'micro-app bootstrap [options]',
25-
options: {
26-
'-': 'bootstrap default.',
27-
// '-n <name>': 'only bootstrap <name>.',
28-
},
29-
details: dedent`
30-
Examples:
31-
${chalk.gray('# bootstrap')}
32-
micro-app bootstrap`,
33-
}, this.execute.bind(this));
34-
}
35-
36-
execute(args) {
37-
const { registry, npmClient = 'npm', npmClientArgs = [] } = this.options;
5+
module.exports = function BootstrapCommand(api, options = {}) {
6+
7+
const { chalk, dedent } = require('@micro-app/shared-utils');
8+
9+
api.registerMethod('beforeCommandBootstrap', {
10+
type: api.API_TYPE.EVENT,
11+
description: 'bootstrap 前事件',
12+
});
13+
api.registerMethod('afterCommandBootstrap', {
14+
type: api.API_TYPE.EVENT,
15+
description: 'bootstrap 完毕后事件',
16+
});
17+
18+
// start
19+
api.registerCommand('bootstrap', {
20+
description: 'bootstrap micro app project.',
21+
usage: 'micro-app bootstrap [options]',
22+
options: {
23+
'-': 'bootstrap default.',
24+
// '-n <name>': 'only bootstrap <name>.',
25+
},
26+
details: dedent`
27+
Examples:
28+
${chalk.gray('# bootstrap')}
29+
micro-app bootstrap`,
30+
}, args => {
31+
const { registry, npmClient = 'npm', npmClientArgs = [] } = options;
3832
const { force = false } = args;
39-
const api = this.api;
4033

4134
const spinner = api.logger.spinner('bootstrap...');
4235

36+
const npmConfig = {
37+
registry,
38+
npmClient,
39+
npmClientArgs,
40+
};
41+
4342
let chain = Promise.resolve();
4443

4544
chain = chain.then(() => {
4645
spinner.start();
4746
});
4847

49-
chain = chain.then(() => api.applyPluginHooks('beforeCommandBootstrap', { args, options: this.options }));
50-
51-
chain = chain.then(() => {
52-
this.npmConfig = {
53-
registry,
54-
npmClient,
55-
npmClientArgs,
56-
};
57-
});
48+
chain = chain.then(() => api.applyPluginHooks('beforeCommandBootstrap', { args, options, npmConfig }));
5849

5950
chain = chain.then(() => {
6051
if (npmClient === 'yarn') {
61-
this.npmConfig.npmClientArgs.unshift('--pure-lockfile');
52+
npmConfig.npmClientArgs.unshift('--pure-lockfile');
6253
} else {
63-
this.npmConfig.npmClientArgs.unshift('--no-save');
54+
npmConfig.npmClientArgs.unshift('--no-save');
6455
}
6556
});
6657

67-
chain = chain.then(() => this.initPackages());
68-
69-
chain = chain.then(() => this.initTempFiles());
70-
7158
// 判断 node_modules 是否存在
72-
chain = chain.then(() => this.initNodeModules({ force }));
59+
chain = chain.then(() => initNodeModules(api, { force, npmConfig }));
7360

74-
chain = chain.then(() => this.bootstrap({ force }));
61+
chain = chain.then(() => filterPackages(api));
7562

76-
chain = chain.then(() => this.initSymlinks());
63+
chain = chain.then(pkgs => bootstrap(api, { force, npmConfig, pkgs }));
7764

78-
chain = chain.then(() => api.applyPluginHooks('afterCommandBootstrap', { args, options: this.options }));
65+
chain = chain.then(pkgs => initSymlinks(api, { pkgs }));
66+
67+
chain = chain.then(() => api.applyPluginHooks('afterCommandBootstrap', { args, options }));
7968

8069
return chain.then(() => {
8170
spinner.succeed('finished!');
8271
}).catch(e => {
8372
spinner.fail(e.message);
8473
});
85-
}
8674

87-
initPackages() {
88-
const { _ } = require('@micro-app/shared-utils');
75+
});
76+
};
8977

90-
const api = this.api;
91-
const allPackages = api.packages;
9278

93-
if (_.isEmpty(allPackages)) {
94-
return;
95-
}
79+
function filterPackages(api) {
80+
const { _ } = require('@micro-app/shared-utils');
9681

97-
// init git
98-
const pkgs = allPackages.filter(item => {
99-
return item.pkgInfo;
100-
}).map(item => {
101-
return item.pkgInfo;
102-
});
82+
const allPackages = api.packages;
10383

104-
api.logger.debug('[bootstrap > initPackages]', pkgs.length);
105-
this.filteredPackages = pkgs;
84+
if (_.isEmpty(allPackages)) {
85+
return;
10686
}
10787

108-
initTempFiles() {
109-
const { _ } = require('@micro-app/shared-utils');
110-
const api = this.api;
111-
const allPackages = api.packages;
112-
if (_.isEmpty(allPackages)) {
113-
return;
114-
}
115-
this.tempDir = api.tempDir;
116-
}
88+
// init git
89+
const pkgs = allPackages.filter(item => {
90+
return item.pkgInfo;
91+
}).map(item => {
92+
return item.pkgInfo;
93+
});
11794

118-
initNodeModules() {
119-
const { fs } = require('@micro-app/shared-utils');
120-
const npmInstall = require('./npmInstall');
121-
const api = this.api;
122-
const currentNodeModules = api.nodeModulesPath;
123-
if (fs.pathExistsSync(currentNodeModules)) {
124-
api.logger.warn('[bootstrap]', 'skip root install!');
125-
return;
126-
}
127-
const root = api.root;
128-
return npmInstall(root, this.npmConfig);
129-
}
95+
api.logger.debug('[bootstrap > initPackages]', pkgs.length);
96+
return pkgs;
97+
}
13098

131-
bootstrap({ force }) {
132-
const { fs, path } = require('@micro-app/shared-utils');
133-
const npmInstall = require('./npmInstall');
134-
const api = this.api;
135-
if (!force) {
136-
if (fs.pathExistsSync(this.tempDir) && fs.pathExistsSync(path.join(this.tempDir, 'node_modles'))) {
137-
api.logger.warn('[bootstrap]', `${this.tempDir} is not empty!`);
138-
return;
139-
}
140-
}
141-
return npmInstall.micros(this.filteredPackages, this.tempDir, this.npmConfig);
99+
function initNodeModules(api, { npmConfig }) {
100+
const { fs } = require('@micro-app/shared-utils');
101+
const npmInstall = require('./npmInstall');
102+
const currentNodeModules = api.nodeModulesPath;
103+
if (fs.pathExistsSync(currentNodeModules)) {
104+
api.logger.warn('[bootstrap]', 'skip root install!');
105+
return;
142106
}
107+
const root = api.root;
108+
return npmInstall(root, npmConfig);
109+
}
143110

144-
initSymlinks() {
145-
const initSymlinks = require('./initSymlinks');
146-
const filteredPackages = this.filteredPackages;
147-
const api = this.api;
148-
149-
return initSymlinks(api, { filteredPackages }).then(() => {
150-
api.logger.debug('[bootstrap > initSymlinks]', 'finished');
151-
});
111+
function bootstrap(api, { force, npmConfig, pkgs }) {
112+
const { fs, path } = require('@micro-app/shared-utils');
113+
const npmInstall = require('./npmInstall');
114+
const tempDir = api.tempDir;
115+
fs.ensureDirSync(tempDir);
116+
if (!force) {
117+
if (fs.pathExistsSync(path.join(api.tempDir, 'node_modles'))) {
118+
api.logger.warn('[bootstrap]', `${tempDir} is not empty!`);
119+
return;
120+
}
152121
}
122+
return npmInstall.micros(pkgs, tempDir, npmConfig).then(() => pkgs);
153123
}
154124

125+
function initSymlinks(api, { pkgs }) {
126+
const initSymlinks = require('./initSymlinks');
155127

156-
module.exports = BootstrapCommand;
128+
return initSymlinks(api, { filteredPackages: pkgs }).then(() => {
129+
api.logger.debug('[bootstrap > initSymlinks]', 'finished');
130+
});
131+
}
157132

158133
module.exports.configuration = {
159134
description: '初始化命令行',

src/plugins/commands/bootstrap/initSymlinks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = function initSymlinks(api, { filteredPackages }) {
1111
return Promise.resolve();
1212
}
1313

14-
const tempDirPackageGraph = api.tempDirPackageGraph;
14+
const tempDirPackageGraph = api.getTempDirPackageGraph();
1515
let names = filteredPackages.map(item => item.name);
1616

1717
const dependencies = [];

src/plugins/commands/init/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ module.exports = function initCommand(api, opts) {
1717

1818

1919
module.exports.configuration = {
20-
description: '增加初始化命令行',
20+
description: '增强初始化命令行',
2121
};

src/plugins/extends/configSchema.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = {
4+
additionalProperties: true,
5+
properties: {
6+
server: {
7+
description: '服务端配置. ( object )',
8+
type: 'object',
9+
},
10+
},
11+
type: 'object',
12+
};

src/plugins/extends/server/index.js

+20-30
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,14 @@ module.exports = function extendServer(api, opts) {
55
const registerMethods = require('./methods');
66
registerMethods(api);
77

8-
const { _, smartMerge, yParser } = require('@micro-app/shared-utils');
8+
const { _, smartMerge } = require('@micro-app/shared-utils');
99

1010
const logger = api.logger;
1111

12-
// 环境变量参数引用(动态)
13-
let tempArgvs = {};
14-
api.onRunCommand(({ args = {} }) => {
15-
tempArgvs = args;
16-
});
17-
1812
api.extendMethod('parseArgv', {
1913
description: 'resolve parse command argv.',
2014
}, function() {
21-
const argv = yParser(process.argv.slice(2)) || {};
22-
return smartMerge({}, argv, tempArgvs);
15+
return _.cloneDeep(api.context);
2316
});
2417

2518
api.extendConfig('selfServerConfig', {
@@ -31,7 +24,7 @@ module.exports = function extendServer(api, opts) {
3124
const _serverConfig = _originalConfig.server || {};
3225
return {
3326
..._serverConfig,
34-
info: _.cloneDeep(microConfig),
27+
info: microConfig.toJSON(),
3528
shared: microConfig.shared,
3629
sharedObj: microConfig.sharedObj,
3730
resolveShared: microConfig.resolveShared,
@@ -66,31 +59,28 @@ module.exports = function extendServer(api, opts) {
6659
return config;
6760
});
6861

62+
// ZAP: 需要优化时机 和 内容
63+
// merge server config
6964
api.extendConfig('serverConfig', {
65+
cache: true,
7066
description: '当前工程下服务端配置集合',
7167
}, function() {
72-
return api.applyPluginHooks('modifyDefaultServerConfig', {});
68+
const selfServerConfig = api.selfServerConfig;
69+
const microsServerConfig = api.microsServerConfig;
70+
const micros = api.micros;
71+
// 组装 server 配置
72+
const mergeConfig = smartMerge(...micros.map(key => {
73+
const _msc = microsServerConfig[key];
74+
if (!_msc) return {};
75+
return _.pick(_msc, [
76+
'shared',
77+
'sharedObj',
78+
'resolveShared',
79+
]);
80+
}), selfServerConfig);
81+
return Object.assign({}, mergeConfig);
7382
});
7483

75-
// merge server config
76-
api.onInitWillDone(() => {
77-
api.modifyDefaultServerConfig(_serverConfig => {
78-
const selfServerConfig = api.selfServerConfig;
79-
const microsServerConfig = api.microsServerConfig;
80-
const micros = api.micros;
81-
// 组装 server 配置
82-
const mergeConfig = smartMerge(...micros.map(key => {
83-
const _msc = microsServerConfig[key];
84-
if (!_msc) return {};
85-
return _.pick(_msc, [
86-
'shared',
87-
'sharedObj',
88-
'resolveShared',
89-
]);
90-
}), selfServerConfig);
91-
return Object.assign(_serverConfig, mergeConfig);
92-
});
93-
});
9484
};
9585

9686

0 commit comments

Comments
 (0)