Skip to content

Commit 677c3c3

Browse files
committed
feat: add build cmd and enhance bootstrap
1 parent 35271fa commit 677c3c3

34 files changed

+769
-2461
lines changed

.github/workflows/coveralls.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
name: Coveralls
22
on:
3-
pull_request:
4-
branches:
5-
- master
6-
- develop
73
push:
84
branches:
95
- master

README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ Pluggable micro application framework.
44

55
基于webpack多入口的多仓库业务模块开发的插件应用框架脚手架.
66

7+
[![Github Actions Coveralls][Github-Actions-Coveralls]][Github-Actions-Coveralls-url]
78
[![Coverage Status][Coverage-img]][Coverage-url]
8-
[![CircleCI][CircleCI-img]][CircleCI-url]
99
[![NPM Version][npm-img]][npm-url]
1010
[![NPM Download][download-img]][download-url]
1111

12+
[Github-Actions-Coveralls]: https://github.com/MicroAppJS/cli/workflows/Coveralls/badge.svg
13+
[Github-Actions-Coveralls-url]: https://github.com/MicroAppJS/cli
1214
[Coverage-img]: https://coveralls.io/repos/github/MicroAppJS/cli/badge.svg?branch=master
1315
[Coverage-url]: https://coveralls.io/github/MicroAppJS/cli?branch=master
14-
[CircleCI-img]: https://circleci.com/gh/MicroAppJS/cli/tree/master.svg?style=svg
15-
[CircleCI-url]: https://circleci.com/gh/MicroAppJS/cli/tree/master
1616
[npm-img]: https://img.shields.io/npm/v/@micro-app/cli.svg?style=flat-square
1717
[npm-url]: https://npmjs.org/package/@micro-app/cli
1818
[download-img]: https://img.shields.io/npm/dm/@micro-app/cli.svg?style=flat-square
@@ -59,12 +59,6 @@ module.exports = {
5959
},
6060
],
6161

