forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-modules-dependencies-builder.ts
169 lines (146 loc) · 4.22 KB
/
node-modules-dependencies-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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as path from "path";
import { PACKAGE_JSON_FILE_NAME } from "../../constants";
import { INodeModulesDependenciesBuilder } from "../../definitions/platform";
import { IDependencyData } from "../../declarations";
import { IFileSystem } from "../../common/declarations";
import * as _ from "lodash";
import { injector } from "../../common/yok";
import { resolvePackagePath } from "@rigor789/resolve-package-path";
interface IDependencyDescription {
parent: IDependencyDescription;
parentDir: string;
name: string;
depth: number;
}
export class NodeModulesDependenciesBuilder
implements INodeModulesDependenciesBuilder {
public constructor(private $fs: IFileSystem) {}
public getProductionDependencies(
projectPath: string,
ignore?: string[]
): IDependencyData[] {
const projectPackageJsonPath = path.join(
projectPath,
PACKAGE_JSON_FILE_NAME
);
const packageJsonContent = this.$fs.readJson(projectPackageJsonPath);
const dependencies = packageJsonContent && packageJsonContent.dependencies;
const resolvedDependencies: IDependencyData[] = [];
const queue: IDependencyDescription[] = _.keys(dependencies).map(
(dependencyName) => ({
parent: null,
parentDir: projectPath,
name: dependencyName,
depth: 0,
})
);
while (queue.length) {
const currentModule = queue.shift();
const resolvedDependency = this.findModule(
currentModule,
resolvedDependencies,
projectPath
);
if (
resolvedDependency &&
!_.some(
resolvedDependencies,
(r) => r.directory === resolvedDependency.directory
)
) {
_.each(resolvedDependency.dependencies, (d) => {
const dependency: IDependencyDescription = {
parent: currentModule,
name: d,
parentDir: resolvedDependency.directory,
depth: resolvedDependency.depth + 1,
};
const shouldAdd = !_.some(
queue,
(element) =>
element.parent === dependency.parent &&
element.name === dependency.name &&
element.parentDir === dependency.parentDir &&
element.depth === dependency.depth
);
if (shouldAdd) {
queue.push(dependency);
}
});
resolvedDependencies.push(resolvedDependency);
}
}
if (ignore && ignore.length > 0) {
return resolvedDependencies.filter((d) => ignore.indexOf(d.name) === -1);
}
return resolvedDependencies;
}
private findModule(
depDescription: IDependencyDescription,
resolvedDependencies: IDependencyData[],
rootPath: string
): IDependencyData {
try {
const parentModulesPath =
depDescription?.parentDir ?? depDescription?.parent?.parentDir;
let modulePath: string = resolvePackagePath(depDescription.name, {
paths: [parentModulesPath],
});
// perhaps traverse up the tree here?
if (!modulePath) {
// fallback to searching in the root path
modulePath = resolvePackagePath(depDescription.name, {
paths: [rootPath],
});
}
// if we failed to find the module...
if (!modulePath) {
return null;
}
// if we already resolved this dependency, we return null to avoid a duplicate resolution
if (
resolvedDependencies.some((r) => {
return r.name === depDescription.name && r.directory === modulePath;
})
) {
return null;
}
return this.getDependencyData(
depDescription.name,
modulePath,
depDescription.depth
);
} catch (err) {
return null;
}
}
private getDependencyData(
name: string,
directory: string,
depth: number
): IDependencyData {
const dependency: IDependencyData = {
name,
directory,
depth,
version: null,
};
const packageJsonPath = path.join(directory, PACKAGE_JSON_FILE_NAME);
const packageJsonExists = this.$fs.getLsStats(packageJsonPath).isFile();
if (packageJsonExists) {
const packageJsonContents = this.$fs.readJson(packageJsonPath);
dependency.version = packageJsonContents.version;
if (!!packageJsonContents.nativescript) {
// add `nativescript` property, necessary for resolving plugins
dependency.nativescript = packageJsonContents.nativescript;
}
dependency.dependencies = _.keys(packageJsonContents.dependencies);
return dependency;
}
return null;
}
}
injector.register(
"nodeModulesDependenciesBuilder",
NodeModulesDependenciesBuilder
);