@@ -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