@maniumek/suwgate 0.2.0 → 0.4.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/README.md +132 -24
- package/dist/cli.js +49 -5
- package/dist/dev/db.d.ts +1 -1
- package/dist/dev/db.js +25 -0
- package/dist/dev/logs.js +9 -1
- package/dist/dev/settings.d.ts +2 -0
- package/dist/dev/settings.js +5 -2
- package/dist/eslint/index.js +4 -1
- package/dist/gate/check.d.ts +10 -0
- package/dist/gate/check.js +94 -18
- package/dist/gate/config.d.ts +19 -1
- package/dist/gate/config.js +119 -4
- package/dist/gate/contract.d.ts +13 -0
- package/dist/gate/guardsStep.d.ts +3 -1
- package/dist/gate/guardsStep.js +14 -3
- package/dist/gate/plan.d.ts +20 -1
- package/dist/gate/plan.js +92 -20
- package/dist/gate/report.d.ts +1 -0
- package/dist/gate/report.js +12 -3
- package/dist/gate/repository.d.ts +1 -0
- package/dist/gate/repository.js +8 -0
- package/dist/gate/runner.js +18 -3
- package/dist/guards/comments/hash.js +22 -1
- package/dist/guards/dotnet/appsettingsBound.js +29 -2
- package/dist/guards/dotnet/index.js +2 -1
- package/dist/guards/dotnet/suwbackkitWiring.d.ts +2 -0
- package/dist/guards/dotnet/suwbackkitWiring.js +31 -0
- package/dist/guards/repository/imagePins.js +1 -1
- package/dist/guards/repository/index.js +2 -0
- package/dist/guards/repository/justModules.js +28 -10
- package/dist/guards/repository/noRawColors.js +1 -1
- package/dist/guards/repository/scriptsCalled.js +38 -11
- package/dist/guards/repository/suwfrontkitWiring.d.ts +2 -0
- package/dist/guards/repository/suwfrontkitWiring.js +52 -0
- package/dist/profiles/contract.js +81 -38
- package/dist/profiles/criteria.d.ts +1 -0
- package/dist/profiles/criteria.js +1 -1
- package/dist/profiles/criteriaRan.d.ts +22 -0
- package/dist/profiles/criteriaRan.js +152 -0
- package/dist/profiles/database.js +11 -4
- package/dist/profiles/dotnet.d.ts +2 -0
- package/dist/profiles/dotnet.js +39 -6
- package/dist/profiles/dotnetGuards.d.ts +3 -0
- package/dist/profiles/dotnetGuards.js +54 -0
- package/dist/profiles/everyRepository.js +2 -0
- package/dist/profiles/index.d.ts +2 -0
- package/dist/profiles/index.js +2 -0
- package/dist/profiles/library.js +12 -7
- package/dist/profiles/node.js +21 -7
- package/dist/profiles/presets/knip.d.ts +1 -0
- package/dist/profiles/presets/knip.js +4 -2
- package/dist/profiles/presets/playwright.d.ts +3 -1
- package/dist/profiles/presets/playwright.js +7 -1
- package/dist/profiles/reports.d.ts +35 -0
- package/dist/profiles/reports.js +60 -0
- package/dist/profiles/shell.d.ts +6 -0
- package/dist/profiles/shell.js +39 -4
- package/dist/profiles/verdict.d.ts +9 -0
- package/dist/profiles/verdict.js +124 -0
- package/dist/profiles/web.js +25 -6
- package/package.json +2 -2
- package/schema/suwgate.schema.json +82 -4
|
@@ -7,7 +7,8 @@ const NGINX = new Set(["nginx.conf", "nginx.conf.template"]);
|
|
|
7
7
|
const SHELLCHECK_DIRECTIVE = /^# shellcheck (disable|enable|source|shell|external-sources)=\S+$/;
|
|
8
8
|
const DOCKERFILE_SYNTAX = /^# syntax=\S+$/;
|
|
9
9
|
const EDITORCONFIG_CRITERION = new RegExp(`^# ${CRITERION_ID}$`);
|
|
10
|
-
const RECIPE_OR_MODULE = /^(@?[A-Za-z_][\w-]*(\s+[
|
|
10
|
+
const RECIPE_OR_MODULE = /^(@?[A-Za-z_][\w-]*(\s+[^:]*)?:(?!=)|mod\??\s+[\w-]+(\s+(['"])[^'"]+\4)?\s*$)/;
|
|
11
|
+
const BLOCK_SCALAR = /^(\s*)((?:-\s+)*)(?:([^\s#][^#]*?):\s+)?[|>](?:[1-9][-+]?|[-+][1-9]?)?\s*(?:#.*)?$/;
|
|
11
12
|
const isJust = (file) => ["justfile", ".justfile"].includes(fileName(file).toLowerCase()) || file.endsWith(".just");
|
|
12
13
|
const isShell = (file) => file.endsWith(".sh") || file.endsWith(".bash") || file.startsWith(".githooks/");
|
|
13
14
|
const isDockerfile = (file) => /^Dockerfile(\..+)?$/.test(fileName(file)) || file.endsWith(".Dockerfile");
|
|
@@ -56,9 +57,29 @@ function isAllowed(file, lines, index, comment) {
|
|
|
56
57
|
(isEditorconfig(file) && EDITORCONFIG_CRITERION.test(comment)) ||
|
|
57
58
|
(isJust(file) && describesTheRecipeBelow(lines, index)));
|
|
58
59
|
}
|
|
60
|
+
const isYaml = (file) => file.endsWith(".yaml") || file.endsWith(".yml");
|
|
61
|
+
const indentOf = (line) => line.length - line.trimStart().length;
|
|
62
|
+
function blockScalarLines(lines) {
|
|
63
|
+
const inside = new Set();
|
|
64
|
+
let parent = null;
|
|
65
|
+
lines.forEach((line, index) => {
|
|
66
|
+
if (parent !== null && (line.trim() === "" || indentOf(line) > parent)) {
|
|
67
|
+
inside.add(index);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
parent = null;
|
|
71
|
+
const start = BLOCK_SCALAR.exec(line);
|
|
72
|
+
if (start !== null)
|
|
73
|
+
parent = (start[1] ?? "").length + (start[3] === undefined ? 0 : (start[2] ?? "").length);
|
|
74
|
+
});
|
|
75
|
+
return inside;
|
|
76
|
+
}
|
|
59
77
|
export function hashCommentsIn(file, text) {
|
|
60
78
|
const lines = text.split("\n");
|
|
79
|
+
const literal = isYaml(file) ? blockScalarLines(lines) : new Set();
|
|
61
80
|
return lines.flatMap((line, index) => {
|
|
81
|
+
if (literal.has(index))
|
|
82
|
+
return [];
|
|
62
83
|
const comment = commentOn(file, line);
|
|
63
84
|
return comment !== null && !isAllowed(file, lines, index, comment) ? [{ line: index + 1, text: comment }] : [];
|
|
64
85
|
});
|
|
@@ -3,6 +3,33 @@ const FRAMEWORK_SECTIONS = ["Logging", "AllowedHosts", "ConnectionStrings", "Kes
|
|
|
3
3
|
const APPSETTINGS = /(^|\/)appsettings(\.[\w-]+)?\.json$/;
|
|
4
4
|
const CSHARP = /\.cs$/;
|
|
5
5
|
const SECTION_READ = /\b(?:BindConfiguration|GetSection|GetRequiredSection)\(\s*"([^":]+)/g;
|
|
6
|
+
const SECTION_READ_BY_NAME = /\b(?:BindConfiguration|GetSection|GetRequiredSection)\(\s*([A-Za-z_][\w.]*)\s*\)/g;
|
|
7
|
+
const TYPE_OR_CONSTANT = /\b(?:class|record|struct|interface)\s+(\w+)|\bconst\s+string\s+(\w+)\s*=\s*"([^":]+)"/g;
|
|
8
|
+
function constants(text) {
|
|
9
|
+
const found = new Map();
|
|
10
|
+
let type = "";
|
|
11
|
+
for (const match of text.matchAll(TYPE_OR_CONSTANT)) {
|
|
12
|
+
if (match[1] !== undefined) {
|
|
13
|
+
type = match[1];
|
|
14
|
+
}
|
|
15
|
+
else if (match[2] !== undefined && match[3] !== undefined) {
|
|
16
|
+
found.set(match[2], match[3]);
|
|
17
|
+
if (type !== "")
|
|
18
|
+
found.set(`${type}.${match[2]}`, match[3]);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return found;
|
|
22
|
+
}
|
|
23
|
+
function sectionsRead(texts) {
|
|
24
|
+
const named = new Map(texts.flatMap((text) => [...constants(text)]));
|
|
25
|
+
return new Set(texts.flatMap((text) => [
|
|
26
|
+
...[...text.matchAll(SECTION_READ)].map((match) => match[1] ?? ""),
|
|
27
|
+
...[...text.matchAll(SECTION_READ_BY_NAME)].flatMap((match) => {
|
|
28
|
+
const name = match[1] ?? "";
|
|
29
|
+
return named.get(name) ?? named.get(name.split(".").slice(-2).join(".")) ?? [];
|
|
30
|
+
}),
|
|
31
|
+
]));
|
|
32
|
+
}
|
|
6
33
|
function sectionsOf(file, text) {
|
|
7
34
|
try {
|
|
8
35
|
const parsed = JSON.parse(text);
|
|
@@ -17,10 +44,10 @@ function sectionsOf(file, text) {
|
|
|
17
44
|
}
|
|
18
45
|
export const appsettingsBound = {
|
|
19
46
|
id: "appsettings-bound",
|
|
20
|
-
description: "Every section of appsettings*.json is read by the code (an options class bound to it), so no setting is dead",
|
|
47
|
+
description: "Every section of appsettings*.json is read by the code (an options class bound to it, by a string or a const string), so no setting is dead",
|
|
21
48
|
profiles: ["dotnet"],
|
|
22
49
|
check(context) {
|
|
23
|
-
const read =
|
|
50
|
+
const read = sectionsRead(dotnetFiles(context, CSHARP).map((file) => context.read(file)));
|
|
24
51
|
const problems = [];
|
|
25
52
|
const unread = new Map();
|
|
26
53
|
for (const file of dotnetFiles(context, APPSETTINGS)) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { appsettingsBound } from "./appsettingsBound.js";
|
|
2
2
|
import { bannedNugetPackages } from "./bannedNugetPackages.js";
|
|
3
|
+
import { suwbackkitWiring } from "./suwbackkitWiring.js";
|
|
3
4
|
import { unusedNugetVersions } from "./unusedNugetVersions.js";
|
|
4
|
-
export const dotnetGuards = [bannedNugetPackages, unusedNugetVersions, appsettingsBound];
|
|
5
|
+
export const dotnetGuards = [bannedNugetPackages, unusedNugetVersions, appsettingsBound, suwbackkitWiring];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { dotnetFiles } from "./msbuild.js";
|
|
3
|
+
const PROJECT = /\.csproj$/;
|
|
4
|
+
const CSHARP = /\.cs$/;
|
|
5
|
+
const WEB_SDK = /<Project\b[^>]*\bSdk\s*=\s*"Microsoft\.NET\.Sdk\.Web"/;
|
|
6
|
+
const KIT_REFERENCE = /<(?:PackageReference\b[^>]*\bInclude\s*=\s*"Suwbackkit\.Server"|ProjectReference\b[^>]*\bInclude\s*=\s*"[^"]*Suwbackkit\.Server\.csproj")/;
|
|
7
|
+
const CALLS = [
|
|
8
|
+
{ method: "AddServerDefaults", pattern: /\.AddServerDefaults\s*(?:<[^>]*>)?\s*\(/, on: "the builder (builder.AddServerDefaults())" },
|
|
9
|
+
{ method: "MapServerDefaults", pattern: /\.MapServerDefaults\s*\(/, on: "the app (app.MapServerDefaults())" },
|
|
10
|
+
];
|
|
11
|
+
function findingsOf(context, project) {
|
|
12
|
+
const folder = path.posix.dirname(project);
|
|
13
|
+
const sources = dotnetFiles(context, CSHARP)
|
|
14
|
+
.filter((file) => folder === "." || file.startsWith(`${folder}/`))
|
|
15
|
+
.map((file) => context.read(file));
|
|
16
|
+
return CALLS.filter((call) => !sources.some((text) => call.pattern.test(text))).map((call) => ({
|
|
17
|
+
target: project,
|
|
18
|
+
message: `the server references Suwbackkit.Server but never calls ${call.method}: call it on ${call.on} where the server is built, so it gets the kit's logging, problem details, OpenAPI and the /health and /alive checks`,
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
export const suwbackkitWiring = {
|
|
22
|
+
id: "suwbackkit-wiring",
|
|
23
|
+
description: "A web server that references Suwbackkit.Server calls AddServerDefaults and MapServerDefaults, so the kit is wired, not only installed",
|
|
24
|
+
profiles: ["dotnet"],
|
|
25
|
+
check: (context) => dotnetFiles(context, PROJECT)
|
|
26
|
+
.filter((project) => {
|
|
27
|
+
const text = context.read(project);
|
|
28
|
+
return WEB_SDK.test(text) && KIT_REFERENCE.test(text);
|
|
29
|
+
})
|
|
30
|
+
.flatMap((project) => findingsOf(context, project)),
|
|
31
|
+
};
|
|
@@ -44,7 +44,7 @@ export const imagePins = {
|
|
|
44
44
|
check(context) {
|
|
45
45
|
const found = context.files.flatMap((file) => isDockerfile(baseName(file)) || /\.(ya?ml|sh)$/.test(file) ? references(file, context.read(file)) : []);
|
|
46
46
|
const unpinned = found
|
|
47
|
-
.filter(({ image }) => !image.includes("$") && !isPinned(image))
|
|
47
|
+
.filter(({ image }) => !image.includes("$") && !image.includes("{{") && !isPinned(image))
|
|
48
48
|
.map(({ file, line, image }) => ({
|
|
49
49
|
target: file,
|
|
50
50
|
line,
|
|
@@ -13,6 +13,7 @@ import { repositoryLayout } from "./repositoryLayout.js";
|
|
|
13
13
|
import { requiredFiles } from "./requiredFiles.js";
|
|
14
14
|
import { scriptsCalled } from "./scriptsCalled.js";
|
|
15
15
|
import { scriptsHelp } from "./scriptsHelp.js";
|
|
16
|
+
import { suwfrontkitWiring } from "./suwfrontkitWiring.js";
|
|
16
17
|
import { vsCodeSettings } from "./vsCodeSettings.js";
|
|
17
18
|
export const repositoryGuards = [
|
|
18
19
|
requiredFiles,
|
|
@@ -31,4 +32,5 @@ export const repositoryGuards = [
|
|
|
31
32
|
noRawColors,
|
|
32
33
|
dependencyDirection,
|
|
33
34
|
hooks,
|
|
35
|
+
suwfrontkitWiring,
|
|
34
36
|
];
|
|
@@ -1,31 +1,49 @@
|
|
|
1
1
|
import { baseName } from "./text.js";
|
|
2
|
-
const MOD_LINE = /^mod\??\s+([\w-]+)\s*$/gm;
|
|
2
|
+
const MOD_LINE = /^mod\??\s+([\w-]+)(?:\s+(['"])([^'"]+)\2)?\s*$/gm;
|
|
3
|
+
const SOURCES_IN_A_FOLDER = ["mod.just", "justfile", ".justfile"];
|
|
3
4
|
function moduleName(file) {
|
|
4
5
|
return file.endsWith("/mod.just") ? (file.split("/")[0] ?? "") : baseName(file).replace(/\.just$/, "");
|
|
5
6
|
}
|
|
7
|
+
function normalized(file) {
|
|
8
|
+
return file.replace(/^\.\//, "").replace(/\/+$/, "");
|
|
9
|
+
}
|
|
10
|
+
function sourceOf(mod, files) {
|
|
11
|
+
if (mod.path === undefined)
|
|
12
|
+
return files.find((file) => file === `${mod.name}.just` || file === `${mod.name}/mod.just`);
|
|
13
|
+
const path = normalized(mod.path);
|
|
14
|
+
return files.includes(path) ? path : SOURCES_IN_A_FOLDER.map((name) => `${path}/${name}`).find((file) => files.includes(file));
|
|
15
|
+
}
|
|
6
16
|
export const justModules = {
|
|
7
17
|
id: "just-modules",
|
|
8
|
-
description: "Every just module is <name>.just beside the justfile
|
|
18
|
+
description: "Every just module is <name>.just beside the justfile, <folder>/mod.just, or the file a mod line names by its path (mod <name> '<path>'), and the justfile names each one",
|
|
9
19
|
profiles: "every",
|
|
10
20
|
check(context) {
|
|
11
21
|
const modules = context.files.filter((file) => file.endsWith(".just"));
|
|
12
|
-
const
|
|
13
|
-
? [...context.read("justfile").matchAll(MOD_LINE)].map((match) =>
|
|
22
|
+
const mods = context.files.includes("justfile")
|
|
23
|
+
? [...context.read("justfile").matchAll(MOD_LINE)].map((match) => ({
|
|
24
|
+
name: match[1] ?? "",
|
|
25
|
+
...(match[3] === undefined ? {} : { path: match[3] }),
|
|
26
|
+
}))
|
|
14
27
|
: [];
|
|
28
|
+
const byPath = mods.filter((mod) => mod.path !== undefined).flatMap((mod) => sourceOf(mod, context.files) ?? []);
|
|
29
|
+
const named = mods.filter((mod) => mod.path === undefined).map((mod) => mod.name);
|
|
15
30
|
const misplaced = modules
|
|
31
|
+
.filter((file) => !byPath.includes(file))
|
|
16
32
|
.filter((file) => file.includes("/") && (baseName(file) !== "mod.just" || file.split("/").length !== 2))
|
|
17
33
|
.map((file) => ({
|
|
18
34
|
target: file,
|
|
19
|
-
message: "just finds a module only as <name>.just beside the justfile or as <folder>/mod.just: move it there",
|
|
35
|
+
message: "just finds a module by its name only as <name>.just beside the justfile or as <folder>/mod.just: move it there, or name its path in the mod line (mod <name> '<path>')",
|
|
20
36
|
}));
|
|
21
|
-
const missing =
|
|
22
|
-
.filter((
|
|
23
|
-
.map((
|
|
37
|
+
const missing = mods
|
|
38
|
+
.filter((mod) => sourceOf(mod, context.files) === undefined)
|
|
39
|
+
.map((mod) => ({
|
|
24
40
|
target: "justfile",
|
|
25
|
-
message:
|
|
41
|
+
message: mod.path === undefined
|
|
42
|
+
? `mod ${mod.name} has no ${mod.name}.just or ${mod.name}/mod.just: add the module, or delete the mod line`
|
|
43
|
+
: `mod ${mod.name} names ${mod.path}, which is not a module file here: fix the path, or delete the mod line`,
|
|
26
44
|
}));
|
|
27
45
|
const orphans = modules
|
|
28
|
-
.filter((file) => !named.includes(moduleName(file)))
|
|
46
|
+
.filter((file) => !byPath.includes(file) && !named.includes(moduleName(file)))
|
|
29
47
|
.filter((file) => !misplaced.some((finding) => finding.target === file))
|
|
30
48
|
.map((file) => ({
|
|
31
49
|
target: file,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { inProfileDirectory, numberedLines, stringList } from "./text.js";
|
|
2
|
-
const RAW_COLOR =
|
|
2
|
+
const RAW_COLOR = /(?<!&)#[0-9a-fA-F]{3,8}\b|\b(rgb|rgba|hsl|hsla|oklch|oklab)\(|\[#/;
|
|
3
3
|
const CODE_FILE = /\.(ts|tsx|js|jsx)$/;
|
|
4
4
|
const TEST_FILE = /\.(test|spec|e2e)\.(ts|tsx|js|jsx)$/;
|
|
5
5
|
export const noRawColors = {
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
"suwgate.yml",
|
|
6
|
-
".claude/settings.json",
|
|
7
|
-
".vscode/tasks.json",
|
|
8
|
-
".template.config/template.json",
|
|
9
|
-
]);
|
|
1
|
+
import { parse } from "yaml";
|
|
2
|
+
import { baseName, isRecord } from "./text.js";
|
|
3
|
+
const CONFIG_FILE = "suwgate.yml";
|
|
4
|
+
const CALLER_FILES = new Set(["justfile", "package.json", ".claude/settings.json", ".vscode/tasks.json", ".template.config/template.json"]);
|
|
10
5
|
export function isScript(file) {
|
|
11
6
|
return file.endsWith(".sh") || /(^|\/)scripts\/(.+\/)?[^/]+\.(ts|js|mjs|py|ps1)$/.test(file);
|
|
12
7
|
}
|
|
@@ -18,16 +13,48 @@ function isCaller(file) {
|
|
|
18
13
|
/\.(sh|just|csproj|proj|props|targets)$/.test(name) ||
|
|
19
14
|
/^Dockerfile|\.Dockerfile$/.test(name) ||
|
|
20
15
|
/^(docker-)?compose(\.[\w-]+)?\.ya?ml$/.test(name) ||
|
|
16
|
+
file === "hooks/hooks.json" ||
|
|
17
|
+
file.endsWith("/hooks/hooks.json") ||
|
|
21
18
|
file.startsWith(".githooks/") ||
|
|
22
19
|
file.startsWith(".github/workflows/") ||
|
|
23
20
|
file.startsWith(".github/actions/"));
|
|
24
21
|
}
|
|
22
|
+
function strings(value) {
|
|
23
|
+
if (typeof value === "string")
|
|
24
|
+
return [value];
|
|
25
|
+
if (Array.isArray(value))
|
|
26
|
+
return value.flatMap(strings);
|
|
27
|
+
return isRecord(value) ? Object.values(value).flatMap(strings) : [];
|
|
28
|
+
}
|
|
29
|
+
function commandsOfConfig(context) {
|
|
30
|
+
let parsed;
|
|
31
|
+
try {
|
|
32
|
+
parsed = parse(context.read(CONFIG_FILE));
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return "";
|
|
36
|
+
}
|
|
37
|
+
if (!isRecord(parsed))
|
|
38
|
+
return "";
|
|
39
|
+
const steps = isRecord(parsed["steps"]) ? parsed["steps"] : {};
|
|
40
|
+
const added = Array.isArray(steps["add"]) ? steps["add"] : [];
|
|
41
|
+
return [
|
|
42
|
+
...added.flatMap((step) => (isRecord(step) ? strings(step["run"]) : [])),
|
|
43
|
+
...strings(steps["configure"]),
|
|
44
|
+
...strings(parsed["env"]),
|
|
45
|
+
...strings(parsed["dev"]),
|
|
46
|
+
].join("\n");
|
|
47
|
+
}
|
|
48
|
+
function callersOf(context) {
|
|
49
|
+
const callers = context.files.filter(isCaller).map((file) => ({ file, text: context.read(file) }));
|
|
50
|
+
return context.files.includes(CONFIG_FILE) ? [...callers, { file: CONFIG_FILE, text: commandsOfConfig(context) }] : callers;
|
|
51
|
+
}
|
|
25
52
|
export const scriptsCalled = {
|
|
26
53
|
id: "scripts-called",
|
|
27
|
-
description: "Every script (*.sh, and the files of a scripts/ folder) is called by a recipe, another script, a hook, the workflow or suwgate.yml",
|
|
54
|
+
description: "Every script (*.sh, and the files of a scripts/ folder) is called by a recipe, another script, a hook (also a Claude Code plugin's hooks/hooks.json), the workflow or a command of suwgate.yml (steps.add run, steps.configure, env, dev)",
|
|
28
55
|
profiles: "every",
|
|
29
56
|
check(context) {
|
|
30
|
-
const callers =
|
|
57
|
+
const callers = callersOf(context);
|
|
31
58
|
return context.files
|
|
32
59
|
.filter(isScript)
|
|
33
60
|
.filter((script) => !callers.some((caller) => caller.file !== script && caller.text.includes(baseName(script))))
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { npmDependencies } from "./packageManifests.js";
|
|
3
|
+
const KIT = "@maniumek/suwfrontkit";
|
|
4
|
+
const THEME = `${KIT}/theme.css`;
|
|
5
|
+
const PROFILES = ["web", "library"];
|
|
6
|
+
const MAIN_SCRIPT = /<script\b[^>]*\btype\s*=\s*["']module["'][^>]*\bsrc\s*=\s*["']\/?([^"']+)["']/;
|
|
7
|
+
const CSS_IMPORT = /\bimport\s+["'](\.{1,2}\/[^"']+\.css)["']/g;
|
|
8
|
+
function importsTheme(text) {
|
|
9
|
+
const escaped = THEME.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
|
|
10
|
+
return new RegExp(`(?:\\bimport\\s+|@import\\s+(?:url\\()?)["']${escaped}["']`).test(text);
|
|
11
|
+
}
|
|
12
|
+
function present(context, file) {
|
|
13
|
+
return context.files.includes(file);
|
|
14
|
+
}
|
|
15
|
+
function findingsIn(context, directory) {
|
|
16
|
+
const joined = (file) => (directory === "" ? file : path.posix.join(directory, file));
|
|
17
|
+
const html = joined("index.html");
|
|
18
|
+
if (!present(context, html))
|
|
19
|
+
return [];
|
|
20
|
+
const main = MAIN_SCRIPT.exec(context.read(html))?.[1];
|
|
21
|
+
if (main === undefined)
|
|
22
|
+
return [];
|
|
23
|
+
const module = joined(main);
|
|
24
|
+
if (!present(context, module))
|
|
25
|
+
return [];
|
|
26
|
+
const text = context.read(module);
|
|
27
|
+
const stylesheets = [...text.matchAll(CSS_IMPORT)].map((match) => path.posix.join(path.posix.dirname(module), match[1] ?? ""));
|
|
28
|
+
if (importsTheme(text) || stylesheets.some((sheet) => present(context, sheet) && importsTheme(context.read(sheet))))
|
|
29
|
+
return [];
|
|
30
|
+
return [
|
|
31
|
+
{
|
|
32
|
+
target: module,
|
|
33
|
+
message: `the app depends on ${KIT} but neither its main module nor a stylesheet it imports loads the kit's theme: add @import "${THEME}"; at the top of the entry stylesheet (or import "${THEME}" in ${path.posix.basename(module)}), so the kit's components get their tokens`,
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
export const suwfrontkitWiring = {
|
|
38
|
+
id: "suwfrontkit-wiring",
|
|
39
|
+
description: `A web app (an index.html with a module script) that depends on ${KIT} loads ${THEME} from its main module or a stylesheet that module imports`,
|
|
40
|
+
profiles: PROFILES,
|
|
41
|
+
check(context) {
|
|
42
|
+
const directories = [
|
|
43
|
+
...new Set(PROFILES.filter((profile) => context.profiles.includes(profile)).map((profile) => context.directories[profile] ?? "")),
|
|
44
|
+
];
|
|
45
|
+
const manifests = new Set(npmDependencies(context)
|
|
46
|
+
.filter((dependency) => dependency.name === KIT)
|
|
47
|
+
.map((dependency) => dependency.manifest));
|
|
48
|
+
return directories
|
|
49
|
+
.filter((directory) => manifests.has(directory === "" ? "package.json" : `${directory}/package.json`))
|
|
50
|
+
.flatMap((directory) => findingsIn(context, directory));
|
|
51
|
+
},
|
|
52
|
+
};
|
|
@@ -2,9 +2,36 @@ import { existsSync } from "node:fs";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { DOTNET_TEST, NO_IDLE_BUILD_PROCESSES } from "./dotnet.js";
|
|
4
4
|
import { PNPM_INSTALL } from "./node.js";
|
|
5
|
-
import { has, quoted, read, script, tracked } from "./shell.js";
|
|
6
|
-
const
|
|
7
|
-
const
|
|
5
|
+
import { has, quoted, read, script, settingsOf, tracked } from "./shell.js";
|
|
6
|
+
const DEFAULT_CONTRACT = "contract/openapi.json";
|
|
7
|
+
const API_VERSION_STEP = "api-version";
|
|
8
|
+
const CONTRACT_SETTINGS = {
|
|
9
|
+
type: "object",
|
|
10
|
+
additionalProperties: false,
|
|
11
|
+
properties: {
|
|
12
|
+
contract: {
|
|
13
|
+
description: "Where the API contract is, relative to the dotnet directory (default contract/openapi.json); api-version compares the same file, and reads its version from the package.json beside it.",
|
|
14
|
+
type: "string",
|
|
15
|
+
pattern: "^[A-Za-z0-9._-][A-Za-z0-9._/-]*\\.json$",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
const VERSION_SETTINGS = {
|
|
20
|
+
type: "object",
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
properties: {
|
|
23
|
+
breaking: {
|
|
24
|
+
description: "major (default): a breaking change of the contract needs a new major version in the package.json beside it; never: every breaking change fails, whatever the version.",
|
|
25
|
+
enum: ["major", "never"],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
function contractOf(context) {
|
|
30
|
+
return settingsOf(context, API_CONTRACT).contract ?? DEFAULT_CONTRACT;
|
|
31
|
+
}
|
|
32
|
+
function versionFileOf(contract) {
|
|
33
|
+
return path.posix.join(path.posix.dirname(contract), "package.json");
|
|
34
|
+
}
|
|
8
35
|
export const API_TYPES = "src/api/openapi.d.ts";
|
|
9
36
|
export const API_CONTRACT = "api-contract";
|
|
10
37
|
const GENERATOR = "Microsoft.Extensions.ApiDescription.Server";
|
|
@@ -22,14 +49,15 @@ function webApp(context) {
|
|
|
22
49
|
return existsSync(path.join(directory, API_TYPES)) ? directory : null;
|
|
23
50
|
}
|
|
24
51
|
export function apiContract(context) {
|
|
25
|
-
|
|
52
|
+
const contract = contractOf(context);
|
|
53
|
+
if (!has(context, contract))
|
|
26
54
|
return null;
|
|
27
55
|
const servers = tracked(context, ["*.csproj"]).filter((project) => (read(context, project) ?? "").includes(GENERATOR));
|
|
28
56
|
const [server, ...others] = servers;
|
|
29
57
|
if (server === undefined)
|
|
30
|
-
return fail(`${
|
|
58
|
+
return fail(`${contract} is there, but no project writes it: reference ${GENERATOR} in the server's project`);
|
|
31
59
|
if (others.length > 0)
|
|
32
|
-
return fail(`${
|
|
60
|
+
return fail(`${contract} has more than one project that could write it (${servers.join(", ")}): keep ${GENERATOR} in the server's project only`);
|
|
33
61
|
const name = path.basename(server, ".csproj");
|
|
34
62
|
const document = `"$work"/${quoted(`${name}.json`)}`;
|
|
35
63
|
const web = webApp(context);
|
|
@@ -41,7 +69,7 @@ export function apiContract(context) {
|
|
|
41
69
|
`rm -f ${quoted(path.join(path.dirname(server), "obj", `${name}.OpenApiFiles.cache`))}`,
|
|
42
70
|
`dotnet build ${quoted(server)} --nologo -v quiet -p:OpenApiGenerateDocumentsOnBuild=true -p:OpenApiDocumentsDirectory="$work" >"$work/build.log" || { cat "$work/build.log" >&2; exit 1; }`,
|
|
43
71
|
"stale=()",
|
|
44
|
-
`diff -q ${document} ${
|
|
72
|
+
`diff -q ${document} ${contract} >/dev/null 2>&1 || stale+=(${quoted(path.relative(context.root, path.join(context.cwd, contract)))})`,
|
|
45
73
|
...(web === null
|
|
46
74
|
? []
|
|
47
75
|
: [
|
|
@@ -53,33 +81,44 @@ export function apiContract(context) {
|
|
|
53
81
|
`echo "API contract${web === null ? "" : " and types"} up to date"`,
|
|
54
82
|
]);
|
|
55
83
|
}
|
|
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
|
-
]);
|
|
84
|
+
function apiVersionScript(contract, breaking) {
|
|
85
|
+
const versionFile = versionFileOf(contract);
|
|
86
|
+
const breakingAllowed = breaking === "never"
|
|
87
|
+
? [
|
|
88
|
+
` echo "the API contract breaks consumers (see above), and this repository allows no break (api-version breaking: never in suwgate.yml): keep the old shape and add the new one beside it." >&2`,
|
|
89
|
+
" exit 1",
|
|
90
|
+
]
|
|
91
|
+
: [
|
|
92
|
+
' if [ -n "$was" ] && [ "${now%%.*}" -gt "${was%%.*}" ]; then',
|
|
93
|
+
' echo "API contract breaks consumers since $base (see above), as the new major version $now: every consumer moves to it on purpose."',
|
|
94
|
+
" exit 0",
|
|
95
|
+
" fi",
|
|
96
|
+
` echo "the API contract breaks consumers (see above). Keep the old shape and add the new one beside it, or, agreed with every consumer, raise the major version in ${versionFile}." >&2`,
|
|
97
|
+
" exit 1",
|
|
98
|
+
];
|
|
99
|
+
return script([
|
|
100
|
+
'if ! git rev-parse --verify -q master >/dev/null && ! git rev-parse --verify -q origin/master >/dev/null; then echo "no master yet: nothing to compare the contract with"; exit 0; fi',
|
|
101
|
+
"if git show-ref --verify --quiet refs/heads/master; then base=master; else base=origin/master; fi",
|
|
102
|
+
'work="$(mktemp -d)"',
|
|
103
|
+
"trap 'rm -rf \"$work\"' EXIT",
|
|
104
|
+
`if ! git show "$base:./${contract}" >"$work/base.json" 2>/dev/null; then echo "no ${contract} on $base yet; nothing to compare against."; exit 0; fi`,
|
|
105
|
+
`if git diff --quiet --no-index "$work/base.json" ${contract} 2>/dev/null; then echo "API contract unchanged since $base"; exit 0; fi`,
|
|
106
|
+
`if ! oasdiff breaking "$work/base.json" ${contract} >"$work/load.log" 2>&1; then cat "$work/load.log" >&2; echo "oasdiff could not compare the documents (see above); is ${contract} valid JSON? Regenerate it (just api-types)" >&2; exit 1; fi`,
|
|
107
|
+
`version_of() { node -e 'let text = ""; process.stdin.on("data", (chunk) => (text += chunk)).on("end", () => console.log(JSON.parse(text).version));'; }`,
|
|
108
|
+
`now="$(version_of <${versionFile} 2>/dev/null || true)"`,
|
|
109
|
+
`was="$(git show "$base:./${versionFile}" 2>/dev/null | version_of 2>/dev/null || true)"`,
|
|
110
|
+
`version_is_higher() { [ "$1" != "$2" ] && [ "$(printf '%s\\n%s\\n' "$1" "$2" | sort -V | tail -n 1)" = "$1" ]; }`,
|
|
111
|
+
`if oasdiff breaking "$work/base.json" ${contract} --fail-on ERR; then`,
|
|
112
|
+
' if [ -n "$was" ] && ! version_is_higher "$now" "$was"; then',
|
|
113
|
+
` echo "the API contract changed since $base, but ${versionFile} still says $now. Raise its minor version (a compatible change), so a consumer can pin the new one." >&2`,
|
|
114
|
+
" exit 1",
|
|
115
|
+
" fi",
|
|
116
|
+
' echo "API contract changed since $base, compatibly (nothing breaks a consumer), as version $now"',
|
|
117
|
+
"else",
|
|
118
|
+
...breakingAllowed,
|
|
119
|
+
"fi",
|
|
120
|
+
]);
|
|
121
|
+
}
|
|
83
122
|
function pins(context) {
|
|
84
123
|
return tracked({ ...context, cwd: context.root }, TAGGED).flatMap((file) => (read({ ...context, cwd: context.root }, file) ?? "").split("\n").flatMap((line, index) => {
|
|
85
124
|
const tag = TRAIT_TAG.exec(line)?.[1] ?? TITLE_TAG.exec(line)?.[1];
|
|
@@ -100,20 +139,24 @@ export function contractPins(context) {
|
|
|
100
139
|
export const contractSteps = [
|
|
101
140
|
{
|
|
102
141
|
id: API_CONTRACT,
|
|
103
|
-
title: `the API contract and the web app's types up to date with the server (${
|
|
142
|
+
title: `the API contract and the web app's types up to date with the server (${DEFAULT_CONTRACT} or settings.contract, ${API_TYPES}, openapi-typescript)`,
|
|
104
143
|
profile: "dotnet",
|
|
105
144
|
needs: ["dotnet"],
|
|
106
145
|
after: [DOTNET_TEST, PNPM_INSTALL],
|
|
107
146
|
run: apiContract,
|
|
147
|
+
settingsSchema: CONTRACT_SETTINGS,
|
|
108
148
|
limitSeconds: 300,
|
|
109
149
|
},
|
|
110
150
|
{
|
|
111
|
-
id:
|
|
112
|
-
title:
|
|
151
|
+
id: API_VERSION_STEP,
|
|
152
|
+
title: "the API contract's version right for what changed since master (the package.json beside the contract, oasdiff breaking)",
|
|
113
153
|
profile: "dotnet",
|
|
114
154
|
needs: ["oasdiff", "node"],
|
|
115
155
|
after: [API_CONTRACT],
|
|
116
|
-
run: (context) =>
|
|
156
|
+
run: (context) => has(context, contractOf(context))
|
|
157
|
+
? apiVersionScript(contractOf(context), settingsOf(context, API_VERSION_STEP).breaking ?? "major")
|
|
158
|
+
: null,
|
|
159
|
+
settingsSchema: VERSION_SETTINGS,
|
|
117
160
|
limitSeconds: 60,
|
|
118
161
|
},
|
|
119
162
|
{
|
|
@@ -5,7 +5,7 @@ import { homedir } from "node:os";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { CONSUMER, VISUAL_CONFIG } from "./library.js";
|
|
7
7
|
import { onPath, quoted, read, script, tracked } from "./shell.js";
|
|
8
|
-
const CRITERIA_FILE = "criteria.json";
|
|
8
|
+
export const CRITERIA_FILE = "criteria.json";
|
|
9
9
|
const VDOC = {
|
|
10
10
|
repository: "Manomenu/suwalski-agent-plugin",
|
|
11
11
|
commit: "02a5f3d98c5caa9f69af3fa0508efa566a6d78f2",
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Step, StepContext } from "../gate/contract.ts";
|
|
2
|
+
import { type ReportFormat } from "./reports.ts";
|
|
3
|
+
interface TestSource {
|
|
4
|
+
step: string;
|
|
5
|
+
format: ReportFormat;
|
|
6
|
+
report: string;
|
|
7
|
+
patterns: string[];
|
|
8
|
+
skipped?: string;
|
|
9
|
+
excludeTraits?: string[];
|
|
10
|
+
}
|
|
11
|
+
export interface CriteriaRanInput {
|
|
12
|
+
root: string;
|
|
13
|
+
run: string;
|
|
14
|
+
specs: string[];
|
|
15
|
+
sources: TestSource[];
|
|
16
|
+
}
|
|
17
|
+
export declare function criterionIds(spec: string): string[];
|
|
18
|
+
export declare function names(text: string, id: string, format: ReportFormat): boolean;
|
|
19
|
+
export declare function criteriaRan(input: CriteriaRanInput): string[];
|
|
20
|
+
export declare function criteriaRanInput(context: StepContext): Omit<CriteriaRanInput, "run"> | null;
|
|
21
|
+
export declare const criteriaRanStep: Step;
|
|
22
|
+
export {};
|