1- import * as fs from "fs" ;
21import * as path from "path" ;
3- import * as semver from "semver" ;
42import * as shelljs from "shelljs" ;
53import * as constants from "../../constants" ;
64import * as minimatch from "minimatch" ;
@@ -10,96 +8,17 @@ export interface ILocalDependencyData extends IDependencyData {
108 directory : string ;
119}
1210
13- export class NpmDependencyResolver {
14- constructor (
15- private projectDir : string
16- ) {
17- }
18-
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 ) ;
28-
29- _ . each ( changedDirectories , changedDirectoryAbsolutePath => {
30- if ( ! devDependencies [ path . basename ( changedDirectoryAbsolutePath ) ] ) {
31- let pathToPackageJson = path . join ( changedDirectoryAbsolutePath , constants . PACKAGE_JSON_FILE_NAME ) ;
32- let packageJsonFiles = fs . existsSync ( pathToPackageJson ) ? [ pathToPackageJson ] : [ ] ;
33- let nodeModulesFolderPath = path . join ( changedDirectoryAbsolutePath , constants . NODE_MODULES_FOLDER_NAME ) ;
34- packageJsonFiles = packageJsonFiles . concat ( this . enumeratePackageJsonFilesSync ( nodeModulesFolderPath ) ) ;
35-
36- _ . each ( packageJsonFiles , packageJsonFilePath => {
37- let fileContent = require ( packageJsonFilePath ) ;
38-
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 = {
41- name : fileContent . name ,
42- version : fileContent . version ,
43- directory : path . dirname ( packageJsonFilePath ) ,
44- nativescript : fileContent . nativescript
45- } ;
46-
47- let addedDependency = dependencies [ currentDependency . name ] ;
48- if ( addedDependency ) {
49- if ( semver . gt ( currentDependency . version , addedDependency . version ) ) {
50- let currentDependencyMajorVersion = semver . major ( currentDependency . version ) ;
51- let addedDependencyMajorVersion = semver . major ( addedDependency . version ) ;
52-
53- let message = `The dependency located at ${ addedDependency . directory } with version ${ addedDependency . version } will be replaced with dependency located at ${ currentDependency . directory } with version ${ currentDependency . version } ` ;
54- let logger = $injector . resolve ( "$logger" ) ;
55- currentDependencyMajorVersion === addedDependencyMajorVersion ? logger . out ( message ) : logger . warn ( message ) ;
56-
57- dependencies [ currentDependency . name ] = currentDependency ;
58- }
59- } else {
60- dependencies [ currentDependency . name ] = currentDependency ;
61- }
62- }
63- } ) ;
64- }
65- } ) ;
66- return dependencies ;
67- }
68-
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- }
80-
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- }
88- }
89- }
90- return foundFiles ;
91- }
92- }
93-
9411export class TnsModulesCopy {
9512 constructor (
9613 private outputRoot : string ,
9714 private $fs : IFileSystem
9815 ) {
9916 }
10017
101- public copyModules ( dependencies : IDictionary < ILocalDependencyData > , platform : string ) : void {
102- _ . each ( dependencies , dependency => {
18+ public copyModules ( dependencies : any [ ] , platform : string ) : void {
19+ for ( let entry in dependencies ) {
20+ let dependency = dependencies [ entry ] ;
21+
10322 this . copyDependencyDir ( dependency ) ;
10423
10524 if ( dependency . name === constants . TNS_CORE_MODULES_NAME ) {
@@ -110,22 +29,23 @@ export class TnsModulesCopy {
11029 let deleteFilesFutures = allFiles . filter ( file => minimatch ( file , "**/*.ts" , { nocase : true } ) ) . map ( file => this . $fs . deleteFile ( file ) ) ;
11130 Future . wait ( deleteFilesFutures ) ;
11231
113- shelljs . cp ( "-Rf" , path . join ( tnsCoreModulesResourcePath , "*" ) , this . outputRoot ) ;
114- this . $fs . deleteDirectory ( tnsCoreModulesResourcePath ) . wait ( ) ;
32+ shelljs . rm ( "-rf" , path . join ( tnsCoreModulesResourcePath , "node_modules" ) ) ;
11533 }
116- } ) ;
34+ }
11735 }
11836
11937 private copyDependencyDir ( dependency : any ) : void {
120- let dependencyDir = path . dirname ( dependency . name || "" ) ;
121- let insideNpmScope = / ^ @ / . test ( dependencyDir ) ;
122- let targetDir = this . outputRoot ;
123- if ( insideNpmScope ) {
124- targetDir = path . join ( this . outputRoot , dependencyDir ) ;
38+ if ( dependency . depth === 0 ) {
39+ let isScoped = dependency . name . indexOf ( "@" ) === 0 ;
40+ let targetDir = this . outputRoot ;
41+
42+ if ( isScoped ) {
43+ targetDir = path . join ( this . outputRoot , dependency . name . substring ( 0 , dependency . name . indexOf ( "/" ) ) ) ;
44+ }
45+
46+ shelljs . mkdir ( "-p" , targetDir ) ;
47+ shelljs . cp ( "-Rf" , dependency . directory , targetDir ) ;
12548 }
126- shelljs . mkdir ( "-p" , targetDir ) ;
127- shelljs . cp ( "-Rf" , dependency . directory , targetDir ) ;
128- shelljs . rm ( "-rf" , path . join ( targetDir , dependency . name , "node_modules" ) ) ;
12949 }
13050}
13151
0 commit comments