@@ -6,33 +6,28 @@ import * as constants from "../../constants";
6
6
import * as minimatch from "minimatch" ;
7
7
import Future = require( "fibers/future" ) ;
8
8
9
- /**
10
- * Intercepts each directory as it is copied to the destination tempdir,
11
- * and tees a copy to the given path outside the tmp dir.
12
- */
13
- export class DestCopy {
14
- private dependencies : IDictionary < any > = null ;
15
- private devDependencies : IDictionary < any > = null ;
9
+ export interface ILocalDependencyData extends IDependencyData {
10
+ directory : string ;
11
+ }
16
12
13
+ export class NpmDependencyResolver {
17
14
constructor (
18
- private inputPath : string ,
19
- private cachePath : string ,
20
- private outputRoot : string ,
21
- private projectDir : string ,
22
- private platform : string ,
23
- private $fs : IFileSystem ,
24
- private $projectFilesManager : IProjectFilesManager ,
25
- private $pluginsService : IPluginsService ,
26
- private $platformsData : IPlatformsData
15
+ private projectDir : string
27
16
) {
28
- this . dependencies = Object . create ( null ) ;
29
- this . devDependencies = this . getDevDependencies ( projectDir ) ;
30
17
}
31
18
32
- public rebuildChangedDirectories ( changedDirectories : string [ ] , platform : string ) : void {
19
+ private getDevDependencies ( projectDir : string ) : IDictionary < any > {
20
+ let projectFilePath = path . join ( projectDir , constants . PACKAGE_JSON_FILE_NAME ) ;
21
+ let projectFileContent = require ( projectFilePath ) ;
22
+ return projectFileContent . devDependencies || { } ;
23
+ }
24
+
25
+ public resolveDependencies ( changedDirectories : string [ ] , platform : string ) : IDictionary < ILocalDependencyData > {
26
+ const devDependencies = this . getDevDependencies ( this . projectDir ) ;
27
+ const dependencies : IDictionary < ILocalDependencyData > = Object . create ( null ) ;
33
28
34
29
_ . each ( changedDirectories , changedDirectoryAbsolutePath => {
35
- if ( ! this . devDependencies [ path . basename ( changedDirectoryAbsolutePath ) ] ) {
30
+ if ( ! devDependencies [ path . basename ( changedDirectoryAbsolutePath ) ] ) {
36
31
let pathToPackageJson = path . join ( changedDirectoryAbsolutePath , constants . PACKAGE_JSON_FILE_NAME ) ;
37
32
let packageJsonFiles = fs . existsSync ( pathToPackageJson ) ? [ pathToPackageJson ] : [ ] ;
38
33
let nodeModulesFolderPath = path . join ( changedDirectoryAbsolutePath , constants . NODE_MODULES_FOLDER_NAME ) ;
@@ -41,15 +36,15 @@ export class DestCopy {
41
36
_ . each ( packageJsonFiles , packageJsonFilePath => {
42
37
let fileContent = require ( packageJsonFilePath ) ;
43
38
44
- if ( ! this . devDependencies [ fileContent . name ] && fileContent . name && fileContent . version ) { // Don't flatten dev dependencies and flatten only dependencies with valid package.json
45
- let currentDependency = {
39
+ if ( ! devDependencies [ fileContent . name ] && fileContent . name && fileContent . version ) { // Don't flatten dev dependencies and flatten only dependencies with valid package.json
40
+ let currentDependency : ILocalDependencyData = {
46
41
name : fileContent . name ,
47
42
version : fileContent . version ,
48
43
directory : path . dirname ( packageJsonFilePath ) ,
49
44
nativescript : fileContent . nativescript
50
45
} ;
51
46
52
- let addedDependency = this . dependencies [ currentDependency . name ] ;
47
+ let addedDependency = dependencies [ currentDependency . name ] ;
53
48
if ( addedDependency ) {
54
49
if ( semver . gt ( currentDependency . version , addedDependency . version ) ) {
55
50
let currentDependencyMajorVersion = semver . major ( currentDependency . version ) ;
@@ -59,26 +54,53 @@ export class DestCopy {
59
54
let logger = $injector . resolve ( "$logger" ) ;
60
55
currentDependencyMajorVersion === addedDependencyMajorVersion ? logger . out ( message ) : logger . warn ( message ) ;
61
56
62
- this . dependencies [ currentDependency . name ] = currentDependency ;
57
+ dependencies [ currentDependency . name ] = currentDependency ;
63
58
}
64
59
} else {
65
- this . dependencies [ currentDependency . name ] = currentDependency ;
60
+ dependencies [ currentDependency . name ] = currentDependency ;
66
61
}
67
62
}
68
63
} ) ;
69
64
}
70
65
} ) ;
71
- if ( ! _ . isEmpty ( this . dependencies ) ) {
72
- this . $platformsData . getPlatformData ( platform ) . platformProjectService . beforePrepareAllPlugins ( ) . wait ( ) ;
73
- }
66
+ return dependencies ;
67
+ }
74
68
75
- _ . each ( this . dependencies , dependency => {
76
- this . copyDependencyDir ( dependency ) ;
69
+ private enumeratePackageJsonFilesSync ( nodeModulesDirectoryPath : string , foundFiles ?: string [ ] ) : string [ ] {
70
+ foundFiles = foundFiles || [ ] ;
71
+ if ( fs . existsSync ( nodeModulesDirectoryPath ) ) {
72
+ let contents = fs . readdirSync ( nodeModulesDirectoryPath ) ;
73
+ for ( let i = 0 ; i < contents . length ; ++ i ) {
74
+ let moduleName = contents [ i ] ;
75
+ let moduleDirectoryInNodeModules = path . join ( nodeModulesDirectoryPath , moduleName ) ;
76
+ let packageJsonFilePath = path . join ( moduleDirectoryInNodeModules , constants . PACKAGE_JSON_FILE_NAME ) ;
77
+ if ( fs . existsSync ( packageJsonFilePath ) ) {
78
+ foundFiles . push ( packageJsonFilePath ) ;
79
+ }
77
80
78
- let isPlugin = ! ! dependency . nativescript ;
79
- if ( isPlugin ) {
80
- this . $pluginsService . prepare ( dependency , platform ) . wait ( ) ;
81
+ let directoryPath = path . join ( moduleDirectoryInNodeModules , constants . NODE_MODULES_FOLDER_NAME ) ;
82
+ if ( fs . existsSync ( directoryPath ) ) {
83
+ this . enumeratePackageJsonFilesSync ( directoryPath , foundFiles ) ;
84
+ } else if ( fs . statSync ( moduleDirectoryInNodeModules ) . isDirectory ( ) ) {
85
+ // Scoped modules (e.g. @angular) are grouped in a subfolder and we need to enumerate them too.
86
+ this . enumeratePackageJsonFilesSync ( moduleDirectoryInNodeModules , foundFiles ) ;
87
+ }
81
88
}
89
+ }
90
+ return foundFiles ;
91
+ }
92
+ }
93
+
94
+ export class TnsModulesCopy {
95
+ constructor (
96
+ private outputRoot : string ,
97
+ private $fs : IFileSystem
98
+ ) {
99
+ }
100
+
101
+ public copyModules ( dependencies : IDictionary < ILocalDependencyData > , platform : string ) : void {
102
+ _ . each ( dependencies , dependency => {
103
+ this . copyDependencyDir ( dependency ) ;
82
104
83
105
if ( dependency . name === constants . TNS_CORE_MODULES_NAME ) {
84
106
let tnsCoreModulesResourcePath = path . join ( this . outputRoot , constants . TNS_CORE_MODULES_NAME ) ;
@@ -92,10 +114,6 @@ export class DestCopy {
92
114
this . $fs . deleteDirectory ( tnsCoreModulesResourcePath ) . wait ( ) ;
93
115
}
94
116
} ) ;
95
-
96
- if ( ! _ . isEmpty ( this . dependencies ) ) {
97
- this . $platformsData . getPlatformData ( platform ) . platformProjectService . afterPrepareAllPlugins ( ) . wait ( ) ;
98
- }
99
117
}
100
118
101
119
private copyDependencyDir ( dependency : any ) : void {
@@ -109,36 +127,29 @@ export class DestCopy {
109
127
shelljs . cp ( "-Rf" , dependency . directory , targetDir ) ;
110
128
shelljs . rm ( "-rf" , path . join ( targetDir , dependency . name , "node_modules" ) ) ;
111
129
}
130
+ }
112
131
113
- private getDevDependencies ( projectDir : string ) : IDictionary < any > {
114
- let projectFilePath = path . join ( projectDir , constants . PACKAGE_JSON_FILE_NAME ) ;
115
- let projectFileContent = require ( projectFilePath ) ;
116
- return projectFileContent . devDependencies || { } ;
132
+ export class NpmPluginPrepare {
133
+ constructor (
134
+ private $fs : IFileSystem ,
135
+ private $pluginsService : IPluginsService ,
136
+ private $platformsData : IPlatformsData
137
+ ) {
117
138
}
118
139
119
- private enumeratePackageJsonFilesSync ( nodeModulesDirectoryPath : string , foundFiles ?: string [ ] ) : string [ ] {
120
- foundFiles = foundFiles || [ ] ;
121
- if ( fs . existsSync ( nodeModulesDirectoryPath ) ) {
122
- let contents = fs . readdirSync ( nodeModulesDirectoryPath ) ;
123
- for ( let i = 0 ; i < contents . length ; ++ i ) {
124
- let moduleName = contents [ i ] ;
125
- let moduleDirectoryInNodeModules = path . join ( nodeModulesDirectoryPath , moduleName ) ;
126
- let packageJsonFilePath = path . join ( moduleDirectoryInNodeModules , constants . PACKAGE_JSON_FILE_NAME ) ;
127
- if ( fs . existsSync ( packageJsonFilePath ) ) {
128
- foundFiles . push ( packageJsonFilePath ) ;
129
- }
140
+ public preparePlugins ( dependencies : IDictionary < IDependencyData > , platform : string ) : void {
141
+ if ( _ . isEmpty ( dependencies ) ) {
142
+ return ;
143
+ }
130
144
131
- let directoryPath = path . join ( moduleDirectoryInNodeModules , constants . NODE_MODULES_FOLDER_NAME ) ;
132
- if ( fs . existsSync ( directoryPath ) ) {
133
- this . enumeratePackageJsonFilesSync ( directoryPath , foundFiles ) ;
134
- } else if ( fs . statSync ( moduleDirectoryInNodeModules ) . isDirectory ( ) ) {
135
- // Some modules can be grouped in one folder and we need to enumerate them too (e.g. @angular).
136
- this . enumeratePackageJsonFilesSync ( moduleDirectoryInNodeModules , foundFiles ) ;
137
- }
145
+ this . $platformsData . getPlatformData ( platform ) . platformProjectService . beforePrepareAllPlugins ( ) . wait ( ) ;
146
+ _ . each ( dependencies , dependency => {
147
+ let isPlugin = ! ! dependency . nativescript ;
148
+ if ( isPlugin ) {
149
+ console . log ( "preparing: " + dependency . name ) ;
150
+ this . $pluginsService . prepare ( dependency , platform ) . wait ( ) ;
138
151
}
139
- }
140
- return foundFiles ;
152
+ } ) ;
153
+ this . $platformsData . getPlatformData ( platform ) . platformProjectService . afterPrepareAllPlugins ( ) . wait ( ) ;
141
154
}
142
155
}
143
-
144
- export default DestCopy ;
0 commit comments