-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnode-modules-builder.ts
155 lines (131 loc) · 5.16 KB
/
node-modules-builder.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
import * as constants from "../../../lib/constants";
import * as path from "path";
import * as shelljs from "shelljs";
import { TnsModulesCopy, NpmPluginPrepare } from "./node-modules-dest-copy";
import { NodeModulesDependenciesBuilder } from "./node-modules-dependencies-builder";
import { sleep, deferPromise } from "../../../lib/common/helpers";
let glob = require("glob");
export class NodeModulesBuilder implements INodeModulesBuilder {
constructor(private $fs: IFileSystem,
private $projectData: IProjectData,
private $injector: IInjector,
private $lockfile: ILockFile,
private $options: IOptions
) { }
public async getChangedNodeModules(absoluteOutputPath: string, platform: string, lastModifiedTime?: Date): Promise<any> {
let projectDir = this.$projectData.projectDir;
let isNodeModulesModified = false;
let nodeModulesPath = path.join(projectDir, constants.NODE_MODULES_FOLDER_NAME);
let nodeModules: any = {};
if (lastModifiedTime) {
let defer = deferPromise();
let match = new glob.Glob("node_modules/**", {
cwd: projectDir,
follow: true,
stat: true
}, (er: Error, files: string[]) => {
while (this.$lockfile.check()) {
sleep(10);
}
this.$lockfile.lock();
if (er) {
if (!defer.isResolved()) {
defer.reject(er);
}
this.$lockfile.unlock();
match.abort();
return;
}
for (let i = 0, l = files.length; i < l; i++) {
let file = files[i],
resolvedPath = path.join(projectDir, file),
relativePath = path.relative(projectDir, resolvedPath);
let stat = match.statCache[resolvedPath] || match.statCache[relativePath];
if (!stat) {
match.statCache[resolvedPath] = stat = this.$fs.getFsStats(resolvedPath);
}
if (stat.mtime <= lastModifiedTime) {
continue;
}
if (file === constants.NODE_MODULES_FOLDER_NAME) {
isNodeModulesModified = true;
this.$lockfile.unlock();
match.abort();
if (!defer.isResolved()) {
defer.resolve();
}
return;
}
let rootModuleName = path.normalize(file).split(path.sep)[1];
let rootModuleFullPath = path.join(nodeModulesPath, rootModuleName);
nodeModules[rootModuleFullPath] = rootModuleFullPath;
}
this.$lockfile.unlock();
});
match.on("end", () => {
if (!defer.isResolved()) {
let intervalId = setInterval(() => {
if (!this.$lockfile.check() || defer.isResolved()) {
if (!defer.isResolved()) {
defer.resolve();
}
clearInterval(intervalId);
}
}, 100);
}
});
await defer.promise;
}
if (isNodeModulesModified && this.$fs.exists(absoluteOutputPath)) {
let currentPreparedTnsModules = this.$fs.readDirectory(absoluteOutputPath);
let tnsModulesPath = path.join(projectDir, constants.NODE_MODULES_FOLDER_NAME, constants.TNS_CORE_MODULES_NAME);
let tnsModulesInApp = this.$fs.readDirectory(tnsModulesPath);
let modulesToDelete = _.difference(currentPreparedTnsModules, tnsModulesInApp);
_.each(modulesToDelete, moduleName => this.$fs.deleteDirectory(path.join(absoluteOutputPath, moduleName)));
}
if (!lastModifiedTime || isNodeModulesModified) {
this.expandScopedModules(nodeModulesPath, nodeModules);
}
return nodeModules;
}
private expandScopedModules(nodeModulesPath: string, nodeModules: IStringDictionary): void {
let nodeModulesDirectories = this.$fs.exists(nodeModulesPath) ? this.$fs.readDirectory(nodeModulesPath) : [];
_.each(nodeModulesDirectories, nodeModuleDirectoryName => {
let isNpmScope = /^@/.test(nodeModuleDirectoryName);
let nodeModuleFullPath = path.join(nodeModulesPath, nodeModuleDirectoryName);
if (isNpmScope) {
this.expandScopedModules(nodeModuleFullPath, nodeModules);
} else {
nodeModules[nodeModuleFullPath] = nodeModuleFullPath;
}
});
}
public async prepareNodeModules(absoluteOutputPath: string, platform: string, lastModifiedTime: Date): Promise<void> {
if (!this.$fs.exists(absoluteOutputPath)) {
// Force copying if the destination doesn't exist.
lastModifiedTime = null;
}
let dependenciesBuilder = this.$injector.resolve(NodeModulesDependenciesBuilder, {});
let productionDependencies = dependenciesBuilder.getProductionDependencies(this.$projectData.projectDir);
let prodDependenciesArr = [];
for (let i in productionDependencies) {
prodDependenciesArr.push(productionDependencies[i].name);
}
let prodDependenciesFilePath = path.join(this.$projectData.projectDir, "platforms", "android", "productionDependencies.json");
this.$fs.writeJson(prodDependenciesFilePath, prodDependenciesArr);
if (!this.$options.bundle) {
const tnsModulesCopy = this.$injector.resolve(TnsModulesCopy, {
outputRoot: absoluteOutputPath
});
tnsModulesCopy.copyModules(productionDependencies, platform);
} else {
this.cleanNodeModules(absoluteOutputPath, platform);
}
const npmPluginPrepare: NpmPluginPrepare = this.$injector.resolve(NpmPluginPrepare);
await npmPluginPrepare.preparePlugins(productionDependencies, platform);
}
public cleanNodeModules(absoluteOutputPath: string, platform: string): void {
shelljs.rm("-rf", absoluteOutputPath);
}
}
$injector.register("nodeModulesBuilder", NodeModulesBuilder);