-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnode-modules-dest-copy.ts
129 lines (108 loc) · 4.23 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
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: any[], platform: string): void {
for (let entry in dependencies) {
let dependency = dependencies[entry];
this.copyDependencyDir(dependency);
if (dependency.name === constants.TNS_CORE_MODULES_NAME) {
let tnsCoreModulesResourcePath = path.join(this.outputRoot, constants.TNS_CORE_MODULES_NAME);
// Remove .ts files
let allFiles = this.$fs.enumerateFilesInDirectorySync(tnsCoreModulesResourcePath);
let 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, "node_modules"));
}
}
}
private copyDependencyDir(dependency: any): void {
if (dependency.depth === 0) {
let isScoped = dependency.name.indexOf("@") === 0;
let targetDir = this.outputRoot;
if (isScoped) {
targetDir = path.join(this.outputRoot, dependency.name.substring(0, dependency.name.indexOf("/")));
}
shelljs.mkdir("-p", targetDir);
shelljs.cp("-Rf", dependency.directory, targetDir);
//remove platform-specific files (processed separately by plugin services)
const targetPackageDir = path.join(targetDir, dependency.name);
shelljs.rm("-rf", path.join(targetPackageDir, "platforms"));
}
}
}
export class NpmPluginPrepare {
constructor(
private $fs: IFileSystem,
private $pluginsService: IPluginsService,
private $platformsData: IPlatformsData
) {
}
protected beforePrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
this.$platformsData.getPlatformData(platform).platformProjectService.beforePrepareAllPlugins(dependencies).wait();
}
protected afterPrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
this.$platformsData.getPlatformData(platform).platformProjectService.afterPrepareAllPlugins().wait();
this.writePreparedDependencyInfo(dependencies, platform);
}
private writePreparedDependencyInfo(dependencies: IDictionary<IDependencyData>, platform: string): void {
let prepareData: IDictionary<boolean> = {};
_.values(dependencies).forEach(d => {
prepareData[d.name] = true;
});
this.$fs.createDirectory(this.preparedPlatformsDir(platform));
this.$fs.writeJson(this.preparedPlatformsFile(platform), prepareData, " ", "utf8");
}
private preparedPlatformsDir(platform: string): string {
const platformRoot = this.$platformsData.getPlatformData(platform).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): string {
return path.join(this.preparedPlatformsDir(platform), "prepared-platforms.json");
}
protected getPreviouslyPreparedDependencies(platform: string): IDictionary<boolean> {
if (!this.$fs.exists(this.preparedPlatformsFile(platform))) {
return {};
}
return this.$fs.readJson(this.preparedPlatformsFile(platform), "utf8");
}
private allPrepared(dependencies: IDictionary<IDependencyData>, platform: string): boolean {
let result = true;
const previouslyPrepared = this.getPreviouslyPreparedDependencies(platform);
_.values(dependencies).forEach(d => {
if (!previouslyPrepared[d.name]) {
result = false;
}
});
return result;
}
public preparePlugins(dependencies: IDictionary<IDependencyData>, platform: string): void {
if (_.isEmpty(dependencies) || this.allPrepared(dependencies, platform)) {
return;
}
this.beforePrepare(dependencies, platform);
_.each(dependencies, dependency => {
let isPlugin = !!dependency.nativescript;
if (isPlugin) {
this.$pluginsService.prepare(dependency, platform).wait();
}
});
this.afterPrepare(dependencies, platform);
}
}