62-
// dlls: [
63-
// {
64-
// context: __dirname,
65-
// },
66-
// ],
67-
6862
alias: { // 别名配置
6963
api: '',
7064
config: {
@@ -84,11 +78,9 @@ module.exports = {
8478

8579
// 服务配置
8680
server: {
81+
hooks: '',
8782
entry: '', // 服务端入口
8883
port: 8088, // 服务端口号
89-
options: {
90-
// 服务端回调参数
91-
},
9284
},
9385

9486
plugins: [ // 自定义插件
@@ -103,7 +95,7 @@ module.exports = {
10395
};
10496
```
10597

106-
### `package.json` 中加载其他模块, 例如:
98+
### `package.json` 中加载其他模块, 例如
10799

108100
```json
109101
"dependencies": {

micro-app.config.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ module.exports = {
55
description: '',
66
version: '0.0.1',
77
type: '', // types 类型
8-
webpack: { // webpack 配置 (只有自己使用)
9-
// output: {
10-
// path: path.resolve(__dirname, 'public'),
11-
// publicPath: '/public/',
12-
// },
13-
},
148

159
entry: {
1610
main: './test/index.js',

package.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/cli",
3-
"version": "0.3.0-alpha.3",
3+
"version": "0.3.0-alpha.5",
44
"description": "[CLI] Pluggable micro application framework.",
55
"main": "src/index.js",
66
"bin": {
@@ -9,9 +9,11 @@
99
"micro-app-start": "bin/micro-app-start"
1010
},
1111
"scripts": {
12+
"prepublishOnly": "npm run test",
1213
"lint": "eslint .",
1314
"lint:fix": "npm run lint -- --fix",
14-
"test": "jest"
15+
"test": "jest",
16+
"publish:next": "npm publish --tag next"
1517
},
1618
"homepage": "https://github.com/MicroAppJS/cli",
1719
"repository": {
@@ -57,20 +59,16 @@
5759
"eslint-config-2o3t": "^1.1.17",
5860
"husky": "^3.1.0",
5961
"jest": "^24.9.0",
60-
"lint-staged": "^9.5.0",
61-
"webpack": "^4.41.5"
62+
"lint-staged": "^9.5.0"
6263
},
6364
"peerDependencies": {
64-
"@micro-app/core": ">=0.3.1"
65+
"@micro-app/core": ">=0.3.3"
6566
},
6667
"dependencies": {
67-
"@micro-app/core": "^0.3.1",
68+
"@micro-app/core": "^0.3.3",
69+
"@micro-app/shared-utils": "^0.1.14",
6870
"@zkochan/cmd-shim": "^3.1.0",
69-
"execa": "^3.4.0",
70-
"koa": "^2.11.0",
7171
"read-cmd-shim": "^1.0.5",
72-
"shelljs": "^0.8.3",
73-
"signal-exit": "^3.0.2",
7472
"update-notifier": "^3.0.1",
7573
"yargs-parser": "^16.1.0"
7674
},

src/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ const cmd = process.argv[2];
1111
const argv = yParser(process.argv.slice(3));
1212

1313
const Service = require('@micro-app/core');
14-
const { _, logger } = require('@micro-app/shared-utils');
14+
const { _, logger, fs, tryRequire, path } = require('@micro-app/shared-utils');
1515

1616
const service = new Service(_.cloneDeep(argv));
1717

1818
// 注册插件
1919
require('./plugins/register')(service);
2020

2121
// 预加载插件
22-
// ZAP prePlugin
22+
// ZAP --pre-register-plugin
23+
if (argv.preRegisterPlugin && _.isString(argv.preRegisterPlugin)) {
24+
const preRegisterPluginPath = path.resolve(service.root, argv.preRegisterPlugin);
25+
if (fs.pathExistsSync(preRegisterPluginPath)) {
26+
const preRegisterPlugin = tryRequire(preRegisterPluginPath);
27+
if (_.isFunction(preRegisterPlugin)) {
28+
preRegisterPlugin(service);
29+
}
30+
}
31+
}
2332

2433
module.exports = { cmd, argv, service, logger };

src/plugins/commands/bootstrap/index.js

Lines changed: 23 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ class BootstrapCommand extends Command {
3535

3636
execute(args) {
3737
const { registry, npmClient = 'npm', npmClientArgs = [] } = this.options;
38-
const { scope = false, force = false } = args;
38+
const { force = false } = args;
3939
const api = this.api;
4040

41+
const spinner = api.logger.spinner('bootstrap...');
42+
4143
let chain = Promise.resolve();
4244

43-
chain = chain.then(() => api.applyPluginHooks('beforeCommandBootstrap', { args, options: this.options }));
45+
chain = chain.then(() => {
46+
spinner.start();
47+
});
4448

45-
chain = chain.then(() => this.initPackages({ scope }));
49+
chain = chain.then(() => api.applyPluginHooks('beforeCommandBootstrap', { args, options: this.options }));
4650

4751
chain = chain.then(() => {
4852
this.npmConfig = {
@@ -60,9 +64,9 @@ class BootstrapCommand extends Command {
6064
}
6165
});
6266

63-
chain = chain.then(() => this.initTempFiles());
67+
chain = chain.then(() => this.initPackages());
6468

65-
chain = chain.then(() => this.progress(true));
69+
chain = chain.then(() => this.initTempFiles());
6670

6771
// 判断 node_modules 是否存在
6872
chain = chain.then(() => this.initNodeModules({ force }));
@@ -71,14 +75,16 @@ class BootstrapCommand extends Command {
7175

7276
chain = chain.then(() => this.initSymlinks());
7377

74-
chain = chain.then(() => this.progress(false));
75-
7678
chain = chain.then(() => api.applyPluginHooks('afterCommandBootstrap', { args, options: this.options }));
7779

78-
return chain;
80+
return chain.then(() => {
81+
spinner.succeed('finished!');
82+
}).catch(e => {
83+
spinner.fail(e.message);
84+
});
7985
}
8086

81-
initPackages({ scope }) {
87+
initPackages() {
8288
const { _ } = require('@micro-app/shared-utils');
8389

8490
const api = this.api;
@@ -95,46 +101,23 @@ class BootstrapCommand extends Command {
95101
return item.pkgInfo;
96102
});
97103

98-
api.logger.debug('pkgs', pkgs.length);
99-
100-
const resultPkgs = [];
101-
pkgs.forEach(item => {
102-
if (scope) { // 是否指定 scope?
103-
resultPkgs.push(_.merge({}, item, {
104-
name: `${scope}/${item.name}`,
105-
}));
106-
} else {
107-
resultPkgs.push(item);
108-
}
109-
});
110-
111-
this.filteredPackages = resultPkgs;
104+
api.logger.debug('[bootstrap > initPackages]', pkgs.length);
105+
this.filteredPackages = pkgs;
112106
}
113107

114108
initTempFiles() {
115109
const initTempDir = require('./initTempDir');
116-
117110
const api = this.api;
118-
119111
this.tempDir = initTempDir(api);
120112
}
121113

122-
progress(flag) {
123-
const { logger } = require('@micro-app/shared-utils');
124-
if (flag) {
125-
logger.enableProgress();
126-
} else {
127-
logger.disableProgress();
128-
}
129-
}
130-
131114
initNodeModules() {
132115
const { fs } = require('@micro-app/shared-utils');
133116
const npmInstall = require('./npmInstall');
134117
const api = this.api;
135118
const currentNodeModules = api.nodeModulesPath;
136119
if (fs.pathExistsSync(currentNodeModules)) {
137-
api.logger.warn('bootstrap', 'skip install!');
120+
api.logger.warn('[bootstrap]', 'skip root install!');
138121
return;
139122
}
140123
const root = api.root;
@@ -146,97 +129,19 @@ class BootstrapCommand extends Command {
146129
const npmInstall = require('./npmInstall');
147130
const api = this.api;
148131
if (fs.pathExistsSync(this.tempDir) && fs.readdirSync(this.tempDir).length > 0 && !force) {
149-
api.logger.warn('bootstrap', `${this.tempDir} is not empty!`);
132+
api.logger.warn('[bootstrap]', `${this.tempDir} is not empty!`);
150133
return;
151134
}
152-
const selfConfig = api.selfConfig;
153-
return npmInstall.micros(selfConfig.manifest, this.filteredPackages, this.tempDir, this.npmConfig);
154-
// return npmInstall.dependencies(selfConfig.manifest, this.filteredPackages, this.npmConfig);
135+
return npmInstall.micros(this.filteredPackages, this.tempDir, this.npmConfig);
155136
}
156137

157138
initSymlinks() {
158-
const path = require('path');
159-
const { _, fs } = require('@micro-app/shared-utils');
160-
161-
// 初始化链接, 依赖 packages
139+
const initSymlinks = require('./initSymlinks');
162140
const filteredPackages = this.filteredPackages;
163-
if (_.isEmpty(filteredPackages)) {
164-
return;
165-
}
166-
167141
const api = this.api;
168-
const tempDirPackageGraph = api.tempDirPackageGraph;
169-
170-
const pkgs = tempDirPackageGraph.addDependents(filteredPackages.map(item => ({ name: item.name })));
171-
// console.warn(pkgs);
172-
const dependencies = pkgs.reduce((arrs, item) => {
173-
const deps = item.dependencies || {};
174-
return arrs.concat(Object.keys(deps).map(name => ({ name })));
175-
}, []);
176-
177-
const finallyDeps = tempDirPackageGraph.addDependencies(pkgs.concat(dependencies));
178-
179-
const symlink = require('../../../utils/symlink');
180-
const currentNodeModules = api.nodeModulesPath;
181142

182-
return Promise.all(finallyDeps.map(item => {
183-
184-
const dependencyName = item.name;
185-
const targetDirectory = path.join(currentNodeModules, dependencyName);
186-
187-
let chain = Promise.resolve();
188-
189-
// check if dependency is already installed
190-
chain = chain.then(() => fs.pathExists(targetDirectory));
191-
192-
chain = chain.then(dirExists => {
193-
if (dirExists) {
194-
195-
const isDepSymlink = symlink.resolve(targetDirectory);
196-
if (isDepSymlink !== false && isDepSymlink !== item.location) {
197-
// installed dependency is a symlink pointing to a different location
198-
api.logger.warn(
199-
'EREPLACE_OTHER',
200-
`Symlink already exists for ${dependencyName}, ` +
201-
'but links to different location. Replacing with updated symlink...'
202-
);
203-
} else if (isDepSymlink === false) {
204-
// installed dependency is not a symlink
205-
api.logger.warn(
206-
'EREPLACE_EXIST',
207-
`${dependencyName} is already installed.`
208-
);
209-
210-
// break;
211-
return true;
212-
}
213-
} else {
214-
// ensure destination directory exists (dealing with scoped subdirs)
215-
fs.ensureDir(path.dirname(targetDirectory));
216-
return false;
217-
}
218-
});
219-
220-
chain = chain.then(isBreak => {
221-
if (!isBreak) {
222-
// create package symlink
223-
const dependencyLocation = item.contents
224-
? path.resolve(currentNodeModules, item.contents)
225-
: currentNodeModules;
226-
227-
api.logger.debug('junction', 'dependencyLocation: ', dependencyLocation);
228-
symlink.create(dependencyLocation, targetDirectory, 'junction');
229-
} else {
230-
api.logger.debug('junction', 'skip: ', dependencyName);
231-
}
232-
});
233-
234-
// TODO: pass PackageGraphNodes directly instead of Packages
235-
// chain = chain.then(() => symlinkBinary(dependencyNode.pkg, currentNode.pkg));
236-
237-
return chain;
238-
})).then(() => {
239-
api.logger.info('initSymlinks', 'finished');
143+
return initSymlinks(api, { filteredPackages }).then(() => {
144+
api.logger.debug('[bootstrap > initSymlinks]', 'finished');
240145
});
241146
}
242147
}

0 commit comments

Comments
 (0)