@hublo/sentinel 1.7.0-alpha.6 → 1.7.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/sentinel.js +13 -80
- package/dist/{chunk-P6KTBQ2Q.js → chunk-2LLRSWG2.js} +481 -188
- package/dist/{chunk-THTXP2QU.js → chunk-7E6YTTAK.js} +1 -1
- package/dist/chunk-7E6YTTAK.js.map +1 -0
- package/dist/{chunk-DLGBRLUU.js → chunk-EESVRILJ.js} +3 -16
- package/dist/chunk-EESVRILJ.js.map +1 -0
- package/dist/{chunk-YRBFCQQI.js → chunk-NSILXJWF.js} +7002 -5697
- package/dist/{chunk-LNFGVGLE.js → chunk-SWWQ7X7B.js} +11 -23
- package/dist/{chunk-LNFGVGLE.js.map → chunk-SWWQ7X7B.js.map} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -6
- package/dist/roles/build/nest/toolchain.d.ts +1 -1
- package/dist/roles/build/nest/toolchain.js +2 -6
- package/dist/roles/build/nest/toolchain.js.map +1 -1
- package/dist/roles/static-analysis/config.js +1 -1
- package/dist/roles/test/nest/toolchain.d.ts +1 -1
- package/dist/roles/test/nest/toolchain.js +2 -3
- package/dist/roles/test/nest/toolchain.js.map +1 -1
- package/dist/roles/test/react/toolchain.js +2 -3
- package/dist/roles/test/react/toolchain.js.map +1 -1
- package/dist/roles/test/shared-test-config.d.ts +1 -1
- package/dist/roles/test/shared-test-config.js +2 -3
- package/dist/tsconfig/nest.json +1 -10
- package/dist/tsconfig/node.json +1 -11
- package/dist/tsconfig/react.json +0 -3
- package/dist/{tsconfig-aliases-C_PxuZni.d.ts → tsconfig-aliases-Ce6axdJ4.d.ts} +2 -17
- package/dist/{validate-4V3EGTXN.js → validate-S5I4Z7IO.js} +1 -3
- package/docs/arch-adoption.md +146 -0
- package/docs/build-adoption.md +0 -7
- package/docs/test-adoption.md +1 -8
- package/docs/typescript-adoption.md +13 -96
- package/docs/typescript-traces.md +89 -7
- package/docs/using-sentinel.md +3 -2
- package/package.json +1 -4
- package/dist/chunk-DLGBRLUU.js.map +0 -1
- package/dist/chunk-H4HAFJFZ.js +0 -79
- package/dist/chunk-H4HAFJFZ.js.map +0 -1
- package/dist/chunk-THTXP2QU.js.map +0 -1
- package/dist/chunk-ZFG6P27V.js +0 -313
- package/dist/chunk-ZZMYV576.js +0 -297
- package/dist/migrate-X7PB2X7R.js +0 -553
- package/dist/tsconfig/legacy.json +0 -21
package/dist/migrate-X7PB2X7R.js
DELETED
|
@@ -1,553 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LEGACY_ENTRY,
|
|
3
|
-
appliedOptions,
|
|
4
|
-
diffConfigs,
|
|
5
|
-
extendsList,
|
|
6
|
-
isBaseEntry,
|
|
7
|
-
isStackPresetEntry,
|
|
8
|
-
remainder
|
|
9
|
-
} from "./chunk-ZZMYV576.js";
|
|
10
|
-
import {
|
|
11
|
-
binFromOwnInstall,
|
|
12
|
-
resolveBin,
|
|
13
|
-
writtenFileFormatter
|
|
14
|
-
} from "./chunk-ZFG6P27V.js";
|
|
15
|
-
import {
|
|
16
|
-
parseJsonc
|
|
17
|
-
} from "./chunk-QXFCZON7.js";
|
|
18
|
-
|
|
19
|
-
// src/roles/typescript/migrate/migrate.ts
|
|
20
|
-
import { execFileSync, spawnSync } from "node:child_process";
|
|
21
|
-
import { existsSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
22
|
-
import { createRequire } from "node:module";
|
|
23
|
-
import { dirname as dirname2, isAbsolute, join, relative as relative2, resolve as resolve2 } from "node:path";
|
|
24
|
-
import { stripComments } from "jsonc-parser";
|
|
25
|
-
|
|
26
|
-
// src/roles/typescript/migrate/detach.ts
|
|
27
|
-
import { dirname, posix, relative, resolve, sep } from "node:path";
|
|
28
|
-
function rebaseTarget(target, pathsRoot, directory) {
|
|
29
|
-
const moved = relative(directory, resolve(pathsRoot, target)).split(sep).join(posix.sep);
|
|
30
|
-
const kept = target.endsWith("/") && !moved.endsWith("/") ? `${moved}/` : moved;
|
|
31
|
-
if (kept === "") return ".";
|
|
32
|
-
return kept.startsWith(".") ? kept : `./${kept}`;
|
|
33
|
-
}
|
|
34
|
-
function pickPaths(basePaths, matched, pathsRoot, directory) {
|
|
35
|
-
const picked = {};
|
|
36
|
-
for (const [pattern, targets] of Object.entries(basePaths)) {
|
|
37
|
-
if (!matched.has(pattern)) continue;
|
|
38
|
-
picked[pattern] = targets.map((target) => rebaseTarget(target, pathsRoot, directory));
|
|
39
|
-
}
|
|
40
|
-
return picked;
|
|
41
|
-
}
|
|
42
|
-
function pathsRootOf(baseFile, baseUrl) {
|
|
43
|
-
return typeof baseUrl === "string" ? resolve(dirname(baseFile), baseUrl) : dirname(baseFile);
|
|
44
|
-
}
|
|
45
|
-
function detachedConfig(input) {
|
|
46
|
-
const chain = extendsList(input.config).flatMap(
|
|
47
|
-
(entry) => isBaseEntry(entry) ? input.legacy ? [LEGACY_ENTRY] : [] : [entry]
|
|
48
|
-
);
|
|
49
|
-
const own = { ...input.config.compilerOptions };
|
|
50
|
-
const ownPaths = own.paths;
|
|
51
|
-
delete own.paths;
|
|
52
|
-
const options = { ...own };
|
|
53
|
-
for (const { key, value } of input.copied) options[key] = value;
|
|
54
|
-
const paths = ownPaths ?? (Object.keys(input.paths).length > 0 ? input.paths : void 0);
|
|
55
|
-
if (paths !== void 0) options.paths = paths;
|
|
56
|
-
const ordered = {};
|
|
57
|
-
if (chain.length > 0) ordered.extends = chain.length === 1 ? chain[0] : chain;
|
|
58
|
-
for (const [key, value] of Object.entries(input.config)) {
|
|
59
|
-
if (key === "extends") continue;
|
|
60
|
-
ordered[key] = key === "compilerOptions" ? options : value;
|
|
61
|
-
}
|
|
62
|
-
if (!("compilerOptions" in ordered) && Object.keys(options).length > 0) {
|
|
63
|
-
ordered.compilerOptions = options;
|
|
64
|
-
}
|
|
65
|
-
if (input.exclude !== void 0 && !("exclude" in ordered)) ordered.exclude = input.exclude;
|
|
66
|
-
return ordered;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// src/roles/typescript/migrate/trace.ts
|
|
70
|
-
var RESOLVING_MODULE = /^======== Resolving module '(.+?)' from '(.+?)'\. ========$/;
|
|
71
|
-
var RESOLVING_TYPE = /^======== Resolving type reference directive '(.+?)', containing file '(.+?)'(?:, root directory '.*')?\. ========$/;
|
|
72
|
-
var MATCHED_PATTERN = /^Module name '(.+?)', matched pattern '(.+?)'\.$/;
|
|
73
|
-
var RESOLVED = /^======== (?:Module name|Type reference directive) '(.+?)' was (successfully resolved to '(.+?)'|not resolved)/;
|
|
74
|
-
function resolutionKey(from, name) {
|
|
75
|
-
return `${from}\0${name}`;
|
|
76
|
-
}
|
|
77
|
-
function parseTrace(text) {
|
|
78
|
-
const resolutions = /* @__PURE__ */ new Map();
|
|
79
|
-
const patterns = /* @__PURE__ */ new Set();
|
|
80
|
-
let open;
|
|
81
|
-
let afterPathsLine = false;
|
|
82
|
-
for (const line of text.split(/\r?\n/)) {
|
|
83
|
-
const isPathsMatch = afterPathsLine;
|
|
84
|
-
afterPathsLine = line.startsWith("'paths' option is specified");
|
|
85
|
-
const resolving = RESOLVING_MODULE.exec(line) ?? RESOLVING_TYPE.exec(line);
|
|
86
|
-
if (resolving !== null) {
|
|
87
|
-
if (open !== void 0) throw new Error(`trace: unclosed resolution of '${open.name}'`);
|
|
88
|
-
open = { name: resolving[1] ?? "", from: resolving[2] ?? "" };
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
const matched = MATCHED_PATTERN.exec(line);
|
|
92
|
-
if (matched !== null) {
|
|
93
|
-
if (isPathsMatch && matched[2] !== void 0) patterns.add(matched[2]);
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const resolved = RESOLVED.exec(line);
|
|
97
|
-
if (resolved !== null) {
|
|
98
|
-
if (open === void 0 || open.name !== resolved[1]) {
|
|
99
|
-
throw new Error(`trace: result of '${resolved[1] ?? ""}' out of order`);
|
|
100
|
-
}
|
|
101
|
-
resolutions.set(resolutionKey(open.from, open.name), resolved[3] ?? null);
|
|
102
|
-
open = void 0;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (open !== void 0) throw new Error(`trace: unclosed resolution of '${open.name}'`);
|
|
106
|
-
return { resolutions, patterns };
|
|
107
|
-
}
|
|
108
|
-
function diffResolutions(before, after) {
|
|
109
|
-
const lines = [];
|
|
110
|
-
for (const key of /* @__PURE__ */ new Set([...before.keys(), ...after.keys()])) {
|
|
111
|
-
const was = before.get(key);
|
|
112
|
-
const now = after.get(key);
|
|
113
|
-
if (was === now && before.has(key) === after.has(key)) continue;
|
|
114
|
-
const [from, name] = key.split("\0");
|
|
115
|
-
const say = (side, value) => side.has(key) ? value ?? "unresolved" : "absent";
|
|
116
|
-
lines.push(`${from ?? ""}: '${name ?? ""}' ${say(before, was)} -> ${say(after, now)}`);
|
|
117
|
-
}
|
|
118
|
-
return lines;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// src/roles/typescript/migrate/migrate.ts
|
|
122
|
-
var PROBE = "tsconfig.sentinel-migrate-probe.json";
|
|
123
|
-
var ORIGINAL = ".sentinel-migrate-original";
|
|
124
|
-
var TSCONFIG_NAME = /^tsconfig.*\.json$/;
|
|
125
|
-
var SOURCE = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
126
|
-
function readTsconfig(file) {
|
|
127
|
-
return parseJsonc(readFileSync(file, "utf8"), file);
|
|
128
|
-
}
|
|
129
|
-
function tscFor(directory) {
|
|
130
|
-
return resolveBin(directory, "tsc") ?? binFromOwnInstall("typescript", "tsc") ?? "tsc";
|
|
131
|
-
}
|
|
132
|
-
var TSC_OUTPUT_LIMIT = 512 * 1024 * 1024;
|
|
133
|
-
function tsc(directory, tsconfig, extra) {
|
|
134
|
-
const command = `tsc -p ${tsconfig} ${extra.join(" ")}`;
|
|
135
|
-
const run = spawnSync(tscFor(directory), ["-p", tsconfig, ...extra], {
|
|
136
|
-
cwd: directory,
|
|
137
|
-
encoding: "utf8",
|
|
138
|
-
maxBuffer: TSC_OUTPUT_LIMIT,
|
|
139
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
140
|
-
});
|
|
141
|
-
if (run.error !== void 0)
|
|
142
|
-
throw new Error(`${command}: ${run.error.message}`, { cause: run.error });
|
|
143
|
-
if (run.signal !== null) throw new Error(`${command}: killed by ${run.signal}`);
|
|
144
|
-
if (run.status === 0) return run.stdout;
|
|
145
|
-
const typeErrors = run.status === 1 || run.status === 2;
|
|
146
|
-
if (typeErrors && run.stdout.length > 0) return run.stdout;
|
|
147
|
-
const said = run.stderr.trim() || run.stdout.trim() || `exit ${String(run.status)}, no output`;
|
|
148
|
-
throw new Error(`${command}: ${said}`);
|
|
149
|
-
}
|
|
150
|
-
function showConfig(directory, tsconfig) {
|
|
151
|
-
const printed = tsc(directory, tsconfig, ["--showConfig"]);
|
|
152
|
-
try {
|
|
153
|
-
return JSON.parse(printed);
|
|
154
|
-
} catch {
|
|
155
|
-
throw new Error(
|
|
156
|
-
`tsc --showConfig on ${join(directory, tsconfig)} did not print a config: ${printed.trim().slice(0, 300)}`
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
function withProbe(directory, config, use) {
|
|
161
|
-
writeFileSync(join(directory, PROBE), JSON.stringify(config));
|
|
162
|
-
try {
|
|
163
|
-
return use(PROBE);
|
|
164
|
-
} finally {
|
|
165
|
-
unlinkSync(join(directory, PROBE));
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
function compilesFiles(config) {
|
|
169
|
-
const empty = (list) => Array.isArray(list) && list.length === 0;
|
|
170
|
-
return !(empty(config.files) && (config.include === void 0 || empty(config.include)));
|
|
171
|
-
}
|
|
172
|
-
function inheritors(directory, names, target) {
|
|
173
|
-
const reaches = (name, seen) => {
|
|
174
|
-
if (name === target) return true;
|
|
175
|
-
if (seen.has(name)) return false;
|
|
176
|
-
seen.add(name);
|
|
177
|
-
return extendsList(readTsconfig(join(directory, name))).some((entry) => {
|
|
178
|
-
const file = resolve2(directory, entry.endsWith(".json") ? entry : `${entry}.json`);
|
|
179
|
-
return dirname2(file) === directory && reaches(relative2(directory, file), seen);
|
|
180
|
-
});
|
|
181
|
-
};
|
|
182
|
-
return names.filter((name) => reaches(name, /* @__PURE__ */ new Set()));
|
|
183
|
-
}
|
|
184
|
-
function owner(file, directory) {
|
|
185
|
-
for (let folder = dirname2(file); folder.startsWith(directory); folder = dirname2(folder)) {
|
|
186
|
-
if (readdirSync(folder).some((name) => TSCONFIG_NAME.test(name))) return folder;
|
|
187
|
-
if (folder === directory) break;
|
|
188
|
-
}
|
|
189
|
-
return directory;
|
|
190
|
-
}
|
|
191
|
-
function moduleFiles(directory) {
|
|
192
|
-
let listed;
|
|
193
|
-
try {
|
|
194
|
-
listed = execFileSync("git", ["ls-files", "-z", "--cached", "--others", "--exclude-standard"], {
|
|
195
|
-
cwd: directory,
|
|
196
|
-
encoding: "utf8"
|
|
197
|
-
});
|
|
198
|
-
} catch {
|
|
199
|
-
return void 0;
|
|
200
|
-
}
|
|
201
|
-
return [...new Set(listed.split("\0").filter(Boolean))].filter(
|
|
202
|
-
(name) => existsSync(join(directory, name))
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
|
-
function programFiles(directory, name) {
|
|
206
|
-
return tsc(directory, name, ["--listFilesOnly"]).split(/\r?\n/).map((line) => line.trim()).filter((line) => isAbsolute(line));
|
|
207
|
-
}
|
|
208
|
-
function outsidePrograms(directory, listed, files) {
|
|
209
|
-
return files.filter((name) => SOURCE.test(name) && !name.endsWith(".d.ts")).map((name) => join(directory, name)).filter((file) => existsSync(file) && !listed.has(file) && owner(file, directory) === directory);
|
|
210
|
-
}
|
|
211
|
-
function trace(directory, name) {
|
|
212
|
-
return parseTrace(tsc(directory, name, ["--noEmit", "--traceResolution"]));
|
|
213
|
-
}
|
|
214
|
-
function traceOutside(directory, target, files) {
|
|
215
|
-
return withProbe(
|
|
216
|
-
directory,
|
|
217
|
-
{
|
|
218
|
-
extends: `./${target}`,
|
|
219
|
-
compilerOptions: { allowJs: true, checkJs: false, noEmit: true },
|
|
220
|
-
files: files.map((file) => `./${relative2(directory, file)}`),
|
|
221
|
-
include: []
|
|
222
|
-
},
|
|
223
|
-
(probe) => trace(directory, probe)
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
var OUTSIDE = "(outside the programs)";
|
|
227
|
-
function sentinelInstalls(directory, presets) {
|
|
228
|
-
const require2 = createRequire(join(directory, "package.json"));
|
|
229
|
-
const packageRoot = (specifier) => {
|
|
230
|
-
let folder;
|
|
231
|
-
try {
|
|
232
|
-
folder = dirname2(require2.resolve(specifier));
|
|
233
|
-
} catch {
|
|
234
|
-
return void 0;
|
|
235
|
-
}
|
|
236
|
-
while (!existsSync(join(folder, "package.json")) && dirname2(folder) !== folder)
|
|
237
|
-
folder = dirname2(folder);
|
|
238
|
-
return folder;
|
|
239
|
-
};
|
|
240
|
-
const legacy = packageRoot(LEGACY_ENTRY);
|
|
241
|
-
if (legacy === void 0) return "no-legacy";
|
|
242
|
-
return presets.every((preset) => packageRoot(preset) === legacy) ? "one" : "several";
|
|
243
|
-
}
|
|
244
|
-
function refused(file, refusal, reason) {
|
|
245
|
-
return { file, kept: false, refusal, reason };
|
|
246
|
-
}
|
|
247
|
-
function refuseEarly(directory, shown, original, chain) {
|
|
248
|
-
if (stripComments(original) !== original) {
|
|
249
|
-
return refused(shown, "comments", "it has comments a rewrite would drop; left as is");
|
|
250
|
-
}
|
|
251
|
-
const installs = sentinelInstalls(directory, chain.filter(isStackPresetEntry));
|
|
252
|
-
if (installs === "no-legacy") {
|
|
253
|
-
return refused(
|
|
254
|
-
shown,
|
|
255
|
-
"no-legacy",
|
|
256
|
-
`${LEGACY_ENTRY} does not resolve from this module: bump its @hublo/sentinel to the version running this command, run \`pnpm install\`, and run it again`
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
if (installs === "several") {
|
|
260
|
-
return refused(
|
|
261
|
-
shown,
|
|
262
|
-
"several-installs",
|
|
263
|
-
`${LEGACY_ENTRY} and the stack preset resolve from two different @hublo/sentinel installs here; give this module one sentinel version and run \`pnpm install\``
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
|
-
return void 0;
|
|
267
|
-
}
|
|
268
|
-
function baseline(directory, target, names, files) {
|
|
269
|
-
const chained = inheritors(directory, names, target).filter(
|
|
270
|
-
(name) => compilesFiles(readTsconfig(join(directory, name)))
|
|
271
|
-
);
|
|
272
|
-
const before = new Map(names.map((name) => [name, showConfig(directory, name)]));
|
|
273
|
-
const listed = new Map(chained.map((name) => [name, programFiles(directory, name)]));
|
|
274
|
-
const empty = new Set(chained.filter((name) => listed.get(name)?.length === 0));
|
|
275
|
-
const traces = new Map(chained.map((name) => [name, trace(directory, name)]));
|
|
276
|
-
const compiled = new Set([...listed.values()].flat());
|
|
277
|
-
const outside = files === void 0 ? [] : outsidePrograms(directory, compiled, files);
|
|
278
|
-
if (outside.length > 0) traces.set(OUTSIDE, traceOutside(directory, target, outside));
|
|
279
|
-
return { before, traces, outside, empty };
|
|
280
|
-
}
|
|
281
|
-
function planDetach(directory, config, chain, effective, traces) {
|
|
282
|
-
const baseFile = resolve2(directory, chain.find(isBaseEntry) ?? "");
|
|
283
|
-
const base = readTsconfig(baseFile);
|
|
284
|
-
const basePaths = base.compilerOptions?.paths ?? {};
|
|
285
|
-
const matched = new Set([...traces.values()].flatMap((one) => [...one.patterns]));
|
|
286
|
-
const paths = pickPaths(
|
|
287
|
-
basePaths,
|
|
288
|
-
matched,
|
|
289
|
-
pathsRootOf(baseFile, base.compilerOptions?.baseUrl),
|
|
290
|
-
directory
|
|
291
|
-
);
|
|
292
|
-
const inputs = Object.fromEntries(
|
|
293
|
-
["files", "include", "references"].filter((key) => key in config).map((key) => [key, config[key]])
|
|
294
|
-
);
|
|
295
|
-
const layers = (entries) => withProbe(directory, { extends: entries, ...inputs }, (probe) => showConfig(directory, probe));
|
|
296
|
-
const stack = layers(chain.filter((entry) => !isBaseEntry(entry)));
|
|
297
|
-
const withLegacy = layers(chain.map((entry) => isBaseEntry(entry) ? LEGACY_ENTRY : entry));
|
|
298
|
-
const stackApplied = appliedOptions(stack);
|
|
299
|
-
const legacyKeys = new Set(
|
|
300
|
-
[...appliedOptions(withLegacy)].filter(([key, value]) => stackApplied.get(key) !== value).map(([key]) => key)
|
|
301
|
-
);
|
|
302
|
-
const ownKeys = new Set(Object.keys(config.compilerOptions ?? {}));
|
|
303
|
-
const withoutLegacy = remainder(effective, stack, ownKeys);
|
|
304
|
-
const legacy = withoutLegacy.some(({ key }) => legacyKeys.has(key));
|
|
305
|
-
const copied = (legacy ? remainder(effective, withLegacy, ownKeys) : withoutLegacy).filter(
|
|
306
|
-
({ key }) => key !== "paths"
|
|
307
|
-
);
|
|
308
|
-
const exclude = config.exclude === void 0 ? effective.exclude : void 0;
|
|
309
|
-
return { paths, legacy, copied, exclude, aliasesOfBase: Object.keys(basePaths).length };
|
|
310
|
-
}
|
|
311
|
-
function proveRewrite(directory, target, names, contents, { before, traces, outside }) {
|
|
312
|
-
const file = join(directory, target);
|
|
313
|
-
const original = readFileSync(file, "utf8");
|
|
314
|
-
writeFileSync(`${file}${ORIGINAL}`, original);
|
|
315
|
-
try {
|
|
316
|
-
writeFileSync(file, contents);
|
|
317
|
-
const configDifferences = names.flatMap(
|
|
318
|
-
(name) => diffConfigs(before.get(name) ?? {}, showConfig(directory, name)).map((difference) => ({
|
|
319
|
-
...difference,
|
|
320
|
-
key: `${name}: ${difference.key}`
|
|
321
|
-
}))
|
|
322
|
-
);
|
|
323
|
-
const resolutionDifferences = [...traces].flatMap(
|
|
324
|
-
([name, was]) => diffResolutions(
|
|
325
|
-
was.resolutions,
|
|
326
|
-
(name === OUTSIDE ? traceOutside(directory, target, outside) : trace(directory, name)).resolutions
|
|
327
|
-
).map((line) => `${name}: ${line}`)
|
|
328
|
-
);
|
|
329
|
-
return { configDifferences, resolutionDifferences };
|
|
330
|
-
} finally {
|
|
331
|
-
writeFileSync(file, original);
|
|
332
|
-
unlinkSync(`${file}${ORIGINAL}`);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
function proveTsconfig(directory, target, names, files, workspaceRoot) {
|
|
336
|
-
const file = join(directory, target);
|
|
337
|
-
const shown = relative2(workspaceRoot, file);
|
|
338
|
-
const original = readFileSync(file, "utf8");
|
|
339
|
-
const config = readTsconfig(file);
|
|
340
|
-
const chain = extendsList(config);
|
|
341
|
-
const early = refuseEarly(directory, shown, original, chain);
|
|
342
|
-
if (early !== void 0) return { result: early };
|
|
343
|
-
const known = baseline(directory, target, names, files);
|
|
344
|
-
const unread = [...known.traces].filter(
|
|
345
|
-
([name, one]) => name !== OUTSIDE && !known.empty.has(name) && one.resolutions.size === 0
|
|
346
|
-
);
|
|
347
|
-
if (unread.length > 0) {
|
|
348
|
-
return {
|
|
349
|
-
result: refused(
|
|
350
|
-
shown,
|
|
351
|
-
"no-trace",
|
|
352
|
-
`tsc --traceResolution printed no resolution sentinel could read for ${unread.map(([name]) => name).join(", ")}; nothing can be proved`
|
|
353
|
-
)
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
const plan = planDetach(directory, config, chain, known.before.get(target) ?? {}, known.traces);
|
|
357
|
-
const contents = `${JSON.stringify(
|
|
358
|
-
detachedConfig({
|
|
359
|
-
config,
|
|
360
|
-
legacy: plan.legacy,
|
|
361
|
-
copied: plan.copied,
|
|
362
|
-
paths: plan.paths,
|
|
363
|
-
exclude: plan.exclude
|
|
364
|
-
}),
|
|
365
|
-
null,
|
|
366
|
-
2
|
|
367
|
-
)}
|
|
368
|
-
`;
|
|
369
|
-
const differences = proveRewrite(directory, target, names, contents, known);
|
|
370
|
-
const agrees = differences.configDifferences.length === 0 && differences.resolutionDifferences.length === 0;
|
|
371
|
-
const result = {
|
|
372
|
-
file: shown,
|
|
373
|
-
kept: false,
|
|
374
|
-
...agrees ? {} : { refusal: "disagrees", reason: "tsc does not agree before and after; restored" },
|
|
375
|
-
legacy: plan.legacy,
|
|
376
|
-
aliases: { kept: Object.keys(plan.paths).length, of: plan.aliasesOfBase },
|
|
377
|
-
written: plan.copied.map(({ key }) => key),
|
|
378
|
-
resolutions: [...known.traces.values()].reduce((sum, one) => sum + one.resolutions.size, 0),
|
|
379
|
-
outsidePrograms: known.outside.map((path) => relative2(directory, path)),
|
|
380
|
-
...differences
|
|
381
|
-
};
|
|
382
|
-
return agrees ? { result, contents } : { result };
|
|
383
|
-
}
|
|
384
|
-
function restoreInterrupted(moduleRoot, files, notes, workspaceRoot) {
|
|
385
|
-
const folders = /* @__PURE__ */ new Set([
|
|
386
|
-
moduleRoot,
|
|
387
|
-
...(files ?? []).filter((name) => TSCONFIG_NAME.test(name.slice(name.lastIndexOf("/") + 1))).map((name) => join(moduleRoot, dirname2(name)))
|
|
388
|
-
]);
|
|
389
|
-
for (const folder of folders) {
|
|
390
|
-
for (const name of readdirSync(folder).filter((entry) => entry.endsWith(ORIGINAL))) {
|
|
391
|
-
const saved = join(folder, name);
|
|
392
|
-
const file = saved.slice(0, -ORIGINAL.length);
|
|
393
|
-
writeFileSync(file, readFileSync(saved, "utf8"));
|
|
394
|
-
unlinkSync(saved);
|
|
395
|
-
notes.push(`restored ${relative2(workspaceRoot, file)}, left rewritten by an interrupted run`);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
function formatWritten(moduleRoot, files, notes, options) {
|
|
400
|
-
const proved = files.filter((one) => options.write ? one.kept : one.refusal === void 0);
|
|
401
|
-
if (proved.length === 0) return;
|
|
402
|
-
const formatter = writtenFileFormatter(moduleRoot);
|
|
403
|
-
if (formatter === void 0) {
|
|
404
|
-
notes.push(
|
|
405
|
-
"no style is declared by this module or its workspace: run your formatter over the rewritten tsconfig before committing"
|
|
406
|
-
);
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
if (!options.write) {
|
|
410
|
-
notes.push(`the rewritten tsconfig would be formatted with ${formatter.origin}`);
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
const written = proved.map((one) => resolve2(options.workspaceRoot, one.file));
|
|
414
|
-
const pass = spawnSync(formatter.bin, [...formatter.args, ...written], {
|
|
415
|
-
cwd: moduleRoot,
|
|
416
|
-
encoding: "utf8"
|
|
417
|
-
});
|
|
418
|
-
notes.push(
|
|
419
|
-
pass.status === 0 ? `formatted the rewritten tsconfig with ${formatter.origin}` : `${formatter.origin} could not run over the rewritten tsconfig: run it before committing`
|
|
420
|
-
);
|
|
421
|
-
}
|
|
422
|
-
function isOwnModule(folder) {
|
|
423
|
-
return existsSync(join(folder, "package.json")) || existsSync(join(folder, "project.json"));
|
|
424
|
-
}
|
|
425
|
-
function foldersToMigrate(moduleRoot, files, notes) {
|
|
426
|
-
const nested = [
|
|
427
|
-
...new Set(
|
|
428
|
-
(files ?? []).filter(
|
|
429
|
-
(name) => TSCONFIG_NAME.test(name.slice(name.lastIndexOf("/") + 1)) && name.includes("/")
|
|
430
|
-
).map((name) => join(moduleRoot, dirname2(name)))
|
|
431
|
-
)
|
|
432
|
-
].filter((folder) => {
|
|
433
|
-
if (!isOwnModule(folder)) return true;
|
|
434
|
-
if (readdirSync(folder).some(
|
|
435
|
-
(name) => TSCONFIG_NAME.test(name) && extendsList(readTsconfig(join(folder, name))).some(isBaseEntry)
|
|
436
|
-
)) {
|
|
437
|
-
notes.push(
|
|
438
|
-
`${relative2(moduleRoot, folder)} is a module of its own: run \`sentinel --migrate --typescript\` there`
|
|
439
|
-
);
|
|
440
|
-
}
|
|
441
|
-
return false;
|
|
442
|
-
});
|
|
443
|
-
return [moduleRoot, ...nested].filter(
|
|
444
|
-
(folder) => readdirSync(folder).filter((name) => TSCONFIG_NAME.test(name) && name !== PROBE).some((name) => extendsList(readTsconfig(join(folder, name))).some(isBaseEntry))
|
|
445
|
-
);
|
|
446
|
-
}
|
|
447
|
-
function removeLeftProbe(folder, notes, workspaceRoot) {
|
|
448
|
-
if (!existsSync(join(folder, PROBE))) return;
|
|
449
|
-
unlinkSync(join(folder, PROBE));
|
|
450
|
-
notes.push(`removed ${relative2(workspaceRoot, join(folder, PROBE))}, left by an interrupted run`);
|
|
451
|
-
}
|
|
452
|
-
function proveFolder(folder, files, workspaceRoot) {
|
|
453
|
-
const names = readdirSync(folder).filter((name) => TSCONFIG_NAME.test(name) && name !== PROBE);
|
|
454
|
-
const targets = names.filter(
|
|
455
|
-
(name) => extendsList(readTsconfig(join(folder, name))).some(isBaseEntry)
|
|
456
|
-
);
|
|
457
|
-
return targets.map((target) => {
|
|
458
|
-
const shown = relative2(workspaceRoot, join(folder, target));
|
|
459
|
-
if (!extendsList(readTsconfig(join(folder, target))).some(isStackPresetEntry)) {
|
|
460
|
-
return {
|
|
461
|
-
result: refused(
|
|
462
|
-
shown,
|
|
463
|
-
"not-adopted",
|
|
464
|
-
"it extends the base but no sentinel preset: only an adopted module is detached. Run `sentinel --init --typescript` first"
|
|
465
|
-
)
|
|
466
|
-
};
|
|
467
|
-
}
|
|
468
|
-
try {
|
|
469
|
-
return proveTsconfig(folder, target, names, files, workspaceRoot);
|
|
470
|
-
} catch (error) {
|
|
471
|
-
return { result: refused(shown, "error", error.message) };
|
|
472
|
-
}
|
|
473
|
-
});
|
|
474
|
-
}
|
|
475
|
-
function markPartlyAdopted(proved) {
|
|
476
|
-
const adopted = proved.filter(({ result }) => result.refusal !== "not-adopted");
|
|
477
|
-
if (adopted.length === 0) return;
|
|
478
|
-
for (const one of proved) {
|
|
479
|
-
if (one.result.refusal !== "not-adopted") continue;
|
|
480
|
-
const name = one.result.file.slice(one.result.file.lastIndexOf("/") + 1);
|
|
481
|
-
one.result = {
|
|
482
|
-
...one.result,
|
|
483
|
-
refusal: "partly-adopted",
|
|
484
|
-
reason: `${name} extends the base without a sentinel preset, while this module's other tsconfigs are adopted: add the module's stack preset to its \`extends\` (or make it extend the adopted tsconfig), then run again`
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
function summarize(results, write) {
|
|
489
|
-
const plural = (count) => `${count} tsconfig${count === 1 ? "" : "s"}`;
|
|
490
|
-
if (results.every((one) => one.refusal === "not-adopted")) {
|
|
491
|
-
return {
|
|
492
|
-
outcome: "blocked",
|
|
493
|
-
message: "this module is not adopted: run `sentinel --init --typescript` first. Nothing was written"
|
|
494
|
-
};
|
|
495
|
-
}
|
|
496
|
-
const refusals = results.filter((one) => one.refusal !== void 0);
|
|
497
|
-
if (refusals.length === 0) {
|
|
498
|
-
return write ? {
|
|
499
|
-
outcome: "migrated",
|
|
500
|
-
message: `detached ${plural(results.length)} from tsconfig.base.json`
|
|
501
|
-
} : {
|
|
502
|
-
outcome: "proved",
|
|
503
|
-
message: "dry run: tsc agrees before and after on every tsconfig; nothing was written"
|
|
504
|
-
};
|
|
505
|
-
}
|
|
506
|
-
const disagreeing = refusals.filter((one) => one.refusal === "disagrees").length;
|
|
507
|
-
const unproved = refusals.length - disagreeing;
|
|
508
|
-
const why = [
|
|
509
|
-
...disagreeing > 0 ? [`tsc does not agree on ${plural(disagreeing)}`] : [],
|
|
510
|
-
...unproved > 0 ? [`${plural(unproved)} could not be proved`] : []
|
|
511
|
-
].join(", ");
|
|
512
|
-
return { outcome: "failed", message: `${write ? "" : "dry run: "}${why}; nothing was written` };
|
|
513
|
-
}
|
|
514
|
-
function migrateModule(moduleRoot, options) {
|
|
515
|
-
const notes = [];
|
|
516
|
-
const files = moduleFiles(moduleRoot);
|
|
517
|
-
if (files === void 0) {
|
|
518
|
-
notes.push(
|
|
519
|
-
"not a git checkout: the files outside every program were not traced, and only the module's own folder was checked for nested tsconfigs and for a run left interrupted"
|
|
520
|
-
);
|
|
521
|
-
}
|
|
522
|
-
restoreInterrupted(moduleRoot, files, notes, options.workspaceRoot);
|
|
523
|
-
const folders = foldersToMigrate(moduleRoot, files, notes);
|
|
524
|
-
if (folders.length === 0) {
|
|
525
|
-
return {
|
|
526
|
-
outcome: "skipped",
|
|
527
|
-
message: "no tsconfig of this module extends tsconfig.base.json: nothing to detach",
|
|
528
|
-
files: [],
|
|
529
|
-
notes
|
|
530
|
-
};
|
|
531
|
-
}
|
|
532
|
-
const proved = [];
|
|
533
|
-
for (const folder of folders) {
|
|
534
|
-
removeLeftProbe(folder, notes, options.workspaceRoot);
|
|
535
|
-
proved.push(...proveFolder(folder, files, options.workspaceRoot));
|
|
536
|
-
}
|
|
537
|
-
markPartlyAdopted(proved);
|
|
538
|
-
const allHold = proved.every(({ result }) => result.refusal === void 0);
|
|
539
|
-
const write = options.write && allHold;
|
|
540
|
-
if (write) {
|
|
541
|
-
for (const { result, contents } of proved) {
|
|
542
|
-
writeFileSync(resolve2(options.workspaceRoot, result.file), contents ?? "");
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
const results = proved.map(({ result }) => ({ ...result, kept: write }));
|
|
546
|
-
formatWritten(moduleRoot, results, notes, options);
|
|
547
|
-
return { ...summarize(results, options.write), files: results, notes };
|
|
548
|
-
}
|
|
549
|
-
export {
|
|
550
|
-
ORIGINAL,
|
|
551
|
-
PROBE,
|
|
552
|
-
migrateModule
|
|
553
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"noImplicitAny": false,
|
|
5
|
-
"noImplicitThis": false,
|
|
6
|
-
"strictBindCallApply": false,
|
|
7
|
-
"strictFunctionTypes": false,
|
|
8
|
-
"strictPropertyInitialization": false,
|
|
9
|
-
"useUnknownInCatchVariables": false,
|
|
10
|
-
"ignoreDeprecations": "6.0",
|
|
11
|
-
"experimentalDecorators": true,
|
|
12
|
-
"emitDecoratorMetadata": true,
|
|
13
|
-
"declaration": false,
|
|
14
|
-
"types": [
|
|
15
|
-
"vite/client",
|
|
16
|
-
"@types/segment-analytics"
|
|
17
|
-
],
|
|
18
|
-
"importHelpers": true,
|
|
19
|
-
"sourceMap": true
|
|
20
|
-
}
|
|
21
|
-
}
|