Skip to content

Commit bca46d5

Browse files
authored
Merge pull request #2 from zyao89/develop
🐛 fixed show env.
2 parents df44e81 + 7f62134 commit bca46d5

File tree

5 files changed

+218
-20
lines changed

5 files changed

+218
-20
lines changed

README.md

Lines changed: 190 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
# Micro APP
1+
# Micro APP CLI
2+
3+
Pluggable application framework.
4+
5+
[![NPM Version][npm-img]][npm-url]
6+
[![NPM Download][download-img]][download-url]
7+
8+
[npm-img]: https://img.shields.io/npm/v/@micro-app/cli.svg?style=flat-square
9+
[npm-url]: https://npmjs.org/package/@micro-app/cli
10+
[download-img]: https://img.shields.io/npm/dm/@micro-app/cli.svg?style=flat-square
11+
[download-url]: https://npmjs.org/package/@micro-app/cli
212

313
## Install
414

@@ -35,11 +45,13 @@ module.exports = {
3545
// },
3646
},
3747

38-
entry: {
48+
staticPath: [], // String | Array
49+
50+
entry: { // 入口
3951
main: './test/index.js',
4052
},
4153

42-
htmls: [
54+
htmls: [ // 输出模版配置
4355
{
4456
template: './test/index.js',
4557
},
@@ -51,7 +63,7 @@ module.exports = {
5163
// },
5264
// ],
5365

54-
alias: { // 前端
66+
alias: { // 别名配置
5567
api: '',
5668
config: {
5769
link: '',
@@ -64,7 +76,7 @@ module.exports = {
6476
},
6577
},
6678

67-
strict: true,
79+
strict: true, // 严格强依赖模式
6880

6981
micros: [ 'test' ], // 被注册的容器
7082
// micros$$test: { // 单独配置
@@ -82,7 +94,7 @@ module.exports = {
8294
},
8395
},
8496

85-
plugins: [
97+
plugins: [ // 自定义插件
8698
// [{
8799
// id: 'test',
88100
// description: '这是test',
@@ -156,22 +168,191 @@ const api = require('@micro-demo/api');
156168

157169
## Plugins 扩展
158170

171+
### 首先在 micro-app.config.js 中注册插件
172+
173+
```js
174+
plugins: [
175+
[ // 1
176+
{
177+
id: 'test', // 插件 id
178+
description: '这是test', // 插件描述
179+
link: __dirname + '/test/testPlugin.js', // 插件地址
180+
}, { // 注册入的 opts
181+
a: 1,
182+
}
183+
],
184+
],
185+
```
186+
187+
### 插件文件 `testPlugin.js`
188+
189+
文件必须返回一个方法.
190+
191+
```js
192+
module.exports = function(api, opts) {
193+
console.log(opts);
194+
api.onInitDone(item => {
195+
console.log('init Done', item);
196+
});
197+
api.onInitDone(() => {
198+
console.log('init Done2', api.getState('webpackConfig'));
199+
});
200+
api.onPluginInitDone(item => {
201+
console.log('onPluginInitDone', item);
202+
});
203+
api.beforeMergeWebpackConfig(item => {
204+
console.log('beforeMergeWebpackConfig', item);
205+
});
206+
api.afterMergeWebpackConfig(item => {
207+
console.log('afterMergeWebpackConfig', item);
208+
});
209+
// api.onChainWebpcakConfig(webpackChainConfig => {
210+
// console.log('onChainWebpcakConfig', webpackChainConfig);
211+
// });
212+
};
213+
```
214+
215+
### 内置部分插件提供的 api 方法
216+
217+
可通过如下命令进行动态查看
218+
219+
```js
220+
npx micro-app show methods
221+
```
222+
223+
以提供的方法如下, `System Build-in` 为内置方法
224+
225+
```js
226+
╰─➤ npx micro-app show methods
227+
Plugin Methods:
228+
* onPluginInitDone ( System Build-in )
229+
* beforeMergeConfig ( System Build-in )
230+
* afterMergeConfig ( System Build-in )
231+
* beforeMergeServerConfig ( System Build-in )
232+
* afterMergeServerConfig ( System Build-in )
233+
* onInitWillDone ( System Build-in )
234+
* onInitDone ( System Build-in )
235+
* modifyCommand ( System Build-in )
236+
* onRunCommand ( System Build-in )
237+
* modifyCommandHelp ( System Build-in )
238+
* beforeMergeWebpackConfig ( 合并 webpack 配置之前事件 )
239+
* afterMergeWebpackConfig ( 合并 webpack 配置之后事件 )
240+
* modifyChainWebpcakConfig ( 合并之后提供 webpack-chain 进行再次修改事件 )
241+
* onChainWebpcakConfig ( 修改之后提供 webpack-chain 进行查看事件 )
242+
* onServerInit ( 服务初始化时事件 )
243+
* onServerInitDone ( 服务初始化完成事件 )
244+
* onServerRunSuccess ( 服务运行启动成功时事件 )
245+
* onServerRunFail ( 服务运行启动失败时事件 )
246+
* beforeServerEntry ( 服务进入业务逻辑前事件 )
247+
* afterServerEntry ( 服务从业务逻辑出来后事件 )
248+
* modifyWebpackCompiler ( 对服务启动前对 webpack compiler 进行修改, 需要返回所有参数 )
249+
* beforeDevServer ( 开发服务创建前事件 )
250+
* afterDevServer ( 开发服务创建后事件 )
251+
* onBuildSuccess ( 构建成功时事件 )
252+
* onBuildFail ( 构建失败时事件 )
253+
* beforeBuild ( 开始构建前事件 )
254+
* afterBuild ( 构建结束后事件 )
255+
* beforeCommandUpdate ( 开始更新前事件 )
256+
* afterCommandUpdate ( 更新完毕后事件 )
257+
* beforeCommandDeploy ( 发布前事件 )
258+
* afterCommandDeploy ( 发布后事件 )
259+
* modifyCommandDeployMessage ( 发布消息二次编辑事件 )
260+
261+
// 以下为针对 vusion 类型的方法
262+
* modifyVusionConfig ( 对服务启动前对 vusion config 进行修改, 需要返回所有参数 )
263+
* modifyVusionWebpackConfig ( 对服务启动前对 vusion webpackConfig 进行修改, 需要返回所有参数 )
264+
* modifyDefaultVusionConfig ( 初始化修改通用 vusion.config.js, 需要返回所有参数 )
265+
```
266+
267+
### api 方法扩展
268+
269+
可参考以下插件, 如:
270+
271+
```js
272+
// 注册一个方法
273+
api.registerMethod('beforeCommandUpdate', {
274+
type: api.API_TYPE.EVENT,
275+
description: '开始更新前事件',
276+
});
277+
278+
// 注册一个终端命令
279+
api.registerCommand('update', {
280+
description: 'update package.json',
281+
usage: 'micro-app update [options]',
282+
options: {
283+
'-': 'update all.',
284+
'-n <name>': 'only update <name>.',
285+
},
286+
details: `
287+
Examples:
288+
${chalk.gray('# update all')}
289+
micro-app update
290+
${chalk.gray('# only update <name>')}
291+
micro-app update -n <name>
292+
`.trim(),
293+
}, args => {
294+
const name = args.n;
295+
return updateMicro(api, name);
296+
});
297+
298+
// 对外抛出已注册的方法.
299+
api.applyPluginHooks('beforeCommandUpdate', { name, logger, microsConfig });
300+
301+
```
302+
303+
其它插件使用 `beforeCommandUpdate` 方法, 如下:
304+
305+
```js
306+
api.beforeCommandUpdate(item => {
307+
console.log('beforeCommandUpdate', item);
308+
});
309+
```
310+
159311
## 其他
160312

161-
- 展示所有容器
313+
### 已支持的终端命令行
314+
315+
```js
316+
╰─➤ npx micro-app help
317+
318+
319+
Usage: micro-app <command> [options]
320+
321+
322+
Commands:
323+
* show ( show alias & shared list, etc. )
324+
* check ( check all dependencies. )
325+
* version ( show version )
326+
* start ( runs server for production )
327+
* serve ( runs server for development )
328+
* build ( build for production )
329+
* update ( update package.json )
330+
* deploy ( sync commit status. )
331+
332+
333+
run micro-app help [command] for usage of a specific command.
334+
```
335+
336+
### 展示所有容器
162337

163338
```js
164339
npx micro-app show micros
165340
```
166341

167-
- 展示所有前端共享接口
342+
### 展示所有前端共享接口
168343

169344
```js
170345
npx micro-app show alias
171346
```
172347

173-
- 展示所有后端共享接口
348+
### 展示所有全局共享接口
174349

175350
```js
176351
npx micro-app show shared
177352
```
353+
354+
### 启动开发模式
355+
356+
```js
357+
npx micro-app-dev --progress
358+
```

bin/base.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ const yParser = require('yargs-parser');
1010
const cmd = process.argv[2];
1111
const argv = yParser(process.argv.slice(3));
1212

13+
// 全局环境
14+
if ([ 'start', 'build' ].includes(cmd)) {
15+
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
16+
} else if ([ 'serve' ].includes(cmd)) {
17+
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
18+
}
19+
1320
// 全局指令
1421
if (!global.MicroAppConfig) {
1522
global.MicroAppConfig = {};

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/cli",
3-
"version": "0.1.0-RC7",
3+
"version": "0.1.0",
44
"description": "micro app cli",
55
"bin": {
66
"micro-app": "./bin/micro-app.js",
@@ -11,6 +11,14 @@
1111
"scripts": {
1212
"test": "echo \"Error: no test specified\" && exit 1"
1313
},
14+
"homepage": "https://github.com/zyao89/MicroApp-CLI",
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/zyao89/MicroApp-CLI.git"
18+
},
19+
"bugs": {
20+
"url": "https://github.com/zyao89/MicroApp-CLI/issues"
21+
},
1422
"files": [
1523
"bin",
1624
"plugins",
@@ -34,7 +42,7 @@
3442
"webpack": "^4.39.2"
3543
},
3644
"dependencies": {
37-
"@micro-app/core": "^0.1.0-RC4",
45+
"@micro-app/core": "^0.1.0",
3846
"koa": "^2.8.1",
3947
"koa-static": "^5.0.0",
4048
"opn": "^5.5.0",

src/utils/checker.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ exports.checkUpgrade = function() {
1919

2020
exports.checkNode = function() {
2121
// Ensure minimum supported node version is used
22-
const result = semver.satisfies(process.version, pkg.engines.node);
23-
24-
!result && logger.error(` You must upgrade node to ${pkg.engines.node} to use ${pkg.name}`);
25-
26-
return result;
22+
try {
23+
const result = semver.satisfies(process.version, pkg.engines.node);
24+
!result && logger.error(` You must upgrade node to ${pkg.engines.node} to use ${pkg.name}`);
25+
return result;
26+
} catch (error) {
27+
return true;
28+
}
2729
};

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@
182182
lodash "^4.17.13"
183183
to-fast-properties "^2.0.0"
184184

185-
"@micro-app/core@^0.1.0-RC4":
186-
version "0.1.0-RC4"
187-
resolved "https://registry.yarnpkg.com/@micro-app/core/-/core-0.1.0-RC4.tgz#d391e0172ac597b2e07788e46e7a22c2374eed1b"
188-
integrity sha512-XGeWdS+fnypBpQODHO8Aj0cZoZkirYg1I+K1IXZWvNf3ClXZzQKupTITjLBqhL35vkVKEJJJIlooEvTF47r6hQ==
185+
"@micro-app/core@^0.1.0":
186+
version "0.1.0"
187+
resolved "https://registry.yarnpkg.com/@micro-app/core/-/core-0.1.0.tgz#6f59e395817c7aca666b128e764d82de83b6baaf"
188+
integrity sha512-spS0B1ok6LuL4d6Lb+MGOc9X4XMsqPFK7P+ZHv/iRc0XNXEDck8nNcEy/1Cwf/MUbjKofDKRMM41KXctpipnCQ==
189189
dependencies:
190190
ajv "^6.10.2"
191191
ajv-keywords "^3.4.1"

0 commit comments

Comments
 (0)