1
- import * as path from "path" ;
2
- import * as shelljs from "shelljs" ;
3
- import * as constants from "../../constants" ;
4
- import * as minimatch from "minimatch" ;
5
-
6
- export interface ILocalDependencyData extends IDependencyData {
7
- directory : string ;
8
- }
9
-
10
- export class TnsModulesCopy {
11
- constructor (
12
- private outputRoot : string ,
13
- private $fs : IFileSystem ,
14
- private $pluginsService : IPluginsService
15
- ) {
16
- }
17
-
18
- public copyModules ( opts : { dependencies : IDependencyData [ ] , release : boolean } ) : void {
19
- const filePatternsToDelete = opts . release ? "**/*.ts" : "**/*.d.ts" ;
20
- for ( const entry in opts . dependencies ) {
21
- const dependency = opts . dependencies [ entry ] ;
22
-
23
- this . copyDependencyDir ( dependency , filePatternsToDelete ) ;
24
- }
25
- }
26
-
27
- private copyDependencyDir ( dependency : IDependencyData , filePatternsToDelete : string ) : void {
28
- if ( dependency . depth === 0 ) {
29
- const targetPackageDir = path . join ( this . outputRoot , dependency . name ) ;
30
-
31
- shelljs . mkdir ( "-p" , targetPackageDir ) ;
32
-
33
- const isScoped = dependency . name . indexOf ( "@" ) === 0 ;
34
- const destinationPath = isScoped ? path . join ( this . outputRoot , dependency . name . substring ( 0 , dependency . name . indexOf ( "/" ) ) ) : this . outputRoot ;
35
- shelljs . cp ( "-RfL" , dependency . directory , destinationPath ) ;
36
-
37
- // remove platform-specific files (processed separately by plugin services)
38
- shelljs . rm ( "-rf" , path . join ( targetPackageDir , "platforms" ) ) ;
39
-
40
- this . removeNonProductionDependencies ( dependency , targetPackageDir ) ;
41
- this . removeDependenciesPlatformsDirs ( targetPackageDir ) ;
42
- const allFiles = this . $fs . enumerateFilesInDirectorySync ( targetPackageDir ) ;
43
- allFiles . filter ( file => minimatch ( file , filePatternsToDelete , { nocase : true } ) ) . map ( file => this . $fs . deleteFile ( file ) ) ;
44
- }
45
- }
46
-
47
- private removeDependenciesPlatformsDirs ( dependencyDir : string ) : void {
48
- const dependenciesFolder = path . join ( dependencyDir , constants . NODE_MODULES_FOLDER_NAME ) ;
49
-
50
- if ( this . $fs . exists ( dependenciesFolder ) ) {
51
- const dependencies = this . getDependencies ( dependenciesFolder ) ;
52
-
53
- dependencies
54
- . forEach ( d => {
55
- const pathToDependency = path . join ( dependenciesFolder , d ) ;
56
- const pathToPackageJson = path . join ( pathToDependency , constants . PACKAGE_JSON_FILE_NAME ) ;
57
-
58
- if ( this . $pluginsService . isNativeScriptPlugin ( pathToPackageJson ) ) {
59
- this . $fs . deleteDirectory ( path . join ( pathToDependency , constants . PLATFORMS_DIR_NAME ) ) ;
60
- }
61
-
62
- this . removeDependenciesPlatformsDirs ( pathToDependency ) ;
63
- } ) ;
64
- }
65
- }
66
-
67
- private removeNonProductionDependencies ( dependency : IDependencyData , targetPackageDir : string ) : void {
68
- const packageJsonFilePath = path . join ( dependency . directory , constants . PACKAGE_JSON_FILE_NAME ) ;
69
- if ( ! this . $fs . exists ( packageJsonFilePath ) ) {
70
- return ;
71
- }
72
-
73
- const packageJsonContent = this . $fs . readJson ( packageJsonFilePath ) ;
74
- const productionDependencies = packageJsonContent . dependencies ;
75
-
76
- const dependenciesFolder = path . join ( targetPackageDir , constants . NODE_MODULES_FOLDER_NAME ) ;
77
- if ( this . $fs . exists ( dependenciesFolder ) ) {
78
- const dependencies = this . getDependencies ( dependenciesFolder ) ;
79
-
80
- dependencies . filter ( dir => ! productionDependencies || ! productionDependencies . hasOwnProperty ( dir ) )
81
- . forEach ( dir => shelljs . rm ( "-rf" , path . join ( dependenciesFolder , dir ) ) ) ;
82
- }
83
- }
84
-
85
- private getDependencies ( dependenciesFolder : string ) : string [ ] {
86
- const dependencies = _ . flatten ( this . $fs . readDirectory ( dependenciesFolder )
87
- . map ( dir => {
88
- if ( _ . startsWith ( dir , "@" ) ) {
89
- const pathToDir = path . join ( dependenciesFolder , dir ) ;
90
- const contents = this . $fs . readDirectory ( pathToDir ) ;
91
- return _ . map ( contents , subDir => `${ dir } /${ subDir } ` ) ;
92
- }
93
-
94
- return dir ;
95
- } ) ) ;
96
-
97
- return dependencies ;
98
- }
99
- }
100
-
101
1
export class NpmPluginPrepare {
102
2
constructor (
103
- private $fs : IFileSystem ,
104
3
private $pluginsService : IPluginsService ,
105
4
private $platformsData : IPlatformsData ,
106
- private $logger : ILogger
107
- ) {
108
- }
109
-
110
- protected async afterPrepare ( dependencies : IDependencyData [ ] , platform : string , projectData : IProjectData ) : Promise < void > {
111
- const prepareData : IDictionary < boolean > = { } ;
112
- _ . each ( dependencies , d => {
113
- prepareData [ d . name ] = true ;
114
- } ) ;
115
- this . $fs . createDirectory ( this . preparedPlatformsDir ( platform , projectData ) ) ;
116
- this . $fs . writeJson ( this . preparedPlatformsFile ( platform , projectData ) , prepareData , " " , "utf8" ) ;
117
- }
118
-
119
- private preparedPlatformsDir ( platform : string , projectData : IProjectData ) : string {
120
- const platformRoot = this . $platformsData . getPlatformData ( platform , projectData ) . projectRoot ;
121
- if ( / a n d r o i d / i. test ( platform ) ) {
122
- return path . join ( platformRoot , "build" , "intermediates" ) ;
123
- } else if ( / i o s / i. test ( platform ) ) {
124
- return path . join ( platformRoot , "build" ) ;
125
- } else {
126
- throw new Error ( "Invalid platform: " + platform ) ;
127
- }
128
- }
129
-
130
- private preparedPlatformsFile ( platform : string , projectData : IProjectData ) : string {
131
- return path . join ( this . preparedPlatformsDir ( platform , projectData ) , "prepared-platforms.json" ) ;
132
- }
133
-
134
- protected getPreviouslyPreparedDependencies ( platform : string , projectData : IProjectData ) : IDictionary < boolean > {
135
- if ( ! this . $fs . exists ( this . preparedPlatformsFile ( platform , projectData ) ) ) {
136
- return { } ;
137
- }
138
- return this . $fs . readJson ( this . preparedPlatformsFile ( platform , projectData ) , "utf8" ) ;
139
- }
5
+ ) { }
140
6
141
- private allPrepared ( dependencies : IDependencyData [ ] , platform : string , projectData : IProjectData ) : boolean {
142
- let result = true ;
143
- const previouslyPrepared = this . getPreviouslyPreparedDependencies ( platform , projectData ) ;
144
- _ . each ( dependencies , d => {
145
- if ( ! previouslyPrepared [ d . name ] ) {
146
- result = false ;
147
- }
148
- } ) ;
149
- return result ;
150
- }
151
-
152
- public async preparePlugins ( dependencies : IDependencyData [ ] , platform : string , projectData : IProjectData , projectFilesConfig : IProjectFilesConfig ) : Promise < void > {
7
+ public async preparePlugins ( dependencies : IDependencyData [ ] , platform : string , projectData : IProjectData ) : Promise < void > {
153
8
if ( _ . isEmpty ( dependencies ) ) {
154
9
return ;
155
10
}
@@ -164,29 +19,5 @@ export class NpmPluginPrepare {
164
19
await this . $pluginsService . preparePluginNativeCode ( pluginData , platform , projectData ) ;
165
20
}
166
21
}
167
-
168
- await this . afterPrepare ( dependencies , platform , projectData ) ;
169
- }
170
-
171
- public async prepareJSPlugins ( dependencies : IDependencyData [ ] , platform : string , projectData : IProjectData , projectFilesConfig : IProjectFilesConfig ) : Promise < void > {
172
- if ( _ . isEmpty ( dependencies ) || this . allPrepared ( dependencies , platform , projectData ) ) {
173
- return ;
174
- }
175
-
176
- for ( const dependencyKey in dependencies ) {
177
- const dependency = dependencies [ dependencyKey ] ;
178
- const isPlugin = ! ! dependency . nativescript ;
179
- if ( isPlugin ) {
180
- platform = platform . toLowerCase ( ) ;
181
- const pluginData = this . $pluginsService . convertToPluginData ( dependency , projectData . projectDir ) ;
182
- const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
183
- const appFolderExists = this . $fs . exists ( path . join ( platformData . appDestinationDirectoryPath , constants . APP_FOLDER_NAME ) ) ;
184
- if ( appFolderExists ) {
185
- this . $pluginsService . preparePluginScripts ( pluginData , platform , projectData , projectFilesConfig ) ;
186
- // Show message
187
- this . $logger . out ( `Successfully prepared plugin ${ pluginData . name } for ${ platform } .` ) ;
188
- }
189
- }
190
- }
191
22
}
192
23
}
0 commit comments