/
node-module.ts
242 lines (198 loc) · 5.55 KB
/
node-module.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import fs, { Stats } from 'fs';
import path from 'path';
import { PackageJson } from 'type-fest';
import getFolderSize from 'get-folder-size';
import { readLinkSilent, npmView } from '../utils';
import Workspace from './workspace';
import semverMaxSatisfying from 'semver/ranges/max-satisfying';
import semver from 'semver';
export type RemoteData = {
time: Record<string | 'modified' | 'created', string>;
versions: Array<string>;
'dist-tags': Record<'latest' | string, string>;
};
export default class NodeModule {
name: string;
nodeModulesPath: string;
parent?: NodeModule;
workspace: Workspace;
_remoteData: RemoteData | null;
_packageJson: PackageJson | null;
_stats: Stats | null;
_yarnRequiredBy: Set<string> | null;
_symlink: string | null;
constructor({
nodeModulesPath,
name,
parent,
workspace,
}: {
nodeModulesPath: string;
name: string;
parent?: NodeModule;
workspace: Workspace;
}) {
this.name = name;
this.nodeModulesPath = nodeModulesPath;
this.parent = parent;
this.workspace = workspace;
this._stats = null;
this._remoteData = null;
this._packageJson = null;
this._yarnRequiredBy = null;
this._symlink = null;
}
get packageJson(): PackageJson {
if (!this._packageJson) {
return this.load();
}
return this._packageJson;
}
get remoteData(): RemoteData {
if (!this._remoteData) {
return this.loadRemoteData();
}
return this._remoteData;
}
get version(): string {
if (!this.packageJson.version) {
throw new Error(`No version property for package ${this.name}`);
}
return this.packageJson.version;
}
get path() {
return path.join(this.nodeModulesPath, this.name);
}
get stats() {
if (!this._stats) {
this._stats = fs.statSync(this.path);
}
return this._stats;
}
get symlink() {
if (!this._symlink) {
const modulePath = path.join(this.nodeModulesPath, this.name);
this._symlink = readLinkSilent(modulePath);
}
return this._symlink;
}
get requiredBy(): Array<string> {
if ((this.packageJson as any)._requiredBy) {
return (this.packageJson as any)._requiredBy as Array<string>;
}
if (this.workspace.isYarn) {
this.workspace.modulesMap.assignYarnRequiredBy();
if (this._yarnRequiredBy) {
return [...this._yarnRequiredBy!];
}
}
return [];
}
get latestVersion(): string {
return this.remoteData['dist-tags'].latest;
}
get latestLastModified(): Date {
return new Date(this.remoteData.time[this.latestVersion]);
}
get maxVersionInSameMajor(): string | null {
return semverMaxSatisfying(
this.remoteData.versions,
`^${semver.clean(this.version)}`
);
}
get maxVersionInSameMajorLastModified(): Date | null {
if (!this.maxVersionInSameMajor) return null;
return new Date(this.remoteData.time[this.maxVersionInSameMajor]);
}
get releaseDate(): Date {
if (!this.packageJson.version) {
throw new Error(
`missing "version" in package.json for module: ${this.path}`
);
}
return new Date(this.remoteData.time[this.packageJson.version]);
}
loadRemoteData() {
this._remoteData = npmView(this.name);
return this._remoteData;
}
addYarnRequiredByDependency(moduleName: string) {
if (!this._yarnRequiredBy) {
this._yarnRequiredBy = new Set();
}
this._yarnRequiredBy.add(moduleName);
}
/**
* return the size of the modules in mb
*/
getSize(): Promise<number> {
return new Promise((resolve) => {
getFolderSize(this.path, (err, size) => {
if (err) {
console.error(err);
}
resolve(Number((size / 1024 / 1024).toFixed(2)));
});
});
}
get isResolutions(): boolean {
const resolutions = this.workspace?.resolutionsList;
if (!Array.isArray(resolutions)) {
return false;
}
return resolutions.includes(this.name);
}
get isbundledDependency() {
const bundledDependencies = this.parent?.packageJson.bundledDependencies;
const bundleDependencies = this.parent?.packageJson.bundleDependencies;
if (
!Array.isArray(bundledDependencies) &&
!Array.isArray(bundleDependencies)
) {
return false;
}
return (
bundledDependencies?.includes(this.name) ||
bundleDependencies?.includes(this.name)
);
}
get whyInfo() {
const requiredByInfo = this.requiredBy;
if (requiredByInfo) {
return requiredByInfo.map((modulePath) => {
if (modulePath === '/') {
return 'dependencies';
} else if (modulePath === '#DEV:/') {
return 'devDependencies';
} else if (modulePath === '#USER') {
return `npm install ${this.name}`;
}
// npm sometimes starts with requiredBy with `/`
return modulePath.startsWith('/') ? modulePath.slice(1) : modulePath;
});
}
return [];
}
toObject(): Record<string, any> {
return {
name: this.name,
path: this.path,
version: this.version,
parent: this.parent && this.parent.toObject(),
};
}
load(): PackageJson {
const modulePath = path.join(this.nodeModulesPath, this.name);
const packageJsonPath = path.join(modulePath, 'package.json');
try {
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
this._packageJson = pkg;
return pkg;
} catch (error: any) {
if (error.code === 'ENOENT') {
throw new Error(`Couldn't find "package.json" for module ${this.name}`);
}
throw error;
}
}
}