@@ -26,19 +26,19 @@ function main() {
2626 //
2727 const entries = Object . keys ( yarn . object ) . map ( key => makeYarnEntry ( key , yarn . object [ key ] ) ) ;
2828
29- // For all top-level folders in the node_modules directory that
30- // contain a package.json file...
31- const getModulesIn = p => fs . readdirSync ( p )
32- . filter ( f =>
33- fs . statSync ( path . join ( p , f ) ) . isDirectory ( ) &&
34- fs . existsSync ( path . join ( p , f , 'package.json' ) ) &&
35- fs . statSync ( path . join ( p , f , 'package.json' ) ) . isFile ( ) ) ;
36-
37- // ... parse ithem
38- const modules = getModulesIn ( 'node_modules' ) . map ( dir => parseNodeModulePackageJson ( dir ) ) ;
39-
40- // Iterate all the modules and merge the information from yarn into
41- // the module
29+ // Scan the node_modules directory and find all top-level ('foo') or scoped (@bar/baz)
30+ // modules, i.e. folders which contain a package.json file...
31+ const getModulesIn = p => fs . readdirSync ( p ) . filter ( f => isPackage ( p , undefined , f ) ) ;
32+ const findScopes = p => fs . readdirSync ( p ) . filter ( f => f . startsWith ( "@" ) && fs . statSync ( path . join ( p , f ) ) . isDirectory ( ) ) ;
33+ const getModulesInScope = ( p , s ) => fs . readdirSync ( path . join ( p , s ) ) . filter ( f => isPackage ( p , s , f ) ) ;
34+
35+ // ...then parse the package.json files to collect the metadata
36+ let topLevelModuleDirs = getModulesIn ( 'node_modules' ) ;
37+ let scopeModuleDirs = findScopes ( 'node_modules' ) . map ( scope => getModulesInScope ( 'node_modules' , scope ) . map ( m => scope + '/' + m ) ) . reduce ( ( a , b ) => a . concat ( b ) , [ ] ) ;
38+ let moduleDirs = topLevelModuleDirs . concat ( scopeModuleDirs ) ;
39+ const modules = moduleDirs . map ( dir => parseNodeModulePackageJson ( dir ) ) ;
40+
41+ // Iterate all the modules and merge the information from yarn into the module
4242 modules . forEach ( module => mergePackageJsonWithYarnEntry ( entries , module ) ) ;
4343
4444 // Didn't realize that the nodejs module ecosystem can contain
@@ -68,6 +68,12 @@ function main() {
6868 print ( "# EOF" ) ;
6969}
7070
71+ function isPackage ( p , s , f ) {
72+ let dir = s ? path . join ( p , s , f ) : path . join ( p , f ) ;
73+ return fs . statSync ( dir ) . isDirectory ( ) &&
74+ fs . existsSync ( path . join ( dir , 'package.json' ) ) &&
75+ fs . statSync ( path . join ( dir , 'package.json' ) ) . isFile ( )
76+ }
7177
7278/**
7379 * Given a list of yarn entries and a target module, find an exact
@@ -202,9 +208,10 @@ function breakCircularDependencies(modules) {
202208 }
203209 } ) ;
204210
205- // Each entry in the cluster must have no other outgoing
206- // dependencies
207- entry . deps = new Set ( ) ;
211+ // Each entry in the cluster must have no connections to other
212+ // dependencies in the cluster, or on the cluster pseudo-dep
213+ pseudo . deps . forEach ( circ => entry . deps . delete ( circ ) ) ;
214+ entry . deps . delete ( pseudo ) ;
208215 } ) ;
209216
210217 // Store this new pseudo-module in the modules list
@@ -278,17 +285,22 @@ function printNodeModule(module) {
278285 print ( `` ) ;
279286 printJson ( module ) ;
280287 print ( `node_module(` ) ;
281- print ( ` name = "${ module . name } ",` ) ;
288+ print ( ` name = "${ module . yarn ? module . yarn . label : module . name } ",` ) ;
282289
283290 // SCC pseudomodule wont have 'yarn' property
284291 if ( module . yarn ) {
285292 const url = module . yarn . url || module . url ;
286293 const sha1 = module . yarn . sha1 ;
287294 const executables = module . executables ;
288-
295+
296+ print ( ` module_name = "${ module . name } ",` ) ;
289297 print ( ` version = "${ module . version } ",` ) ;
290298 print ( ` package_json = "node_modules/${ module . name } /package.json",` ) ;
291- print ( ` srcs = glob(["node_modules/${ module . name } /**/*"], exclude = ["node_modules/${ module . name } /package.json"]),` ) ;
299+ // Exclude filenames with spaces: Bazel can't cope with them (we just have to hope they aren't needed later...)
300+ print ( ` srcs = glob(["node_modules/${ module . name } /**/*"], exclude = [
301+ "node_modules/${ module . name } /package.json",
302+ "**/* *",
303+ ]),` ) ;
292304 if ( url ) {
293305 print ( ` url = "${ url } ",` ) ;
294306 }
@@ -303,12 +315,12 @@ function printNodeModule(module) {
303315 }
304316 print ( ` },` ) ;
305317 }
306-
307318 }
319+
308320 if ( deps && deps . size ) {
309321 print ( ` deps = [` ) ;
310322 deps . forEach ( dep => {
311- print ( ` ":${ dep . name } ",` ) ;
323+ print ( ` ":${ dep . yarn ? dep . yarn . label : dep . name } ",` ) ;
312324 } ) ;
313325 print ( ` ],` ) ;
314326 }
@@ -327,7 +339,7 @@ function printNodeModuleAll(modules) {
327339 print ( ` name = "_all_",` ) ;
328340 print ( ` deps = [` ) ;
329341 modules . forEach ( module => {
330- print ( ` ":${ module . name } ",` ) ;
342+ print ( ` ":${ module . yarn ? module . yarn . label : module . name } ",` ) ;
331343 } ) ;
332344 print ( ` ],` ) ;
333345 print ( `)` ) ;
0 commit comments