-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.mjs
More file actions
164 lines (154 loc) · 3.91 KB
/
config.mjs
File metadata and controls
164 lines (154 loc) · 3.91 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { makeRe } from "minimatch";
const {
Error,
Object: { hasOwn, getOwnPropertyNames },
} = globalThis;
/**
* @type {(
* string: string,
* ) => string}
*/
const trim = (string) => string.trim();
/**
* @type {(
* glob: string,
* ) => RegExp}
*/
const compileGlobRegExp = (glob) => {
const regexp = makeRe(glob);
if (!regexp) {
throw new Error(`Invalid glob pattern: ${glob}`);
}
return regexp;
};
/**
* @type {(
* globs: string,
* ) => RegExp[]}
*/
const compileGlobRegExpArray = (globs) => {
if (globs.trim() === "") {
return [];
} else {
return globs.split(",").map(trim).map(compileGlobRegExp);
}
};
/**
* @type {<X extends string>(
* value: string,
* valids: X[],
* name: string,
* ) => X}
*/
const getEnumeration = (value, valids, location) => {
for (const valid of valids) {
if (valid.toLowerCase() === value.toLowerCase()) {
return valid;
}
}
throw new Error(`Invalid enum value for ${location}, got: ${value}`);
};
/**
* @type {(
* value: string,
* name: string,
* ) => boolean}
*/
const getBoolean = (value, location) => {
value = value.toLowerCase();
if (value === "true" || value === "1" || value === "on") {
return true;
} else if (value === "false" || value === "0" || value === "off") {
return false;
} else {
throw new Error(`Invalid boolean value for ${location}, got: ${value}`);
}
};
/**
* @type {["internal", "external"]}
*/
const regions = ["internal", "external"];
/**
* @type {(
* config: import("./config.d.ts").Config,
* ) => string[]}
*/
export const listConfigWarning = ({
global_object,
selection,
global_dynamic_code,
}) => {
/** @type {string[]} */
const warnings = [];
if (global_object === "internal") {
if (selection !== null) {
warnings.push(
"Internalizing the global object (and the global declarative record) requires to instrument every single files, " +
"thus selecting them is unsafe and LINVAIL_INCLUDE and LINVAIL_EXCLUDE should respectively be set to '**/*' and ''.",
);
}
if (global_dynamic_code !== "internal") {
warnings.push(
"Internalizing the global object (and the global declarative record) requires to instrument global dynamic code.",
);
}
}
return warnings;
};
const default_config = {
LINVAIL_COUNT: "0",
LINVAIL_GLOBAL_DYNAMIC_CODE: "internal",
LINVAIL_GLOBAL_OBJECT: "external",
LINVAIL_INCLUDE: "**/*",
LINVAIL_EXCLUDE: "node_modules/**/*",
};
/**
* @type {(
* env: {[k in string]?: string},
* ) => import("./config.d.ts").Config}
*/
export const toConfig = (env) => {
for (const key of getOwnPropertyNames(env)) {
if (key.startsWith("LINVAIL_") && !hasOwn(default_config, key)) {
throw new Error(`Unknown linvail configuration key: ${key}`);
}
}
const config = {
...default_config,
...env,
};
const inclusions = compileGlobRegExpArray(config.LINVAIL_INCLUDE);
const exclusions = compileGlobRegExpArray(config.LINVAIL_EXCLUDE);
return {
count: getBoolean(config.LINVAIL_COUNT, "LINVAIL_COUNT"),
global_dynamic_code: getEnumeration(
config.LINVAIL_GLOBAL_DYNAMIC_CODE,
regions,
"LINVAIL_GLOBAL_DYNAMIC_CODE",
),
global_object: getEnumeration(
config.LINVAIL_GLOBAL_OBJECT,
regions,
"LINVAIL_GLOBAL_OBJECT",
),
selection:
config.LINVAIL_INCLUDE === "**/*" && config.LINVAIL_EXCLUDE === ""
? null
: (specifier) => {
const normal = specifier.startsWith("./")
? specifier.slice(2)
: specifier;
for (const inclusion of inclusions) {
if (inclusion.test(normal)) {
for (const exclusion of exclusions) {
if (exclusion.test(normal)) {
return false;
}
}
return true;
}
}
return false;
},
};
};