@maniumek/suwgate 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +149 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +97 -0
- package/dist/dev/checkoutEnv.d.ts +20 -0
- package/dist/dev/checkoutEnv.js +101 -0
- package/dist/dev/db.d.ts +25 -0
- package/dist/dev/db.js +103 -0
- package/dist/dev/index.d.ts +16 -0
- package/dist/dev/index.js +108 -0
- package/dist/dev/logs.d.ts +15 -0
- package/dist/dev/logs.js +127 -0
- package/dist/dev/process.d.ts +24 -0
- package/dist/dev/process.js +34 -0
- package/dist/dev/server.d.ts +8 -0
- package/dist/dev/server.js +61 -0
- package/dist/dev/settings.d.ts +46 -0
- package/dist/dev/settings.js +160 -0
- package/dist/dev/smoke.d.ts +15 -0
- package/dist/dev/smoke.js +50 -0
- package/dist/dev/testing.d.ts +19 -0
- package/dist/dev/testing.js +18 -0
- package/dist/dev/tmux.d.ts +24 -0
- package/dist/dev/tmux.js +121 -0
- package/dist/eslint/index.d.ts +17 -0
- package/dist/eslint/index.js +91 -0
- package/dist/eslint/noComments.d.ts +2 -0
- package/dist/eslint/noComments.js +27 -0
- package/dist/gate/check.d.ts +13 -0
- package/dist/gate/check.js +49 -0
- package/dist/gate/config.d.ts +34 -0
- package/dist/gate/config.js +170 -0
- package/dist/gate/contract.d.ts +51 -0
- package/dist/gate/contract.js +1 -0
- package/dist/gate/guardsStep.d.ts +14 -0
- package/dist/gate/guardsStep.js +53 -0
- package/dist/gate/plan.d.ts +29 -0
- package/dist/gate/plan.js +86 -0
- package/dist/gate/report.d.ts +15 -0
- package/dist/gate/report.js +65 -0
- package/dist/gate/repository.d.ts +3 -0
- package/dist/gate/repository.js +37 -0
- package/dist/gate/runner.d.ts +10 -0
- package/dist/gate/runner.js +75 -0
- package/dist/guards/comments/csharp.d.ts +3 -0
- package/dist/guards/comments/csharp.js +163 -0
- package/dist/guards/comments/css.d.ts +3 -0
- package/dist/guards/comments/css.js +11 -0
- package/dist/guards/comments/eslint.d.ts +2 -0
- package/dist/guards/comments/eslint.js +81 -0
- package/dist/guards/comments/hash.d.ts +3 -0
- package/dist/guards/comments/hash.js +72 -0
- package/dist/guards/comments/index.d.ts +2 -0
- package/dist/guards/comments/index.js +18 -0
- package/dist/guards/comments/json.d.ts +3 -0
- package/dist/guards/comments/json.js +13 -0
- package/dist/guards/comments/markup.d.ts +3 -0
- package/dist/guards/comments/markup.js +14 -0
- package/dist/guards/comments/scan.d.ts +19 -0
- package/dist/guards/comments/scan.js +84 -0
- package/dist/guards/comments/sql.d.ts +3 -0
- package/dist/guards/comments/sql.js +11 -0
- package/dist/guards/comments/switches.d.ts +3 -0
- package/dist/guards/comments/switches.js +30 -0
- package/dist/guards/criteria/criteriaKinds.d.ts +2 -0
- package/dist/guards/criteria/criteriaKinds.js +45 -0
- package/dist/guards/criteria/index.d.ts +2 -0
- package/dist/guards/criteria/index.js +2 -0
- package/dist/guards/database/connectionHost.d.ts +2 -0
- package/dist/guards/database/connectionHost.js +30 -0
- package/dist/guards/database/index.d.ts +2 -0
- package/dist/guards/database/index.js +3 -0
- package/dist/guards/database/migrationsUnchanged.d.ts +2 -0
- package/dist/guards/database/migrationsUnchanged.js +56 -0
- package/dist/guards/dotnet/appsettingsBound.d.ts +2 -0
- package/dist/guards/dotnet/appsettingsBound.js +43 -0
- package/dist/guards/dotnet/bannedNugetPackages.d.ts +2 -0
- package/dist/guards/dotnet/bannedNugetPackages.js +51 -0
- package/dist/guards/dotnet/index.d.ts +2 -0
- package/dist/guards/dotnet/index.js +4 -0
- package/dist/guards/dotnet/msbuild.d.ts +5 -0
- package/dist/guards/dotnet/msbuild.js +17 -0
- package/dist/guards/dotnet/testContext.d.ts +2 -0
- package/dist/guards/dotnet/testContext.js +12 -0
- package/dist/guards/dotnet/unusedNugetVersions.d.ts +2 -0
- package/dist/guards/dotnet/unusedNugetVersions.js +21 -0
- package/dist/guards/index.d.ts +2 -0
- package/dist/guards/index.js +6 -0
- package/dist/guards/repository/agentsFile.d.ts +3 -0
- package/dist/guards/repository/agentsFile.js +56 -0
- package/dist/guards/repository/bannedNpmPackages.d.ts +2 -0
- package/dist/guards/repository/bannedNpmPackages.js +43 -0
- package/dist/guards/repository/composePorts.d.ts +6 -0
- package/dist/guards/repository/composePorts.js +57 -0
- package/dist/guards/repository/dependencyDirection.d.ts +2 -0
- package/dist/guards/repository/dependencyDirection.js +87 -0
- package/dist/guards/repository/docsPaths.d.ts +2 -0
- package/dist/guards/repository/docsPaths.js +54 -0
- package/dist/guards/repository/forbiddenNames.d.ts +4 -0
- package/dist/guards/repository/forbiddenNames.js +69 -0
- package/dist/guards/repository/hooks.d.ts +3 -0
- package/dist/guards/repository/hooks.js +86 -0
- package/dist/guards/repository/imagePins.d.ts +3 -0
- package/dist/guards/repository/imagePins.js +68 -0
- package/dist/guards/repository/index.d.ts +2 -0
- package/dist/guards/repository/index.js +32 -0
- package/dist/guards/repository/justModules.d.ts +2 -0
- package/dist/guards/repository/justModules.js +36 -0
- package/dist/guards/repository/noRawColors.d.ts +2 -0
- package/dist/guards/repository/noRawColors.js +29 -0
- package/dist/guards/repository/packageManifests.d.ts +13 -0
- package/dist/guards/repository/packageManifests.js +26 -0
- package/dist/guards/repository/repositoryLayout.d.ts +3 -0
- package/dist/guards/repository/repositoryLayout.js +75 -0
- package/dist/guards/repository/requiredFiles.d.ts +6 -0
- package/dist/guards/repository/requiredFiles.js +78 -0
- package/dist/guards/repository/scriptsCalled.d.ts +3 -0
- package/dist/guards/repository/scriptsCalled.js +39 -0
- package/dist/guards/repository/scriptsHelp.d.ts +2 -0
- package/dist/guards/repository/scriptsHelp.js +37 -0
- package/dist/guards/repository/testRepository.d.ts +4 -0
- package/dist/guards/repository/testRepository.js +39 -0
- package/dist/guards/repository/text.d.ts +20 -0
- package/dist/guards/repository/text.js +33 -0
- package/dist/guards/repository/vsCodeSettings.d.ts +3 -0
- package/dist/guards/repository/vsCodeSettings.js +49 -0
- package/dist/profiles/contract.d.ts +5 -0
- package/dist/profiles/contract.js +124 -0
- package/dist/profiles/criteria.d.ts +25 -0
- package/dist/profiles/criteria.js +162 -0
- package/dist/profiles/database.d.ts +4 -0
- package/dist/profiles/database.js +23 -0
- package/dist/profiles/dotnet.d.ts +5 -0
- package/dist/profiles/dotnet.js +56 -0
- package/dist/profiles/everyRepository.d.ts +2 -0
- package/dist/profiles/everyRepository.js +32 -0
- package/dist/profiles/index.d.ts +2 -0
- package/dist/profiles/index.js +21 -0
- package/dist/profiles/library.d.ts +7 -0
- package/dist/profiles/library.js +132 -0
- package/dist/profiles/node.d.ts +5 -0
- package/dist/profiles/node.js +79 -0
- package/dist/profiles/presets/knip.d.ts +11 -0
- package/dist/profiles/presets/knip.js +29 -0
- package/dist/profiles/presets/playwright.d.ts +68 -0
- package/dist/profiles/presets/playwright.js +37 -0
- package/dist/profiles/presets/prettier.d.ts +12 -0
- package/dist/profiles/presets/prettier.js +7 -0
- package/dist/profiles/presets/tsconfig/browser.json +11 -0
- package/dist/profiles/presets/tsconfig/node.json +8 -0
- package/dist/profiles/presets/tsconfig/strict.json +17 -0
- package/dist/profiles/presets/vitest.d.ts +36 -0
- package/dist/profiles/presets/vitest.js +16 -0
- package/dist/profiles/shell.d.ts +10 -0
- package/dist/profiles/shell.js +62 -0
- package/dist/profiles/web.d.ts +2 -0
- package/dist/profiles/web.js +17 -0
- package/dist/session/guardEdit.d.ts +19 -0
- package/dist/session/guardEdit.js +96 -0
- package/dist/session/preCommit.d.ts +16 -0
- package/dist/session/preCommit.js +33 -0
- package/dist/session/stop.d.ts +8 -0
- package/dist/session/stop.js +39 -0
- package/package.json +97 -3
- package/schema/suwgate.schema.json +196 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { CONFIG_FILE } from "./config.js";
|
|
4
|
+
function shown(guard, finding) {
|
|
5
|
+
return `${guard.id}: ${finding.target}${finding.line === undefined ? "" : `:${finding.line}`}: ${finding.message}`;
|
|
6
|
+
}
|
|
7
|
+
async function findingsOf(guard, input, exceptions) {
|
|
8
|
+
try {
|
|
9
|
+
return await guard.check({
|
|
10
|
+
root: input.root,
|
|
11
|
+
files: input.files,
|
|
12
|
+
profiles: input.config.profiles,
|
|
13
|
+
directories: input.config.directories,
|
|
14
|
+
exceptions,
|
|
15
|
+
settings: input.config.settings[guard.id],
|
|
16
|
+
read: (file) => readFileSync(path.join(input.root, file), "utf8"),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
return [
|
|
21
|
+
{
|
|
22
|
+
target: guard.id,
|
|
23
|
+
message: `the guard itself failed: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`,
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export async function runGuards(input) {
|
|
29
|
+
const lines = [];
|
|
30
|
+
let excepted = 0;
|
|
31
|
+
let failed = false;
|
|
32
|
+
for (const guard of input.guards) {
|
|
33
|
+
const exceptions = input.config.exceptions.filter((exception) => exception.guard === guard.id);
|
|
34
|
+
const used = new Set();
|
|
35
|
+
for (const finding of await findingsOf(guard, input, exceptions)) {
|
|
36
|
+
const exception = exceptions.find((candidate) => candidate.target === finding.target);
|
|
37
|
+
if (exception === undefined) {
|
|
38
|
+
lines.push(shown(guard, finding));
|
|
39
|
+
failed = true;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
used.add(exception);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
excepted += used.size;
|
|
46
|
+
for (const stale of exceptions.filter((exception) => !used.has(exception))) {
|
|
47
|
+
lines.push(`${guard.id}: ${stale.target}: the exception in ${CONFIG_FILE} matches nothing any more: delete it`);
|
|
48
|
+
failed = true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const header = `${input.guards.length} guard${input.guards.length === 1 ? "" : "s"}, ${excepted} findings excepted in ${CONFIG_FILE}`;
|
|
52
|
+
return { passed: !failed, log: [header, ...lines].join("\n") };
|
|
53
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Catalog, Config } from "./config.ts";
|
|
2
|
+
export interface Outcome {
|
|
3
|
+
passed: boolean;
|
|
4
|
+
log: string;
|
|
5
|
+
}
|
|
6
|
+
export interface PlannedStep {
|
|
7
|
+
id: string;
|
|
8
|
+
title: string;
|
|
9
|
+
after: readonly string[];
|
|
10
|
+
limitSeconds: number;
|
|
11
|
+
skipped?: {
|
|
12
|
+
reason: string;
|
|
13
|
+
byConfig: boolean;
|
|
14
|
+
};
|
|
15
|
+
command?: {
|
|
16
|
+
line: string;
|
|
17
|
+
cwd: string;
|
|
18
|
+
};
|
|
19
|
+
inline?: () => Promise<Outcome>;
|
|
20
|
+
}
|
|
21
|
+
export interface PlanInput {
|
|
22
|
+
root: string;
|
|
23
|
+
quick: boolean;
|
|
24
|
+
config: Config;
|
|
25
|
+
catalog: Catalog;
|
|
26
|
+
files: readonly string[];
|
|
27
|
+
changed: () => readonly string[];
|
|
28
|
+
}
|
|
29
|
+
export declare function plan(input: PlanInput): PlannedStep[];
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { accessSync, constants } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { runGuards } from "./guardsStep.js";
|
|
4
|
+
const DEFAULT_LIMIT_SECONDS = 300;
|
|
5
|
+
const GUARDS_STEP = "guards";
|
|
6
|
+
function onPath(tool) {
|
|
7
|
+
return (process.env["PATH"] ?? "").split(path.delimiter).some((folder) => {
|
|
8
|
+
try {
|
|
9
|
+
accessSync(path.join(folder, tool), constants.X_OK);
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function addedSteps(config) {
|
|
18
|
+
return config.add.map((added) => ({
|
|
19
|
+
id: added.name,
|
|
20
|
+
title: added.title ?? added.name,
|
|
21
|
+
run: added.run,
|
|
22
|
+
...(added.quick === true ? { quick: true } : {}),
|
|
23
|
+
...(added.after === undefined ? {} : { after: added.after }),
|
|
24
|
+
...(added.limitSeconds === undefined ? {} : { limitSeconds: added.limitSeconds }),
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
function chosenRun(step, quick) {
|
|
28
|
+
if (!quick)
|
|
29
|
+
return step.run;
|
|
30
|
+
if (step.quick === true)
|
|
31
|
+
return step.run;
|
|
32
|
+
return step.quick;
|
|
33
|
+
}
|
|
34
|
+
function guardsStep(input) {
|
|
35
|
+
const active = input.catalog.guards.filter((guard) => (guard.profiles === "every" || guard.profiles.some((profile) => input.config.profiles.includes(profile))) &&
|
|
36
|
+
(!input.quick || guard.quick !== false));
|
|
37
|
+
return {
|
|
38
|
+
id: GUARDS_STEP,
|
|
39
|
+
title: `repository guards (${active.length})`,
|
|
40
|
+
after: [],
|
|
41
|
+
limitSeconds: DEFAULT_LIMIT_SECONDS,
|
|
42
|
+
inline: async () => {
|
|
43
|
+
const summary = await runGuards({ root: input.root, guards: active, files: input.files, config: input.config });
|
|
44
|
+
return { passed: summary.passed, log: summary.log };
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function plan(input) {
|
|
49
|
+
const steps = [...input.catalog.stepsFor(input.config.profiles), ...addedSteps(input.config)];
|
|
50
|
+
const planned = [guardsStep(input)];
|
|
51
|
+
for (const step of steps) {
|
|
52
|
+
const run = chosenRun(step, input.quick);
|
|
53
|
+
if (run === undefined)
|
|
54
|
+
continue;
|
|
55
|
+
const cwd = path.join(input.root, (step.profile === undefined ? undefined : input.config.directories[step.profile]) ?? "");
|
|
56
|
+
const base = { id: step.id, title: step.title, after: step.after ?? [], limitSeconds: step.limitSeconds ?? DEFAULT_LIMIT_SECONDS };
|
|
57
|
+
const skippedByConfig = input.config.skip.find((skipped) => skipped.step === step.id);
|
|
58
|
+
if (skippedByConfig !== undefined) {
|
|
59
|
+
planned.push({ ...base, skipped: { reason: skippedByConfig.reason, byConfig: true } });
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const context = {
|
|
63
|
+
root: input.root,
|
|
64
|
+
cwd,
|
|
65
|
+
quick: input.quick,
|
|
66
|
+
profiles: input.config.profiles,
|
|
67
|
+
directories: input.config.directories,
|
|
68
|
+
changed: input.changed,
|
|
69
|
+
};
|
|
70
|
+
const line = typeof run === "string" ? run : run(context);
|
|
71
|
+
const missing = (step.needs ?? []).filter((tool) => !onPath(tool));
|
|
72
|
+
if (line === null) {
|
|
73
|
+
planned.push({ ...base, inline: () => Promise.resolve({ passed: true, log: "nothing to check" }) });
|
|
74
|
+
}
|
|
75
|
+
else if (typeof line !== "string") {
|
|
76
|
+
planned.push({ ...base, skipped: { reason: line.skip, byConfig: false } });
|
|
77
|
+
}
|
|
78
|
+
else if (missing.length > 0) {
|
|
79
|
+
planned.push({ ...base, skipped: { reason: `${missing.join(", ")} not installed`, byConfig: false } });
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
planned.push({ ...base, command: { line, cwd } });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return planned;
|
|
86
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StepResult } from "./runner.ts";
|
|
2
|
+
export interface ReportInput {
|
|
3
|
+
results: readonly StepResult[];
|
|
4
|
+
command: string;
|
|
5
|
+
totalSeconds: number;
|
|
6
|
+
quickLimit: number | null;
|
|
7
|
+
ci: boolean;
|
|
8
|
+
exceptions: number;
|
|
9
|
+
verbose: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface Report {
|
|
12
|
+
text: string;
|
|
13
|
+
passed: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare function report(input: ReportInput): Report;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const FAILURE_LINES = 25;
|
|
2
|
+
function row(status, seconds, title, note = "") {
|
|
3
|
+
return ` ${status.padEnd(4)} ${seconds === null ? " " : `${String(seconds).padStart(4)}s`} ${title}${note === "" ? "" : ` — ${note}`}`;
|
|
4
|
+
}
|
|
5
|
+
function tail(log) {
|
|
6
|
+
return log.trimEnd().split("\n").slice(-FAILURE_LINES).join("\n");
|
|
7
|
+
}
|
|
8
|
+
export function report(input) {
|
|
9
|
+
const failures = [];
|
|
10
|
+
const rows = [];
|
|
11
|
+
const notVerified = [];
|
|
12
|
+
const skippedByConfig = [];
|
|
13
|
+
let passedCount = 0;
|
|
14
|
+
let failed = false;
|
|
15
|
+
for (const result of input.results) {
|
|
16
|
+
const { step } = result;
|
|
17
|
+
if (result.status === "skip") {
|
|
18
|
+
if (step.skipped?.byConfig === true) {
|
|
19
|
+
skippedByConfig.push(step.id);
|
|
20
|
+
rows.push(row("SKIP", null, step.title, `skipped in suwgate.yml: ${result.log}`));
|
|
21
|
+
}
|
|
22
|
+
else if (input.ci) {
|
|
23
|
+
failed = true;
|
|
24
|
+
rows.push(row("FAIL", null, step.title, `${result.log} (CI: a skipped step fails)`));
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
notVerified.push(step.id);
|
|
28
|
+
rows.push(row("SKIP", null, step.title, result.log));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (result.status === "fail") {
|
|
32
|
+
failed = true;
|
|
33
|
+
rows.push(row("FAIL", result.seconds, step.title));
|
|
34
|
+
failures.push(`==================== failed: ${step.title} (last ${FAILURE_LINES} lines) ====================\n${tail(result.log)}`);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
passedCount++;
|
|
38
|
+
if (input.verbose)
|
|
39
|
+
rows.push(row("PASS", result.seconds, step.title));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (input.quickLimit !== null && input.totalSeconds > input.quickLimit) {
|
|
43
|
+
failed = true;
|
|
44
|
+
rows.push(row("FAIL", input.totalSeconds, `the quick gate is over its budget of ${input.quickLimit}s: move the slow step to the full gate or speed it up; a machine that is slow across the board runs with SUWGATE_QUICK_LIMIT=<seconds>`));
|
|
45
|
+
}
|
|
46
|
+
const extras = [
|
|
47
|
+
`${input.exceptions} exception${input.exceptions === 1 ? "" : "s"} in suwgate.yml`,
|
|
48
|
+
...(skippedByConfig.length > 0 ? [`skipped in suwgate.yml: ${skippedByConfig.join(", ")}`] : []),
|
|
49
|
+
].join("; ");
|
|
50
|
+
const lines = [];
|
|
51
|
+
if (failures.length > 0)
|
|
52
|
+
lines.push("", failures.join("\n\n"));
|
|
53
|
+
const clean = !failed && notVerified.length === 0 && skippedByConfig.length === 0;
|
|
54
|
+
if (clean && !input.verbose) {
|
|
55
|
+
lines.push(`PASS ${input.command}: all ${passedCount} steps in ${input.totalSeconds}s (${extras})`);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
if (lines.length > 0)
|
|
59
|
+
lines.push("");
|
|
60
|
+
lines.push(...rows, ` ${failed ? "FAIL" : "PASS"} ${input.command}: ${passedCount} passed, total ${input.totalSeconds}s (${extras})`);
|
|
61
|
+
}
|
|
62
|
+
if (notVerified.length > 0)
|
|
63
|
+
lines.push(`NOT VERIFIED: ${notVerified.join(" ")}`);
|
|
64
|
+
return { text: lines.join("\n").replace(/^\n/, ""), passed: !failed };
|
|
65
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
function git(root, args) {
|
|
5
|
+
try {
|
|
6
|
+
return execFileSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] });
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function lines(output) {
|
|
13
|
+
return (output ?? "")
|
|
14
|
+
.split("\n")
|
|
15
|
+
.map((line) => line.trimEnd())
|
|
16
|
+
.filter((line) => line.length > 0);
|
|
17
|
+
}
|
|
18
|
+
export function repositoryRoot(cwd) {
|
|
19
|
+
return git(cwd, ["rev-parse", "--show-toplevel"])?.trim() ?? null;
|
|
20
|
+
}
|
|
21
|
+
export function repositoryFiles(root) {
|
|
22
|
+
return lines(git(root, ["ls-files", "--cached", "--others", "--exclude-standard"])).filter((file) => existsSync(path.join(root, file)));
|
|
23
|
+
}
|
|
24
|
+
function changeBase(root) {
|
|
25
|
+
for (const branch of ["master", "origin/master"]) {
|
|
26
|
+
const base = git(root, ["merge-base", "HEAD", branch])?.trim();
|
|
27
|
+
if (base !== undefined && base.length > 0)
|
|
28
|
+
return base;
|
|
29
|
+
}
|
|
30
|
+
return git(root, ["rev-parse", "--verify", "-q", "HEAD"])?.trim() ?? null;
|
|
31
|
+
}
|
|
32
|
+
export function changedFiles(root) {
|
|
33
|
+
const base = changeBase(root);
|
|
34
|
+
const committedOrStaged = base === null ? git(root, ["ls-files", "--cached"]) : git(root, ["diff", "--name-only", "--diff-filter=d", base]);
|
|
35
|
+
const untracked = git(root, ["ls-files", "--others", "--exclude-standard"]);
|
|
36
|
+
return [...new Set([...lines(committedOrStaged), ...lines(untracked)])].sort();
|
|
37
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PlannedStep } from "./plan.ts";
|
|
2
|
+
type Status = "pass" | "fail" | "skip";
|
|
3
|
+
export interface StepResult {
|
|
4
|
+
step: PlannedStep;
|
|
5
|
+
status: Status;
|
|
6
|
+
seconds: number;
|
|
7
|
+
log: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function runSteps(steps: readonly PlannedStep[], env: NodeJS.ProcessEnv, stepLimit: number | null): Promise<StepResult[]>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
const KILL_GRACE_MS = 10_000;
|
|
3
|
+
function killGroup(pid, signal) {
|
|
4
|
+
if (pid === undefined)
|
|
5
|
+
return;
|
|
6
|
+
try {
|
|
7
|
+
process.kill(-pid, signal);
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function runCommand(line, cwd, limitSeconds, env) {
|
|
14
|
+
return new Promise((resolve) => {
|
|
15
|
+
const chunks = [];
|
|
16
|
+
const child = spawn("bash", ["-c", line], { cwd, env, detached: true, stdio: ["ignore", "pipe", "pipe"] });
|
|
17
|
+
child.stdout.on("data", (chunk) => chunks.push(chunk));
|
|
18
|
+
child.stderr.on("data", (chunk) => chunks.push(chunk));
|
|
19
|
+
let timedOut = false;
|
|
20
|
+
const timer = setTimeout(() => {
|
|
21
|
+
timedOut = true;
|
|
22
|
+
killGroup(child.pid, "SIGTERM");
|
|
23
|
+
setTimeout(() => killGroup(child.pid, "SIGKILL"), KILL_GRACE_MS).unref();
|
|
24
|
+
}, limitSeconds * 1000);
|
|
25
|
+
child.on("error", (error) => {
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
resolve({ passed: false, log: `could not start bash: ${error.message}` });
|
|
28
|
+
});
|
|
29
|
+
child.on("close", (code) => {
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
const log = Buffer.concat(chunks).toString("utf8");
|
|
32
|
+
const hung = timedOut
|
|
33
|
+
? `\nhung or too slow: exceeded ${limitSeconds} s and was killed (SUWGATE_STEP_LIMIT overrides every ceiling)`
|
|
34
|
+
: "";
|
|
35
|
+
resolve({ passed: code === 0 && !timedOut, log: log + hung });
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
export async function runSteps(steps, env, stepLimit) {
|
|
40
|
+
const byId = new Map(steps.map((step) => [step.id, step]));
|
|
41
|
+
const running = new Map();
|
|
42
|
+
const start = (step, path) => {
|
|
43
|
+
const known = running.get(step.id);
|
|
44
|
+
if (known !== undefined)
|
|
45
|
+
return known;
|
|
46
|
+
if (path.includes(step.id))
|
|
47
|
+
throw new Error(`steps wait for each other in a circle: ${[...path, step.id].join(" -> ")}`);
|
|
48
|
+
const dependencies = step.after.flatMap((id) => {
|
|
49
|
+
const dependency = byId.get(id);
|
|
50
|
+
return dependency === undefined ? [] : [start(dependency, [...path, step.id])];
|
|
51
|
+
});
|
|
52
|
+
const result = (async () => {
|
|
53
|
+
const finished = await Promise.all(dependencies);
|
|
54
|
+
if (step.skipped !== undefined)
|
|
55
|
+
return { step, status: "skip", seconds: 0, log: step.skipped.reason };
|
|
56
|
+
const failed = finished.filter((dependency) => dependency.status === "fail").map((dependency) => dependency.step.id);
|
|
57
|
+
if (failed.length > 0)
|
|
58
|
+
return { step, status: "fail", seconds: 0, log: `not run: ${failed.join(", ")} failed first` };
|
|
59
|
+
const began = performance.now();
|
|
60
|
+
const limit = stepLimit ?? step.limitSeconds;
|
|
61
|
+
const outcome = step.command !== undefined
|
|
62
|
+
? await runCommand(step.command.line, step.command.cwd, limit, env)
|
|
63
|
+
: await (step.inline ?? (() => Promise.resolve({ passed: true, log: "" })))();
|
|
64
|
+
return {
|
|
65
|
+
step,
|
|
66
|
+
status: outcome.passed ? "pass" : "fail",
|
|
67
|
+
seconds: Math.round((performance.now() - began) / 1000),
|
|
68
|
+
log: outcome.log,
|
|
69
|
+
};
|
|
70
|
+
})();
|
|
71
|
+
running.set(step.id, result);
|
|
72
|
+
return result;
|
|
73
|
+
};
|
|
74
|
+
return await Promise.all(steps.map((step) => start(step, [])));
|
|
75
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { commentGuard, lineBreaks } from "./scan.js";
|
|
2
|
+
const LANGUAGE_MARKER = /^\/\*lang=\w+\*\/$/;
|
|
3
|
+
const GENERATED_MARKER = /^\/\/ <\/?auto-generated>$/;
|
|
4
|
+
const MESSAGE_DIRECTIVE = /^#\s*(region|endregion|error|warning)\b/;
|
|
5
|
+
const STRING_START = new Set(['"', "@", "$"]);
|
|
6
|
+
const GENERATED_FILE = /\.(g|Designer)\.cs$/;
|
|
7
|
+
function run(text, at, sign) {
|
|
8
|
+
let end = at;
|
|
9
|
+
while (text[end] === sign)
|
|
10
|
+
end++;
|
|
11
|
+
return end - at;
|
|
12
|
+
}
|
|
13
|
+
function advanceTo(lexer, end) {
|
|
14
|
+
lexer.line += lineBreaks(lexer.text, lexer.at, end);
|
|
15
|
+
lexer.at = end;
|
|
16
|
+
}
|
|
17
|
+
function comment(lexer, end) {
|
|
18
|
+
lexer.found.push({ line: lexer.line, text: lexer.text.slice(lexer.at, end).trim() });
|
|
19
|
+
advanceTo(lexer, end);
|
|
20
|
+
}
|
|
21
|
+
function atLineStart(text, at) {
|
|
22
|
+
const start = text.lastIndexOf("\n", at - 1) + 1;
|
|
23
|
+
return text.slice(start, at).trim() === "";
|
|
24
|
+
}
|
|
25
|
+
function endOfLine(text, at) {
|
|
26
|
+
const newline = text.indexOf("\n", at);
|
|
27
|
+
return newline < 0 ? text.length : newline;
|
|
28
|
+
}
|
|
29
|
+
function hole(lexer) {
|
|
30
|
+
code(lexer, true);
|
|
31
|
+
}
|
|
32
|
+
function quotedString(lexer, verbatim, interpolated) {
|
|
33
|
+
const { text } = lexer;
|
|
34
|
+
lexer.at++;
|
|
35
|
+
while (lexer.at < text.length) {
|
|
36
|
+
const sign = text.charAt(lexer.at);
|
|
37
|
+
if (sign === "\\" && !verbatim) {
|
|
38
|
+
lexer.at += 2;
|
|
39
|
+
}
|
|
40
|
+
else if (sign === '"' && verbatim && text[lexer.at + 1] === '"') {
|
|
41
|
+
lexer.at += 2;
|
|
42
|
+
}
|
|
43
|
+
else if (sign === '"') {
|
|
44
|
+
lexer.at++;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
else if (sign === "\n" && !verbatim) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
else if (interpolated && (sign === "{" || sign === "}") && text[lexer.at + 1] === sign) {
|
|
51
|
+
lexer.at += 2;
|
|
52
|
+
}
|
|
53
|
+
else if (interpolated && sign === "{") {
|
|
54
|
+
lexer.at++;
|
|
55
|
+
hole(lexer);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
if (sign === "\n")
|
|
59
|
+
lexer.line++;
|
|
60
|
+
lexer.at++;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function rawString(lexer, quotes, dollars) {
|
|
65
|
+
const { text } = lexer;
|
|
66
|
+
lexer.at += quotes;
|
|
67
|
+
while (lexer.at < text.length) {
|
|
68
|
+
const sign = text.charAt(lexer.at);
|
|
69
|
+
if (sign === '"' && run(text, lexer.at, '"') >= quotes) {
|
|
70
|
+
lexer.at += quotes;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const braces = sign === "{" ? run(text, lexer.at, "{") : 0;
|
|
74
|
+
if (dollars > 0 && braces >= dollars) {
|
|
75
|
+
lexer.at += braces;
|
|
76
|
+
hole(lexer);
|
|
77
|
+
lexer.at += Math.min(run(text, lexer.at, "}"), dollars - 1);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
if (sign === "\n")
|
|
81
|
+
lexer.line++;
|
|
82
|
+
lexer.at += Math.max(braces, 1);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function stringAt(lexer) {
|
|
87
|
+
const { text } = lexer;
|
|
88
|
+
let at = lexer.at;
|
|
89
|
+
let verbatim = text[at] === "@";
|
|
90
|
+
if (verbatim)
|
|
91
|
+
at++;
|
|
92
|
+
const dollars = run(text, at, "$");
|
|
93
|
+
at += dollars;
|
|
94
|
+
if (!verbatim && text[at] === "@") {
|
|
95
|
+
verbatim = true;
|
|
96
|
+
at++;
|
|
97
|
+
}
|
|
98
|
+
if (text[at] !== '"')
|
|
99
|
+
return false;
|
|
100
|
+
lexer.at = at;
|
|
101
|
+
const quotes = run(text, at, '"');
|
|
102
|
+
if (!verbatim && quotes >= 3)
|
|
103
|
+
rawString(lexer, quotes, dollars);
|
|
104
|
+
else
|
|
105
|
+
quotedString(lexer, verbatim, dollars > 0);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
function charLiteral(lexer) {
|
|
109
|
+
const { text } = lexer;
|
|
110
|
+
lexer.at++;
|
|
111
|
+
while (lexer.at < text.length && text[lexer.at] !== "'" && text[lexer.at] !== "\n") {
|
|
112
|
+
lexer.at += text[lexer.at] === "\\" ? 2 : 1;
|
|
113
|
+
}
|
|
114
|
+
if (text[lexer.at] === "'")
|
|
115
|
+
lexer.at++;
|
|
116
|
+
}
|
|
117
|
+
function code(lexer, inHole) {
|
|
118
|
+
const { text } = lexer;
|
|
119
|
+
let depth = 0;
|
|
120
|
+
while (lexer.at < text.length) {
|
|
121
|
+
const sign = text.charAt(lexer.at);
|
|
122
|
+
if (sign === "\n") {
|
|
123
|
+
lexer.line++;
|
|
124
|
+
lexer.at++;
|
|
125
|
+
}
|
|
126
|
+
else if (text.startsWith("//", lexer.at)) {
|
|
127
|
+
comment(lexer, endOfLine(text, lexer.at));
|
|
128
|
+
}
|
|
129
|
+
else if (text.startsWith("/*", lexer.at)) {
|
|
130
|
+
const close = text.indexOf("*/", lexer.at + 2);
|
|
131
|
+
comment(lexer, close < 0 ? text.length : close + 2);
|
|
132
|
+
}
|
|
133
|
+
else if (sign === "#" && atLineStart(text, lexer.at) && MESSAGE_DIRECTIVE.test(text.slice(lexer.at, endOfLine(text, lexer.at)))) {
|
|
134
|
+
lexer.at = endOfLine(text, lexer.at);
|
|
135
|
+
}
|
|
136
|
+
else if (sign === "'") {
|
|
137
|
+
charLiteral(lexer);
|
|
138
|
+
}
|
|
139
|
+
else if (sign === "}" && inHole && depth === 0) {
|
|
140
|
+
lexer.at++;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
else if (!(STRING_START.has(sign) && stringAt(lexer))) {
|
|
144
|
+
if (sign === "{")
|
|
145
|
+
depth++;
|
|
146
|
+
if (sign === "}")
|
|
147
|
+
depth--;
|
|
148
|
+
lexer.at++;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export function csharpCommentsIn(text) {
|
|
153
|
+
const lexer = { text, at: 0, line: 1, found: [] };
|
|
154
|
+
code(lexer, false);
|
|
155
|
+
return lexer.found.filter((found) => !LANGUAGE_MARKER.test(found.text) && !GENERATED_MARKER.test(found.text));
|
|
156
|
+
}
|
|
157
|
+
export const commentsCsharp = commentGuard({
|
|
158
|
+
id: "comments-csharp",
|
|
159
|
+
description: "No C# file holds a comment (/// docs included) but the tool markers /*lang=…*/ and // <auto-generated>",
|
|
160
|
+
profiles: ["dotnet"],
|
|
161
|
+
scans: (file) => file.endsWith(".cs") && !GENERATED_FILE.test(file),
|
|
162
|
+
commentsIn: (_, text) => csharpCommentsIn(text),
|
|
163
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { commentGuard, scan } from "./scan.js";
|
|
2
|
+
export function cssCommentsIn(text) {
|
|
3
|
+
return scan(text, null, "'\"");
|
|
4
|
+
}
|
|
5
|
+
export const commentsCss = commentGuard({
|
|
6
|
+
id: "comments-css",
|
|
7
|
+
description: "No CSS file holds a comment",
|
|
8
|
+
profiles: ["web", "library"],
|
|
9
|
+
scans: (file) => file.endsWith(".css"),
|
|
10
|
+
commentsIn: (_, text) => cssCommentsIn(text),
|
|
11
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { pathToFileURL } from "node:url";
|
|
13
|
+
import { isScanned } from "./scan.js";
|
|
14
|
+
const RULE = "suwgate/no-comments";
|
|
15
|
+
const CODE = /\.(ts|tsx|js|jsx|mjs|cjs|mts|cts)$/;
|
|
16
|
+
const LINTED_PROFILES = ["web", "library"];
|
|
17
|
+
const USE_THE_PRESET = `extend strict() or app() of @maniumek/suwgate/eslint in eslint.config.js, or switch ${RULE} on as an error for every TypeScript and JavaScript file`;
|
|
18
|
+
function lintedDirectories(context) {
|
|
19
|
+
return [
|
|
20
|
+
...new Set(LINTED_PROFILES.filter((profile) => context.profiles.includes(profile)).map((profile) => context.directories[profile] ?? ".")),
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
function inDirectory(file, directory) {
|
|
24
|
+
return directory === "." || file.startsWith(`${directory}/`);
|
|
25
|
+
}
|
|
26
|
+
function isError(setting) {
|
|
27
|
+
const severity = Array.isArray(setting) ? setting[0] : setting;
|
|
28
|
+
return severity === 2 || severity === "error";
|
|
29
|
+
}
|
|
30
|
+
async function loadEslint(cwd) {
|
|
31
|
+
if (!existsSync(path.join(cwd, "package.json")))
|
|
32
|
+
return null;
|
|
33
|
+
try {
|
|
34
|
+
const resolved = createRequire(path.join(cwd, "package.json")).resolve("eslint");
|
|
35
|
+
return (await import(__rewriteRelativeImportExtension(pathToFileURL(resolved).href)));
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const IGNORED = "eslint ignores this file, so a comment in it goes unnoticed: take it out of the ignores of eslint.config.js (a file outside the tsconfig projects goes into untyped of strict() instead), or add an exception if it is generated";
|
|
42
|
+
async function verdictOf(eslint, absolute) {
|
|
43
|
+
if (await eslint.isPathIgnored(absolute))
|
|
44
|
+
return "ignored";
|
|
45
|
+
const config = await eslint.calculateConfigForFile(absolute);
|
|
46
|
+
return isError(config?.rules?.[RULE]) ? "refused" : "allowed";
|
|
47
|
+
}
|
|
48
|
+
function findingOf(file, verdict) {
|
|
49
|
+
if (verdict === "ignored")
|
|
50
|
+
return [{ target: file, message: IGNORED }];
|
|
51
|
+
if (verdict === "allowed")
|
|
52
|
+
return [{ target: file, message: `eslint does not refuse comments in this file: ${USE_THE_PRESET}` }];
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
async function findingsIn(context, directory) {
|
|
56
|
+
const files = context.files.filter((file) => CODE.test(file) && isScanned(file) && inDirectory(file, directory));
|
|
57
|
+
if (files.length === 0)
|
|
58
|
+
return [];
|
|
59
|
+
const cwd = path.join(context.root, directory);
|
|
60
|
+
const eslint = await loadEslint(cwd);
|
|
61
|
+
if (eslint === null) {
|
|
62
|
+
return [
|
|
63
|
+
{
|
|
64
|
+
target: directory,
|
|
65
|
+
message: `eslint cannot be loaded from ${directory}: install the packages (pnpm install) and add eslint to its devDependencies`,
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
const linter = new eslint.ESLint({ cwd });
|
|
70
|
+
const verdicts = await Promise.all(files.map((file) => verdictOf(linter, path.join(context.root, file))));
|
|
71
|
+
if (!verdicts.includes("refused")) {
|
|
72
|
+
return [{ target: directory, message: `eslint refuses comments in no TypeScript or JavaScript file here: ${USE_THE_PRESET}` }];
|
|
73
|
+
}
|
|
74
|
+
return files.flatMap((file, index) => findingOf(file, verdicts[index] ?? "allowed"));
|
|
75
|
+
}
|
|
76
|
+
export const commentsEslint = {
|
|
77
|
+
id: "comments-eslint",
|
|
78
|
+
description: `ESLint lints every TypeScript and JavaScript file and refuses its comments with ${RULE} as an error`,
|
|
79
|
+
profiles: LINTED_PROFILES,
|
|
80
|
+
check: async (context) => (await Promise.all(lintedDirectories(context).map((directory) => findingsIn(context, directory)))).flat(),
|
|
81
|
+
};
|