Skip to content

Commit e5de7ba

Browse files
committed
✨ add test
1 parent 657489f commit e5de7ba

File tree

6 files changed

+153
-37
lines changed

6 files changed

+153
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"webpack": "^4.39.2"
4646
},
4747
"dependencies": {
48-
"@micro-app/core": "^0.1.1-rc.2",
48+
"@micro-app/core": "^0.1.1-rc.3",
4949
"koa": "^2.8.1",
5050
"koa-static": "^5.0.0",
5151
"opn": "^5.5.0",

plugins/commands/init.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
11
'use strict';
22

3+
const shelljs = require('shelljs');
4+
const path = require('path');
5+
const chalk = require('chalk');
36

4-
// if (program.init) {
5-
// const mainPath = require.resolve('@micro-app/core');
6-
// const configPath = path.resolve(mainPath, '../../', microApp.CONSTANT.CONFIG_NAME);
7-
// const dest = path.resolve(process.cwd(), microApp.CONSTANT.CONFIG_NAME);
8-
// shelljs.cp('-R', configPath, dest);
7+
module.exports = function initCommand(api, opts) {
98

10-
// logger.success(`Init Fnished, Create: ${chalk.yellow(microApp.CONSTANT.CONFIG_NAME)}`);
11-
// }
9+
api.registerMethod('beforeCommandInit', {
10+
type: api.API_TYPE.EVENT,
11+
description: '初始化前事件',
12+
});
13+
api.registerMethod('afterCommandInit', {
14+
type: api.API_TYPE.EVENT,
15+
description: '初始化完毕后事件',
16+
});
17+
18+
// start
19+
api.registerCommand('init', {
20+
description: 'init micro app project.',
21+
usage: 'micro-app init [options]',
22+
options: {
23+
'-': 'init default.',
24+
// '-n <name>': 'only init <name>.',
25+
},
26+
details: `
27+
Examples:
28+
${chalk.gray('# init')}
29+
micro-app init
30+
`.trim(),
31+
}, args => {
32+
return initMicro(api, args);
33+
});
34+
};
35+
36+
function initMicro(api, args) {
37+
const logger = api.logger;
38+
const config = api.self;
39+
const micros = api.micros;
40+
const microsConfig = api.microsConfig;
41+
42+
api.applyPluginHooks('beforeCommandInit', { args, logger, microsConfig, micros, config });
43+
44+
const microApp = require('@micro-app/core');
45+
const mainPath = require.resolve('@micro-app/core');
46+
const from = path.resolve(mainPath, '../../', microApp.CONSTANTS.CONFIG_NAME);
47+
const to = path.resolve(microApp.CONSTANTS.ROOT, microApp.CONSTANTS.CONFIG_NAME);
48+
shelljs.cp('-R', from, to);
49+
50+
api.applyPluginHooks('afterCommandInit', { args, logger, microsConfig, micros, config, from, to });
51+
52+
logger.success(`Init Fnished, Create: ${chalk.yellow(microApp.CONSTANTS.CONFIG_NAME)}`);
53+
}

plugins/commands/init.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
/* global expect */
4+
5+
describe('Command init', () => {
6+
7+
it('init', () => {
8+
9+
const { service } = require('../../bin/base');
10+
11+
const plugin = service.plugins.find(item => item.id === 'cli:plugins-commands-init');
12+
expect(typeof plugin).toEqual('object');
13+
14+
service.init();
15+
16+
expect(plugin._api).not.toBeUndefined();
17+
18+
plugin._api.beforeCommandInit(({ logger }) => {
19+
expect(plugin._api.logger).toEqual(logger);
20+
});
21+
22+
plugin._api.afterCommandInit(({ from, to }) => {
23+
expect(from).not.toEqual(to);
24+
expect(from).not.toBeUndefined();
25+
expect(to).not.toBeUndefined();
26+
expect(require(from).micros).toEqual(require(to).micros);
27+
});
28+
29+
service.runCommand('init');
30+
31+
expect(service.commands.init).not.toBeNull();
32+
expect(service.commands.init).not.toBeUndefined();
33+
expect(typeof service.commands.init).toEqual('object');
34+
35+
});
36+
37+
});

plugins/commands/version.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
/* global expect */
4+
5+
describe('Command version', () => {
6+
7+
it('version', () => {
8+
9+
const { service } = require('../../bin/base');
10+
11+
const plugin = service.plugins.find(item => item.id === 'cli:plugins-commands-version');
12+
expect(typeof plugin).toEqual('object');
13+
14+
service.init();
15+
16+
expect(plugin._api).not.toBeUndefined();
17+
plugin._api.addCommandVersion({
18+
name: 'a',
19+
version: 'b',
20+
description: 'c',
21+
});
22+
23+
service.runCommand('version');
24+
25+
expect(service.commands.version).not.toBeNull();
26+
expect(service.commands.version).not.toBeUndefined();
27+
expect(typeof service.commands.version).toEqual('object');
28+
});
29+
30+
});

plugins/register.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,43 @@
22

33
const path = require('path');
44

5-
module.exports = function(service) {
6-
7-
service.registerPlugin({
8-
id: 'cli:plugins-commands-version',
9-
link: path.resolve(__dirname, './commands/version'),
5+
const commands = [
6+
{
7+
name: 'version',
108
description: '显示当前版本号',
11-
});
12-
13-
service.registerPlugin({
14-
id: 'cli:plugins-commands-serve',
15-
link: path.resolve(__dirname, './commands/serve'),
9+
},
10+
{
11+
name: 'serve',
1612
description: '服务启动命令行',
17-
});
18-
19-
service.registerPlugin({
20-
id: 'cli:plugins-commands-build',
21-
link: path.resolve(__dirname, './commands/build'),
13+
},
14+
{
15+
name: 'build',
2216
description: '构建命令行',
23-
});
24-
25-
service.registerPlugin({
26-
id: 'cli:plugins-commands-update',
27-
link: path.resolve(__dirname, './commands/update'),
17+
},
18+
{
19+
name: 'update',
2820
description: '强制更新 micros 依赖服务命令行',
29-
});
30-
31-
service.registerPlugin({
32-
id: 'cli:plugins-commands-deploy',
33-
link: path.resolve(__dirname, './commands/deploy'),
21+
},
22+
{
23+
name: 'deploy',
3424
description: '强制发布更新当前提交信息到指定 git 中命令行',
25+
},
26+
{
27+
name: 'init',
28+
description: '初始化命令行',
29+
},
30+
];
31+
32+
module.exports = function(service) {
33+
34+
commands.forEach(item => {
35+
const name = item.name;
36+
const description = item.description;
37+
service.registerPlugin({
38+
id: `cli:plugins-commands-${name}`,
39+
link: path.resolve(__dirname, './commands', name),
40+
description,
41+
});
3542
});
3643

3744
};

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@
379379
"@types/istanbul-reports" "^1.1.1"
380380
"@types/yargs" "^13.0.0"
381381

382-
"@micro-app/core@^0.1.1-rc.2":
383-
version "0.1.1-rc.2"
384-
resolved "https://registry.yarnpkg.com/@micro-app/core/-/core-0.1.1-rc.2.tgz#dc1dc355e4c246b976f1abf041711dbc52d11d1b"
385-
integrity sha512-w8GrSOwelQfktuUhrtmnGP+ya7bD4rDPpFElaMkXlMAMfJRQRy8DkRnt/jwyUc3k+51hbNGV4KuT/gfk64NlQQ==
382+
"@micro-app/core@^0.1.1-rc.3":
383+
version "0.1.1-rc.3"
384+
resolved "https://registry.yarnpkg.com/@micro-app/core/-/core-0.1.1-rc.3.tgz#724aeb1e9df601ee55a705e832c3530ddf08da0e"
385+
integrity sha512-hCphfir+E+fxNtvIRZK7tD4JNHj6W/nfKVvJz6qeOJbRuZ1fGq88S8lxw461yp8QxNtLkJw1A7lXY/bBLzL0fQ==
386386
dependencies:
387387
ajv "^6.10.2"
388388
ajv-keywords "^3.4.1"

0 commit comments

Comments
 (0)