This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 658
/
Copy pathIntegrationLoader.ts
183 lines (159 loc) · 4.55 KB
/
IntegrationLoader.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
import {normalizeManifest} from "@internal/codec-js-manifest";
import {
SemverRange,
SemverVersion,
parseSemverRange,
satisfiesSemver,
stringifySemver,
} from "@internal/codec-semver";
import {Consumer, consumeUnknown} from "@internal/consume";
import {
AbsoluteFilePath,
AbsoluteFilePathMap,
createAbsoluteFilePath,
} from "@internal/path";
import {json} from "@internal/codec-config";
import {
DIAGNOSTIC_CATEGORIES,
createSingleDiagnosticsError,
decorateErrorWithDiagnostics,
descriptions,
} from "@internal/diagnostics";
import internalModule = require("module");
import {ProjectConfigIntegrations} from "@internal/project";
const requires: AbsoluteFilePathMap<NodeRequire> = new AbsoluteFilePathMap();
export function getRequire(path: AbsoluteFilePath): NodeRequire {
const existing = requires.get(path);
if (existing !== undefined) {
return existing;
}
const require: NodeRequire = internalModule.createRequire(path.join());
requires.set(path, require);
return require;
}
type IntegrationLoaderNormalizePayload<IntegrationName extends keyof ProjectConfigIntegrations> = {
consumer: Consumer;
cwd: AbsoluteFilePath;
version: undefined | SemverVersion;
opts?: Omit<ProjectConfigIntegrations[IntegrationName], "enabled">;
};
type IntegrationLoaderNormalize<
Value,
IntegrationName extends keyof ProjectConfigIntegrations
> = (payload: IntegrationLoaderNormalizePayload<IntegrationName>) => Value;
type IntegrationLoaderEntry<Value> = {
version: undefined | SemverVersion;
module: Value;
};
export default class IntegrationLoader<
Value,
IntegrationName extends keyof ProjectConfigIntegrations
> {
constructor(
{name, range, normalize}: {
name: string;
range?: string;
normalize: IntegrationLoaderNormalize<Value, IntegrationName>;
},
) {
this.loaded = new AbsoluteFilePathMap();
this.name = name;
this.normalize = normalize;
this.range =
range === undefined ? undefined : parseSemverRange({input: range});
}
private loaded: AbsoluteFilePathMap<IntegrationLoaderEntry<Value>>;
private normalize: IntegrationLoaderNormalize<Value, IntegrationName>;
private name: string;
private range: undefined | SemverRange;
private resolve(
id: string,
require: NodeRequire,
path: AbsoluteFilePath,
): string {
try {
return require.resolve(id);
} catch (err) {
if (err.code === "MODULE_NOT_FOUND") {
throw createSingleDiagnosticsError({
description: descriptions.INTEGRATIONS.NOT_FOUND(this.name),
location: {
path,
},
});
} else {
throw err;
}
}
}
public async wrap<T>(callback: () => Promise<T>): Promise<T> {
const beginError = new Error();
try {
return await callback();
} catch (err) {
throw decorateErrorWithDiagnostics(
err,
{
description: descriptions.INTEGRATIONS.LOAD(this.name),
cleanRelativeError: beginError,
},
);
}
}
public async load(
path: AbsoluteFilePath,
cwd: AbsoluteFilePath,
opts?: Omit<ProjectConfigIntegrations[IntegrationName], "enabled">,
): Promise<IntegrationLoaderEntry<Value>> {
const existing = this.loaded.get(path);
if (existing !== undefined) {
return existing;
}
// Try to resolve
const require = getRequire(path);
let version: undefined | SemverVersion = undefined;
// Validate range against the package version field
const expectedRange = this.range;
if (expectedRange !== undefined) {
const manifestPath = createAbsoluteFilePath(
this.resolve(`${this.name}/package.json`, require, path),
);
const jsonConsumer = json.consumeValue(
await manifestPath.readFileTextMeta(),
);
const versionProp = jsonConsumer.get("version");
const manifest = await normalizeManifest({
path: manifestPath,
consumer: jsonConsumer,
projects: [],
});
if (manifest.version === undefined) {
throw versionProp.unexpected(() =>
descriptions.INTEGRATIONS.MISSING_VERSION(this.name)
);
}
if (!satisfiesSemver(manifest.version, expectedRange)) {
throw versionProp.unexpected(() =>
descriptions.INTEGRATIONS.UNSUPPORTED_VERSION(
this.name,
stringifySemver(expectedRange),
)
);
}
version = manifest.version;
}
const filename = this.resolve(this.name, require, path);
const value: unknown = await this.wrap(() => {
return require(filename);
});
const consumer = consumeUnknown(
value,
DIAGNOSTIC_CATEGORIES["integration/load"],
this.name,
);
const module = this.normalize({consumer, cwd, version, opts});
const entry: IntegrationLoaderEntry<Value> = {version, module};
this.loaded.set(path, entry);
return entry;
}
}