|
2 | 2 |
|
3 | 3 | // TODO 需要整理目录结构,需要保留 package.json, 但是要更改内容
|
4 | 4 |
|
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; |
38 | 32 | const { force = false } = args;
|
39 |
| - const api = this.api; |
40 | 33 |
|
41 | 34 | const spinner = api.logger.spinner('bootstrap...');
|
42 | 35 |
|
| 36 | + const npmConfig = { |
| 37 | + registry, |
| 38 | + npmClient, |
| 39 | + npmClientArgs, |
| 40 | + }; |
| 41 | + |
43 | 42 | let chain = Promise.resolve();
|
44 | 43 |
|
45 | 44 | chain = chain.then(() => {
|
46 | 45 | spinner.start();
|
47 | 46 | });
|
48 | 47 |
|
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 })); |
58 | 49 |
|
59 | 50 | chain = chain.then(() => {
|
60 | 51 | if (npmClient === 'yarn') {
|
61 |
| - this.npmConfig.npmClientArgs.unshift('--pure-lockfile'); |
| 52 | + npmConfig.npmClientArgs.unshift('--pure-lockfile'); |
62 | 53 | } else {
|
63 |
| - this.npmConfig.npmClientArgs.unshift('--no-save'); |
| 54 | + npmConfig.npmClientArgs.unshift('--no-save'); |
64 | 55 | }
|
65 | 56 | });
|
66 | 57 |
|
67 |
| - chain = chain.then(() => this.initPackages()); |
68 |
| - |
69 |
| - chain = chain.then(() => this.initTempFiles()); |
70 |
| - |
71 | 58 | // 判断 node_modules 是否存在
|
72 |
| - chain = chain.then(() => this.initNodeModules({ force })); |
| 59 | + chain = chain.then(() => initNodeModules(api, { force, npmConfig })); |
73 | 60 |
|
74 |
| - chain = chain.then(() => this.bootstrap({ force })); |
| 61 | + chain = chain.then(() => filterPackages(api)); |
75 | 62 |
|
76 |
| - chain = chain.then(() => this.initSymlinks()); |
| 63 | + chain = chain.then(pkgs => bootstrap(api, { force, npmConfig, pkgs })); |
77 | 64 |
|
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 })); |
79 | 68 |
|
80 | 69 | return chain.then(() => {
|
81 | 70 | spinner.succeed('finished!');
|
82 | 71 | }).catch(e => {
|
83 | 72 | spinner.fail(e.message);
|
84 | 73 | });
|
85 |
| - } |
86 | 74 |
|
87 |
| - initPackages() { |
88 |
| - const { _ } = require('@micro-app/shared-utils'); |
| 75 | + }); |
| 76 | +}; |
89 | 77 |
|
90 |
| - const api = this.api; |
91 |
| - const allPackages = api.packages; |
92 | 78 |
|
93 |
| - if (_.isEmpty(allPackages)) { |
94 |
| - return; |
95 |
| - } |
| 79 | +function filterPackages(api) { |
| 80 | + const { _ } = require('@micro-app/shared-utils'); |
96 | 81 |
|
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; |
103 | 83 |
|
104 |
| - api.logger.debug('[bootstrap > initPackages]', pkgs.length); |
105 |
| - this.filteredPackages = pkgs; |
| 84 | + if (_.isEmpty(allPackages)) { |
| 85 | + return; |
106 | 86 | }
|
107 | 87 |
|
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 | + }); |
117 | 94 |
|
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 | +} |
130 | 98 |
|
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; |
142 | 106 | }
|
| 107 | + const root = api.root; |
| 108 | + return npmInstall(root, npmConfig); |
| 109 | +} |
143 | 110 |
|
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 | + } |
152 | 121 | }
|
| 122 | + return npmInstall.micros(pkgs, tempDir, npmConfig).then(() => pkgs); |
153 | 123 | }
|
154 | 124 |
|
| 125 | +function initSymlinks(api, { pkgs }) { |
| 126 | + const initSymlinks = require('./initSymlinks'); |
155 | 127 |
|
156 |
| -module.exports = BootstrapCommand; |
| 128 | + return initSymlinks(api, { filteredPackages: pkgs }).then(() => { |
| 129 | + api.logger.debug('[bootstrap > initSymlinks]', 'finished'); |
| 130 | + }); |
| 131 | +} |
157 | 132 |
|
158 | 133 | module.exports.configuration = {
|
159 | 134 | description: '初始化命令行',
|
|
0 commit comments