-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnode-modules-dest-copy.ts
156 lines (134 loc) · 6.07 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
import * as fs from "fs";
import * as path from "path";
import * as semver from "semver";
import * as shelljs from "shelljs";
import * as constants from "../../constants";
import * as minimatch from "minimatch";
import Future = require("fibers/future");
export interface ILocalDependencyData extends IDependencyData {
directory: string;
}
export class NpmDependencyResolver {
constructor(
private projectDir: string
) {
}
private getDevDependencies(projectDir: string): IDictionary<any> {
let projectFilePath = path.join(projectDir, constants.PACKAGE_JSON_FILE_NAME);
let projectFileContent = require(projectFilePath);
return projectFileContent.devDependencies || {};
}
public resolveDependencies(changedDirectories: string[], platform: string): IDictionary<ILocalDependencyData> {
const devDependencies = this.getDevDependencies(this.projectDir);
const dependencies: IDictionary<ILocalDependencyData> = Object.create(null);
_.each(changedDirectories, changedDirectoryAbsolutePath => {
if (!devDependencies[path.basename(changedDirectoryAbsolutePath)]) {
let pathToPackageJson = path.join(changedDirectoryAbsolutePath, constants.PACKAGE_JSON_FILE_NAME);
let packageJsonFiles = fs.existsSync(pathToPackageJson) ? [pathToPackageJson] : [];
let nodeModulesFolderPath = path.join(changedDirectoryAbsolutePath, constants.NODE_MODULES_FOLDER_NAME);
packageJsonFiles = packageJsonFiles.concat(this.enumeratePackageJsonFilesSync(nodeModulesFolderPath));
_.each(packageJsonFiles, packageJsonFilePath => {
let fileContent = require(packageJsonFilePath);
if (!devDependencies[fileContent.name] && fileContent.name && fileContent.version) { // Don't flatten dev dependencies and flatten only dependencies with valid package.json
let currentDependency: ILocalDependencyData = {
name: fileContent.name,
version: fileContent.version,
directory: path.dirname(packageJsonFilePath),
nativescript: fileContent.nativescript
};
let addedDependency = dependencies[currentDependency.name];
if (addedDependency) {
if (semver.gt(currentDependency.version, addedDependency.version)) {
let currentDependencyMajorVersion = semver.major(currentDependency.version);
let addedDependencyMajorVersion = semver.major(addedDependency.version);
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}`;
let logger = $injector.resolve("$logger");
currentDependencyMajorVersion === addedDependencyMajorVersion ? logger.out(message) : logger.warn(message);
dependencies[currentDependency.name] = currentDependency;
}
} else {
dependencies[currentDependency.name] = currentDependency;
}
}
});
}
});
return dependencies;
}
private enumeratePackageJsonFilesSync(nodeModulesDirectoryPath: string, foundFiles?: string[]): string[] {
foundFiles = foundFiles || [];
if (fs.existsSync(nodeModulesDirectoryPath)) {
let contents = fs.readdirSync(nodeModulesDirectoryPath);
for (let i = 0; i < contents.length; ++i) {
let moduleName = contents[i];
let moduleDirectoryInNodeModules = path.join(nodeModulesDirectoryPath, moduleName);
let packageJsonFilePath = path.join(moduleDirectoryInNodeModules, constants.PACKAGE_JSON_FILE_NAME);
if (fs.existsSync(packageJsonFilePath)) {
foundFiles.push(packageJsonFilePath);
}
let directoryPath = path.join(moduleDirectoryInNodeModules, constants.NODE_MODULES_FOLDER_NAME);
if (fs.existsSync(directoryPath)) {
this.enumeratePackageJsonFilesSync(directoryPath, foundFiles);
} else if (fs.statSync(moduleDirectoryInNodeModules).isDirectory()) {
// Scoped modules (e.g. @angular) are grouped in a subfolder and we need to enumerate them too.
this.enumeratePackageJsonFilesSync(moduleDirectoryInNodeModules, foundFiles);
}
}
}
return foundFiles;
}
}
export class TnsModulesCopy {
constructor(
private outputRoot: string,
private $fs: IFileSystem
) {
}
public copyModules(dependencies: IDictionary<ILocalDependencyData>, platform: string): void {
_.each(dependencies, dependency => {
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 deleteFilesFutures = allFiles.filter(file => minimatch(file, "**/*.ts", { nocase: true })).map(file => this.$fs.deleteFile(file));
Future.wait(deleteFilesFutures);
shelljs.cp("-Rf", path.join(tnsCoreModulesResourcePath, "*"), this.outputRoot);
this.$fs.deleteDirectory(tnsCoreModulesResourcePath).wait();
}
});
}
private copyDependencyDir(dependency: any): void {
let dependencyDir = path.dirname(dependency.name || "");
let insideNpmScope = /^@/.test(dependencyDir);
let targetDir = this.outputRoot;
if (insideNpmScope) {
targetDir = path.join(this.outputRoot, dependencyDir);
}
shelljs.mkdir("-p", targetDir);
shelljs.cp("-Rf", dependency.directory, targetDir);
shelljs.rm("-rf", path.join(targetDir, dependency.name, "node_modules"));
}
}
export class NpmPluginPrepare {
constructor(
private $fs: IFileSystem,
private $pluginsService: IPluginsService,
private $platformsData: IPlatformsData
) {
}
public preparePlugins(dependencies: IDictionary<IDependencyData>, platform: string): void {
if (_.isEmpty(dependencies)) {
return;
}
this.$platformsData.getPlatformData(platform).platformProjectService.beforePrepareAllPlugins(dependencies).wait();
_.each(dependencies, dependency => {
let isPlugin = !!dependency.nativescript;
if (isPlugin) {
console.log("preparing: " + dependency.name);
this.$pluginsService.prepare(dependency, platform).wait();
}
});
this.$platformsData.getPlatformData(platform).platformProjectService.afterPrepareAllPlugins().wait();
}
}