11import * as path from "path" ;
2- import * as fs from "fs " ;
2+ import { NODE_MODULES_FOLDER_NAME , PACKAGE_JSON_FILE_NAME } from "../../constants " ;
33
44export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesBuilder {
5- private projectPath : string ;
6- private rootNodeModulesPath : string ;
7- private resolvedDependencies : any [ ] ;
8- private seen : any ;
9-
10- public constructor ( private $fs : IFileSystem ) {
11- this . seen = { } ;
12- this . resolvedDependencies = [ ] ;
13- }
5+ public constructor ( private $fs : IFileSystem ) { }
146
15- public getProductionDependencies ( projectPath : string ) : any [ ] {
16- this . projectPath = projectPath ;
17- this . rootNodeModulesPath = path . join ( this . projectPath , "node_modules" ) ;
7+ public getProductionDependencies ( projectPath : string ) : IDependencyData [ ] {
8+ const rootNodeModulesPath = path . join ( projectPath , NODE_MODULES_FOLDER_NAME ) ;
9+ const projectPackageJsonPath = path . join ( projectPath , PACKAGE_JSON_FILE_NAME ) ;
10+ const packageJsonContent = this . $fs . readJson ( projectPackageJsonPath ) ;
11+ const dependencies = packageJsonContent && packageJsonContent . dependencies ;
1812
19- let projectPackageJsonpath = path . join ( this . projectPath , "package.json" ) ;
20- let packageJsonContent = this . $fs . readJson ( projectPackageJsonpath ) ;
13+ let resolvedDependencies : IDependencyData [ ] = [ ] ;
2114
22- _ . keys ( packageJsonContent . dependencies ) . forEach ( dependencyName => {
23- let depth = 0 ;
24- let directory = path . join ( this . rootNodeModulesPath , dependencyName ) ;
15+ _ . keys ( dependencies )
16+ . forEach ( dependencyName => {
17+ const depth = 0 ;
18+ const directory = path . join ( rootNodeModulesPath , dependencyName ) ;
2519
26- // find and traverse child with name `key`, parent's directory -> dep.directory
27- this . traverseDependency ( dependencyName , directory , depth ) ;
28- } ) ;
20+ // find and traverse child with name `key`, parent's directory -> dep.directory
21+ this . traverseDependency ( resolvedDependencies , rootNodeModulesPath , dependencyName , directory , depth ) ;
22+ } ) ;
2923
30- return this . resolvedDependencies ;
24+ return resolvedDependencies ;
3125 }
3226
33- private traverseDependency ( name : string , currentModulePath : string , depth : number ) : void {
27+ private traverseDependency ( resolvedDependencies : IDependencyData [ ] , rootNodeModulesPath : string , name : string , currentModulePath : string , depth : number ) : void {
3428 // Check if child has been extracted in the parent's node modules, AND THEN in `node_modules`
3529 // Slower, but prevents copying wrong versions if multiple of the same module are installed
3630 // Will also prevent copying project's devDependency's version if current module depends on another version
37- let modulePath = path . join ( currentModulePath , "node_modules" , name ) ; // node_modules/parent/node_modules/<package>
38- let alternativeModulePath = path . join ( this . rootNodeModulesPath , name ) ;
31+ const modulePath = path . join ( currentModulePath , NODE_MODULES_FOLDER_NAME , name ) ; // node_modules/parent/node_modules/<package>
32+ const alternativeModulePath = path . join ( rootNodeModulesPath , name ) ;
3933
40- this . findModule ( modulePath , alternativeModulePath , name , depth ) ;
34+ this . findModule ( resolvedDependencies , rootNodeModulesPath , modulePath , alternativeModulePath , name , depth ) ;
4135 }
4236
43- private findModule ( modulePath : string , alternativeModulePath : string , name : string , depth : number ) {
37+ private findModule ( resolvedDependencies : IDependencyData [ ] , rootNodeModulesPath : string , modulePath : string , rootModulesPath : string , name : string , depth : number ) : void {
4438 let exists = this . moduleExists ( modulePath ) ;
39+ let depthInNodeModules = depth + 1 ;
4540
46- if ( exists ) {
47- if ( this . seen [ modulePath ] ) {
48- return ;
49- }
50-
51- let dependency = this . addDependency ( name , modulePath , depth + 1 ) ;
52- this . readModuleDependencies ( modulePath , depth + 1 , dependency ) ;
53- } else {
54- modulePath = alternativeModulePath ; // /node_modules/<package>
41+ if ( ! exists ) {
42+ modulePath = rootModulesPath ; // /node_modules/<package>
5543 exists = this . moduleExists ( modulePath ) ;
5644
57- if ( ! exists ) {
58- return ;
59- }
60-
61- if ( this . seen [ modulePath ] ) {
62- return ;
63- }
45+ depthInNodeModules = 0 ;
46+ }
6447
65- let dependency = this . addDependency ( name , modulePath , 0 ) ;
66- this . readModuleDependencies ( modulePath , 0 , dependency ) ;
48+ if ( ! exists || _ . some ( resolvedDependencies , deps => deps . directory === modulePath ) ) {
49+ return ;
6750 }
6851
69- this . seen [ modulePath ] = true ;
52+ const dependency = this . getDependencyInfo ( name , modulePath , depthInNodeModules ) ;
53+ resolvedDependencies . push ( dependency ) ;
54+
55+ this . readModuleDependencies ( resolvedDependencies , rootNodeModulesPath , modulePath , depthInNodeModules , dependency ) ;
7056 }
7157
72- private readModuleDependencies ( modulePath : string , depth : number , currentModule : any ) : void {
73- let packageJsonPath = path . join ( modulePath , 'package.json' ) ;
74- let packageJsonExists = fs . lstatSync ( packageJsonPath ) . isFile ( ) ;
58+ private readModuleDependencies ( resolvedDependencies : IDependencyData [ ] , rootNodeModulesPath : string , modulePath : string , depth : number , currentModule : IDependencyData ) : void {
59+ const packageJsonPath = path . join ( modulePath , PACKAGE_JSON_FILE_NAME ) ;
60+ const packageJsonExists = this . $ fs. getLsStats ( packageJsonPath ) . isFile ( ) ;
7561
7662 if ( packageJsonExists ) {
7763 let packageJsonContents = this . $fs . readJson ( packageJsonPath ) ;
@@ -81,31 +67,31 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB
8167 currentModule . nativescript = packageJsonContents . nativescript ;
8268 }
8369
84- _ . keys ( packageJsonContents . dependencies ) . forEach ( ( dependencyName ) => {
85- this . traverseDependency ( dependencyName , modulePath , depth ) ;
86- } ) ;
70+ _ . keys ( packageJsonContents . dependencies )
71+ . forEach ( ( dependencyName ) => {
72+ this . traverseDependency ( resolvedDependencies , rootNodeModulesPath , dependencyName , modulePath , depth ) ;
73+ } ) ;
8774 }
8875 }
8976
90- private addDependency ( name : string , directory : string , depth : number ) : any {
91- let dependency : any = {
77+ private getDependencyInfo ( name : string , directory : string , depth : number ) : IDependencyData {
78+ const dependency : IDependencyData = {
9279 name,
9380 directory,
9481 depth
9582 } ;
9683
97- this . resolvedDependencies . push ( dependency ) ;
98-
9984 return dependency ;
10085 }
10186
10287 private moduleExists ( modulePath : string ) : boolean {
10388 try {
104- let exists = fs . lstatSync ( modulePath ) ;
105- if ( exists . isSymbolicLink ( ) ) {
106- exists = fs . lstatSync ( fs . realpathSync ( modulePath ) ) ;
89+ let modulePathLsStat = this . $ fs. getLsStats ( modulePath ) ;
90+ if ( modulePathLsStat . isSymbolicLink ( ) ) {
91+ modulePathLsStat = this . $ fs. getLsStats ( this . $ fs. realpath ( modulePath ) ) ;
10792 }
108- return exists . isDirectory ( ) ;
93+
94+ return modulePathLsStat . isDirectory ( ) ;
10995 } catch ( e ) {
11096 return false ;
11197 }
0 commit comments