@@ -15,8 +15,8 @@ import Future = require("fibers/future");
1515 * and tees a copy to the given path outside the tmp dir.
1616 */
1717export class DestCopy implements IBroccoliPlugin {
18- private dependencies : IDictionary < any > = null ;
19- private devDependencies : IDictionary < any > = null ;
18+ private dependencies : IDictionary < any > = null ;
19+ private devDependencies : IDictionary < any > = null ;
2020
2121 constructor (
2222 private inputPath : string ,
@@ -33,72 +33,72 @@ export class DestCopy implements IBroccoliPlugin {
3333 this . devDependencies = this . getDevDependencies ( projectDir ) ;
3434 }
3535
36- public rebuildChangedDirectories ( changedDirectories : string [ ] , platform : string ) : void {
37- _ . each ( changedDirectories , changedDirectoryAbsolutePath => {
38- if ( ! this . devDependencies [ path . basename ( changedDirectoryAbsolutePath ) ] ) {
39- let pathToPackageJson = path . join ( changedDirectoryAbsolutePath , constants . PACKAGE_JSON_FILE_NAME ) ;
40- let packageJsonFiles = fs . existsSync ( pathToPackageJson ) ? [ pathToPackageJson ] : [ ] ;
41- let nodeModulesFolderPath = path . join ( changedDirectoryAbsolutePath , constants . NODE_MODULES_FOLDER_NAME ) ;
42- packageJsonFiles = packageJsonFiles . concat ( this . enumeratePackageJsonFilesSync ( nodeModulesFolderPath ) ) ;
43-
44- _ . each ( packageJsonFiles , packageJsonFilePath => {
45- let fileContent = require ( packageJsonFilePath ) ;
46-
47- if ( ! this . devDependencies [ fileContent . name ] ) { // Don't flatten dev dependencies
48- let currentDependency = {
49- name : fileContent . name ,
50- version : fileContent . version ,
51- directory : path . dirname ( packageJsonFilePath ) ,
52- nativescript : fileContent . nativescript
53- } ;
54-
55- let addedDependency = this . dependencies [ currentDependency . name ] ;
56- if ( addedDependency ) {
57- if ( semver . gt ( currentDependency . version , addedDependency . version ) ) {
58- let currentDependencyMajorVersion = semver . major ( currentDependency . version ) ;
59- let addedDependencyMajorVersion = semver . major ( addedDependency . version ) ;
60-
61- let message = `The depedency located at ${ addedDependency . directory } with version ${ addedDependency . version } will be replaced with dependency located at ${ currentDependency . directory } with version ${ currentDependency . version } ` ;
62- let logger = $injector . resolve ( "$logger" ) ;
63- currentDependencyMajorVersion === addedDependencyMajorVersion ? logger . out ( message ) : logger . warn ( message ) ;
64-
36+ public rebuildChangedDirectories ( changedDirectories : string [ ] , platform : string ) : void {
37+ _ . each ( changedDirectories , changedDirectoryAbsolutePath => {
38+ if ( ! this . devDependencies [ path . basename ( changedDirectoryAbsolutePath ) ] ) {
39+ let pathToPackageJson = path . join ( changedDirectoryAbsolutePath , constants . PACKAGE_JSON_FILE_NAME ) ;
40+ let packageJsonFiles = fs . existsSync ( pathToPackageJson ) ? [ pathToPackageJson ] : [ ] ;
41+ let nodeModulesFolderPath = path . join ( changedDirectoryAbsolutePath , constants . NODE_MODULES_FOLDER_NAME ) ;
42+ packageJsonFiles = packageJsonFiles . concat ( this . enumeratePackageJsonFilesSync ( nodeModulesFolderPath ) ) ;
43+
44+ _ . each ( packageJsonFiles , packageJsonFilePath => {
45+ let fileContent = require ( packageJsonFilePath ) ;
46+
47+ if ( ! this . devDependencies [ fileContent . name ] ) { // Don't flatten dev dependencies
48+ let currentDependency = {
49+ name : fileContent . name ,
50+ version : fileContent . version ,
51+ directory : path . dirname ( packageJsonFilePath ) ,
52+ nativescript : fileContent . nativescript
53+ } ;
54+
55+ let addedDependency = this . dependencies [ currentDependency . name ] ;
56+ if ( addedDependency ) {
57+ if ( semver . gt ( currentDependency . version , addedDependency . version ) ) {
58+ let currentDependencyMajorVersion = semver . major ( currentDependency . version ) ;
59+ let addedDependencyMajorVersion = semver . major ( addedDependency . version ) ;
60+
61+ let message = `The depedency located at ${ addedDependency . directory } with version ${ addedDependency . version } will be replaced with dependency located at ${ currentDependency . directory } with version ${ currentDependency . version } ` ;
62+ let logger = $injector . resolve ( "$logger" ) ;
63+ currentDependencyMajorVersion === addedDependencyMajorVersion ? logger . out ( message ) : logger . warn ( message ) ;
64+
65+ this . dependencies [ currentDependency . name ] = currentDependency ;
66+ }
67+ } else {
6568 this . dependencies [ currentDependency . name ] = currentDependency ;
6669 }
67- } else {
68- this . dependencies [ currentDependency . name ] = currentDependency ;
6970 }
70- }
71- } ) ;
72- }
73- } ) ;
71+ } ) ;
72+ }
73+ } ) ;
7474
75- _ . each ( this . dependencies , dependency => {
76- this . copyDependencyDir ( dependency ) ;
75+ _ . each ( this . dependencies , dependency => {
76+ this . copyDependencyDir ( dependency ) ;
7777
78- let isPlugin = ! ! dependency . nativescript ;
79- if ( isPlugin ) {
80- this . $pluginsService . prepare ( dependency , platform ) . wait ( ) ;
81- }
78+ let isPlugin = ! ! dependency . nativescript ;
79+ if ( isPlugin ) {
80+ this . $pluginsService . prepare ( dependency , platform ) . wait ( ) ;
81+ }
8282
83- if ( dependency . name === constants . TNS_CORE_MODULES_NAME ) {
84- let tnsCoreModulesResourcePath = path . join ( this . outputRoot , constants . TNS_CORE_MODULES_NAME ) ;
83+ if ( dependency . name === constants . TNS_CORE_MODULES_NAME ) {
84+ let tnsCoreModulesResourcePath = path . join ( this . outputRoot , constants . TNS_CORE_MODULES_NAME ) ;
8585
86- // Remove .ts files
87- let allFiles = this . $fs . enumerateFilesInDirectorySync ( tnsCoreModulesResourcePath ) ;
88- let deleteFilesFutures = allFiles . filter ( file => minimatch ( file , "**/*.ts" , { nocase : true } ) ) . map ( file => this . $fs . deleteFile ( file ) ) ;
89- Future . wait ( deleteFilesFutures ) ;
86+ // Remove .ts files
87+ let allFiles = this . $fs . enumerateFilesInDirectorySync ( tnsCoreModulesResourcePath ) ;
88+ let deleteFilesFutures = allFiles . filter ( file => minimatch ( file , "**/*.ts" , { nocase : true } ) ) . map ( file => this . $fs . deleteFile ( file ) ) ;
89+ Future . wait ( deleteFilesFutures ) ;
9090
91- shelljs . cp ( "-Rf" , path . join ( tnsCoreModulesResourcePath , "*" ) , this . outputRoot ) ;
92- this . $fs . deleteDirectory ( tnsCoreModulesResourcePath ) . wait ( ) ;
93- }
94- } ) ;
91+ shelljs . cp ( "-Rf" , path . join ( tnsCoreModulesResourcePath , "*" ) , this . outputRoot ) ;
92+ this . $fs . deleteDirectory ( tnsCoreModulesResourcePath ) . wait ( ) ;
93+ }
94+ } ) ;
9595
96- if ( ! _ . isEmpty ( this . dependencies ) ) {
97- this . $platformsData . getPlatformData ( platform ) . platformProjectService . afterPrepareAllPlugins ( ) . wait ( ) ;
96+ if ( ! _ . isEmpty ( this . dependencies ) ) {
97+ this . $platformsData . getPlatformData ( platform ) . platformProjectService . afterPrepareAllPlugins ( ) . wait ( ) ;
98+ }
9899 }
99- }
100100
101- private copyDependencyDir ( dependency : any ) : void {
101+ private copyDependencyDir ( dependency : any ) : void {
102102 let dependencyDir = path . dirname ( dependency . name || "" ) ;
103103 let insideNpmScope = / ^ @ / . test ( dependencyDir ) ;
104104 let targetDir = this . outputRoot ;
@@ -108,42 +108,47 @@ export class DestCopy implements IBroccoliPlugin {
108108 shelljs . mkdir ( "-p" , targetDir ) ;
109109 shelljs . cp ( "-Rf" , dependency . directory , targetDir ) ;
110110 shelljs . rm ( "-rf" , path . join ( targetDir , dependency . name , "node_modules" ) ) ;
111- }
111+ }
112112
113- public rebuild ( treeDiff : IDiffResult ) : void {
114- this . rebuildChangedDirectories ( treeDiff . changedDirectories , "" ) ;
113+ public rebuild ( treeDiff : IDiffResult ) : void {
114+ this . rebuildChangedDirectories ( treeDiff . changedDirectories , "" ) ;
115115
116- // Cache input tree
117- let projectFilePath = path . join ( this . projectDir , constants . PACKAGE_JSON_FILE_NAME ) ;
118- let projectFileContent = require ( projectFilePath ) ;
119- projectFileContent [ constants . NATIVESCRIPT_KEY_NAME ] [ constants . NODE_MODULE_CACHE_PATH_KEY_NAME ] = this . inputPath ;
120- fs . writeFileSync ( projectFilePath , JSON . stringify ( projectFileContent , null , "\t" ) , { encoding : "utf8" } ) ;
121- }
116+ // Cache input tree
117+ let projectFilePath = path . join ( this . projectDir , constants . PACKAGE_JSON_FILE_NAME ) ;
118+ let projectFileContent = require ( projectFilePath ) ;
119+ projectFileContent [ constants . NATIVESCRIPT_KEY_NAME ] [ constants . NODE_MODULE_CACHE_PATH_KEY_NAME ] = this . inputPath ;
120+ fs . writeFileSync ( projectFilePath , JSON . stringify ( projectFileContent , null , "\t" ) , { encoding : "utf8" } ) ;
121+ }
122122
123- private getDevDependencies ( projectDir : string ) : IDictionary < any > {
124- let projectFilePath = path . join ( projectDir , constants . PACKAGE_JSON_FILE_NAME ) ;
125- let projectFileContent = require ( projectFilePath ) ;
126- return projectFileContent . devDependencies || { } ;
127- }
123+ private getDevDependencies ( projectDir : string ) : IDictionary < any > {
124+ let projectFilePath = path . join ( projectDir , constants . PACKAGE_JSON_FILE_NAME ) ;
125+ let projectFileContent = require ( projectFilePath ) ;
126+ return projectFileContent . devDependencies || { } ;
127+ }
128128
129- private enumeratePackageJsonFilesSync ( nodeModulesDirectoryPath : string , foundFiles ?: string [ ] ) : string [ ] {
129+ private enumeratePackageJsonFilesSync ( nodeModulesDirectoryPath : string , foundFiles ?: string [ ] ) : string [ ] {
130130 foundFiles = foundFiles || [ ] ;
131- if ( fs . existsSync ( nodeModulesDirectoryPath ) ) {
131+ if ( fs . existsSync ( nodeModulesDirectoryPath ) ) {
132132 let contents = fs . readdirSync ( nodeModulesDirectoryPath ) ;
133133 for ( let i = 0 ; i < contents . length ; ++ i ) {
134- let packageJsonFilePath = path . join ( nodeModulesDirectoryPath , contents [ i ] , constants . PACKAGE_JSON_FILE_NAME ) ;
134+ let moduleName = contents [ i ] ;
135+ let moduleDirectoryInNodeModules = path . join ( nodeModulesDirectoryPath , moduleName ) ;
136+ let packageJsonFilePath = path . join ( moduleDirectoryInNodeModules , constants . PACKAGE_JSON_FILE_NAME ) ;
135137 if ( fs . existsSync ( packageJsonFilePath ) ) {
136138 foundFiles . push ( packageJsonFilePath ) ;
137139 }
138140
139- let directoryPath = path . join ( nodeModulesDirectoryPath , contents [ i ] , constants . NODE_MODULES_FOLDER_NAME ) ;
141+ let directoryPath = path . join ( moduleDirectoryInNodeModules , constants . NODE_MODULES_FOLDER_NAME ) ;
140142 if ( fs . existsSync ( directoryPath ) ) {
141143 this . enumeratePackageJsonFilesSync ( directoryPath , foundFiles ) ;
144+ } else if ( fs . statSync ( moduleDirectoryInNodeModules ) . isDirectory ( ) ) {
145+ // Some modules can be grouped in one folder and we need to enumerate them too (e.g. @angular).
146+ this . enumeratePackageJsonFilesSync ( moduleDirectoryInNodeModules , foundFiles ) ;
142147 }
143148 }
144149 }
145150 return foundFiles ;
146- }
151+ }
147152}
148153
149154export default wrapBroccoliPlugin ( DestCopy ) ;
0 commit comments