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