@@ -6,33 +6,28 @@ import * as constants from "../../constants";
66import * as minimatch from "minimatch" ;
77import Future = require( "fibers/future" ) ;
88
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+ }
1612
13+ export class NpmDependencyResolver {
1714 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
2716 ) {
28- this . dependencies = Object . create ( null ) ;
29- this . devDependencies = this . getDevDependencies ( projectDir ) ;
3017 }
3118
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 ) ;
3328
3429 _ . each ( changedDirectories , changedDirectoryAbsolutePath => {
35- if ( ! this . devDependencies [ path . basename ( changedDirectoryAbsolutePath ) ] ) {
30+ if ( ! devDependencies [ path . basename ( changedDirectoryAbsolutePath ) ] ) {
3631 let pathToPackageJson = path . join ( changedDirectoryAbsolutePath , constants . PACKAGE_JSON_FILE_NAME ) ;
3732 let packageJsonFiles = fs . existsSync ( pathToPackageJson ) ? [ pathToPackageJson ] : [ ] ;
3833 let nodeModulesFolderPath = path . join ( changedDirectoryAbsolutePath , constants . NODE_MODULES_FOLDER_NAME ) ;
@@ -41,15 +36,15 @@ export class DestCopy {
4136 _ . each ( packageJsonFiles , packageJsonFilePath => {
4237 let fileContent = require ( packageJsonFilePath ) ;
4338
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 = {
4641 name : fileContent . name ,
4742 version : fileContent . version ,
4843 directory : path . dirname ( packageJsonFilePath ) ,
4944 nativescript : fileContent . nativescript
5045 } ;
5146
52- let addedDependency = this . dependencies [ currentDependency . name ] ;
47+ let addedDependency = dependencies [ currentDependency . name ] ;
5348 if ( addedDependency ) {
5449 if ( semver . gt ( currentDependency . version , addedDependency . version ) ) {
5550 let currentDependencyMajorVersion = semver . major ( currentDependency . version ) ;
@@ -59,26 +54,53 @@ export class DestCopy {
5954 let logger = $injector . resolve ( "$logger" ) ;
6055 currentDependencyMajorVersion === addedDependencyMajorVersion ? logger . out ( message ) : logger . warn ( message ) ;
6156
62- this . dependencies [ currentDependency . name ] = currentDependency ;
57+ dependencies [ currentDependency . name ] = currentDependency ;
6358 }
6459 } else {
65- this . dependencies [ currentDependency . name ] = currentDependency ;
60+ dependencies [ currentDependency . name ] = currentDependency ;
6661 }
6762 }
6863 } ) ;
6964 }
7065 } ) ;
71- if ( ! _ . isEmpty ( this . dependencies ) ) {
72- this . $platformsData . getPlatformData ( platform ) . platformProjectService . beforePrepareAllPlugins ( ) . wait ( ) ;
73- }
66+ return dependencies ;
67+ }
7468
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+ }
7780
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+ }
8188 }
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 ) ;
82104
83105 if ( dependency . name === constants . TNS_CORE_MODULES_NAME ) {
84106 let tnsCoreModulesResourcePath = path . join ( this . outputRoot , constants . TNS_CORE_MODULES_NAME ) ;
@@ -92,10 +114,6 @@ export class DestCopy {
92114 this . $fs . deleteDirectory ( tnsCoreModulesResourcePath ) . wait ( ) ;
93115 }
94116 } ) ;
95-
96- if ( ! _ . isEmpty ( this . dependencies ) ) {
97- this . $platformsData . getPlatformData ( platform ) . platformProjectService . afterPrepareAllPlugins ( ) . wait ( ) ;
98- }
99117 }
100118
101119 private copyDependencyDir ( dependency : any ) : void {
@@ -109,36 +127,29 @@ export class DestCopy {
109127 shelljs . cp ( "-Rf" , dependency . directory , targetDir ) ;
110128 shelljs . rm ( "-rf" , path . join ( targetDir , dependency . name , "node_modules" ) ) ;
111129 }
130+ }
112131
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+ ) {
117138 }
118139
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+ }
130144
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 ( ) ;
138151 }
139- }
140- return foundFiles ;
152+ } ) ;
153+ this . $platformsData . getPlatformData ( platform ) . platformProjectService . afterPrepareAllPlugins ( ) . wait ( ) ;
141154 }
142155}
143-
144- export default DestCopy ;
0 commit comments