-
-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathModulesCollector.ts
More file actions
144 lines (121 loc) · 4.09 KB
/
Copy pathModulesCollector.ts
File metadata and controls
144 lines (121 loc) · 4.09 KB
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
import { logger } from '@sentry/utils';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { posix, sep } from 'path';
logger.enable();
// eslint-disable-next-line @typescript-eslint/unbound-method
const { dirname, join, resolve, sep: posixSep } = posix;
interface Package {
name?: string,
version?: string,
}
/**
* Collects JS modules from source paths.
*/
export default class ModulesCollector {
/** Collect method */
public static collect(sources: unknown[], modulesPaths: string[]): Record<string, string> {
const normalizedModulesPaths = modulesPaths.map((modulesPath) => resolve(modulesPath.split(sep).join(posixSep)));
const infos: Record<string, string> = {};
const seen: Record<string, true> = {};
sources.forEach((path: unknown) => {
if (typeof path !== 'string') {
return;
}
let dir = path; // included source file path
let candidate: Package | null = null;
/** Traverse directories upward in the search of all package.json files */
const upDirSearch = (): void => {
const parentDir = dir;
dir = dirname(parentDir);
if (normalizedModulesPaths.includes(resolve(dir))) {
if (candidate?.name && candidate?.version) {
infos[candidate.name] = candidate.version;
} else if (candidate?.name) {
infos[candidate.name] = 'unknown';
}
return;
}
if (
!dir ||
parentDir === dir ||
seen[dir]
) {
return;
}
seen[dir] = true;
const pkgPath = join(dir, 'package.json');
if (!existsSync(pkgPath)) {
// fast-forward if the package.json doesn't exist
return upDirSearch();
}
try {
const info: Package = JSON.parse(readFileSync(pkgPath, 'utf8'));
candidate = {
name: info.name,
version: info.version,
};
} catch (error) {
logger.error(`Failed to read ${pkgPath}`);
}
return upDirSearch(); // processed package.json file, continue up search
};
upDirSearch();
});
return infos;
}
/**
* Runs collection of modules.
*/
public static run({
sourceMapPath,
outputModulesPath,
modulesPaths,
collect,
}: Partial<{
sourceMapPath: string,
outputModulesPath: string,
modulesPaths: string[],
collect: (sources: unknown[], modulesPaths: string[]) => Record<string, string>,
}>): void {
if (!sourceMapPath) {
logger.error('First argument `source-map-path` is missing!');
return;
}
if (!outputModulesPath) {
logger.error('Second argument `modules-output-path` is missing!');
return;
}
if (!modulesPaths || modulesPaths.length === 0) {
logger.error('Third argument `modules-paths` is missing!');
return;
}
logger.info('Reading source map from', sourceMapPath);
logger.info('Saving modules to', outputModulesPath);
logger.info('Resolving modules from paths', outputModulesPath);
if (!existsSync(sourceMapPath)) {
logger.error(`Source map file does not exist at ${sourceMapPath}`);
return;
}
for (const modulesPath of modulesPaths) {
if (!existsSync(modulesPath)) {
logger.error(`Modules path does not exist at ${modulesPath}`);
return;
}
}
const map: { sources?: unknown } = JSON.parse(readFileSync(sourceMapPath, 'utf8'));
if (!map.sources || !Array.isArray(map.sources)) {
logger.error(`Modules not collected. No sources found in the source map (${sourceMapPath})!`);
return;
}
const sources: unknown[] = map.sources;
const modules = collect
? collect(sources, modulesPaths)
: ModulesCollector.collect(sources, modulesPaths);
const outputModulesDirPath = dirname(outputModulesPath);
if (!existsSync(outputModulesDirPath)) {
mkdirSync(outputModulesDirPath, { recursive: true });
}
writeFileSync(outputModulesPath, JSON.stringify(modules, null, 2));
logger.info(`Modules collected and saved to: ${outputModulesPath}`);
}
}