@maniumek/suwgate 0.11.0 → 0.12.1
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 +6 -3
- package/dist/cli.js +15 -5
- package/dist/dev/apiTypes.d.ts +26 -0
- package/dist/dev/apiTypes.js +91 -0
- package/dist/dev/checkoutEnv.d.ts +1 -0
- package/dist/dev/checkoutEnv.js +6 -9
- package/dist/dev/index.d.ts +5 -2
- package/dist/dev/index.js +19 -3
- package/dist/dev/slowTests.d.ts +18 -0
- package/dist/dev/slowTests.js +98 -0
- package/dist/guards/dotnet/openApiVersions.js +1 -1
- package/dist/profiles/contract.d.ts +17 -0
- package/dist/profiles/contract.js +32 -10
- package/dist/profiles/index.d.ts +1 -0
- package/dist/profiles/index.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -86,13 +86,15 @@ its reason. The .NET guards and build defaults: [Suwgate.Architecture](dotnet/Su
|
|
|
86
86
|
|
|
87
87
|
<picture>
|
|
88
88
|
<source media="(prefers-color-scheme: dark)" srcset="docs/visual/board/img/readme/dev-dark.png">
|
|
89
|
-
<img alt="suwgate dev: the local stack. db (up, down, reset, psql, logs, status): the PostgreSQL of the compose file, or your own script. server (dotnet watch): restarted when it exits, output teed to a JSON log. tmux (start [--restart], stop): shell, server and web app in one session. logs (why did it fail?): the last warnings and errors; --trace one request. smoke (read-only requests): --ci builds and starts its own stack on free ports. checkout-env (eval in a worktree): own test databases and e2e ports per linked worktree. sibling (image, up, rebuild, down, status)
|
|
89
|
+
<img alt="suwgate dev: the local stack. db (up, down, reset, psql, logs, status): the PostgreSQL of the compose file, or your own script. server (dotnet watch): restarted when it exits, output teed to a JSON log. tmux (start [--restart], stop): shell, server and web app in one session. logs (why did it fail?): the last warnings and errors; --trace one request. smoke (read-only requests): --ci builds and starts its own stack on free ports. checkout-env (eval in a worktree): own test databases and e2e ports per linked worktree. sibling (image, up, rebuild, down, status): a sibling repository's images, rebuilt when its checkout moved on. api-types, new: regenerates the API contract and the web app's types, as the api-contract step generates them. slow-tests (-n count), new: a report of the slowest .NET tests. dev: in suwgate.yml: only name is required; portOffset keeps repositories on one machine apart." src="docs/visual/board/img/readme/dev-light.png" width="100%">
|
|
90
90
|
</picture>
|
|
91
91
|
|
|
92
92
|
```sh
|
|
93
93
|
suwgate dev tmux start # the whole local stack in one tmux session
|
|
94
94
|
eval "$(suwgate dev checkout-env)" # in a linked worktree: its own test databases and e2e ports
|
|
95
95
|
suwgate dev sibling suwagcms up # a sibling's images, rebuilt if its checkout moved on, then started
|
|
96
|
+
suwgate dev api-types # the API contract and the web app's types, from the server
|
|
97
|
+
suwgate dev slow-tests -n 20 # which .NET tests take the longest
|
|
96
98
|
```
|
|
97
99
|
|
|
98
100
|
Every subcommand answers `--help`. The `dev` section and what each command does: [docs/dev.md](docs/dev.md).
|
|
@@ -147,9 +149,10 @@ usage: suwgate check [--quick] [--verbose] | suwgate step [<id> [--only] [--verb
|
|
|
147
149
|
(CLAUDE_PROJECT_DIR, else the git checkout it runs in), with the lists of its profiles
|
|
148
150
|
suwgate hook stop Claude Code Stop hook: runs the quick gate unless nothing changed since it last passed
|
|
149
151
|
suwgate hook pre-commit git pre-commit hook: gitleaks on the staged changes, then the quick gate
|
|
150
|
-
suwgate dev <db|logs|smoke|server|tmux|sibling|checkout-env>
|
|
152
|
+
suwgate dev <db|logs|smoke|server|tmux|sibling|api-types|slow-tests|checkout-env>
|
|
151
153
|
the local stack of the repository (the dev section of suwgate.yml), and the images of the
|
|
152
|
-
sibling repositories it runs, rebuilt from their checkouts when stale
|
|
154
|
+
sibling repositories it runs, rebuilt from their checkouts when stale, the API contract and types regenerated
|
|
155
|
+
from the server, and a report of the slowest .NET tests; suwgate dev --help
|
|
153
156
|
suwgate agents-shared writes AGENTS.shared.md, the rules every repository shares, as this suwgate ships it, into the
|
|
154
157
|
repository root (the agents-shared guard fails until the copy is current)
|
|
155
158
|
suwgate verdict <tests|guards|tsconfig|eslint> ...
|
package/dist/cli.js
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
3
|
import { parseArgs } from "node:util";
|
|
4
4
|
import { check, step } from "./gate/check.js";
|
|
5
|
-
import { dev } from "./dev/index.js";
|
|
5
|
+
import { dev, realEnvironment } from "./dev/index.js";
|
|
6
6
|
import { loadConfig } from "./gate/config.js";
|
|
7
7
|
import { PROFILES } from "./gate/contract.js";
|
|
8
8
|
import { repositoryRoot } from "./gate/repository.js";
|
|
9
9
|
import { guards, SHARED_RULES, writeSharedRules } from "./guards/index.js";
|
|
10
|
-
import { dotnetGuards, stepsFor, verdict } from "./profiles/index.js";
|
|
10
|
+
import { discoverApiIn, dotnetGuards, stepsFor, verdict } from "./profiles/index.js";
|
|
11
11
|
import { guardEdit } from "./session/guardEdit.js";
|
|
12
12
|
import { preCommit, runTool } from "./session/preCommit.js";
|
|
13
13
|
import { stop } from "./session/stop.js";
|
|
@@ -26,9 +26,10 @@ const USAGE = `usage: suwgate check [--quick] [--verbose] | suwgate step [<id> [
|
|
|
26
26
|
(CLAUDE_PROJECT_DIR, else the git checkout it runs in), with the lists of its profiles
|
|
27
27
|
suwgate hook stop Claude Code Stop hook: runs the quick gate unless nothing changed since it last passed
|
|
28
28
|
suwgate hook pre-commit git pre-commit hook: gitleaks on the staged changes, then the quick gate
|
|
29
|
-
suwgate dev <db|logs|smoke|server|tmux|sibling|checkout-env>
|
|
29
|
+
suwgate dev <db|logs|smoke|server|tmux|sibling|api-types|slow-tests|checkout-env>
|
|
30
30
|
the local stack of the repository (the dev section of suwgate.yml), and the images of the
|
|
31
|
-
sibling repositories it runs, rebuilt from their checkouts when stale
|
|
31
|
+
sibling repositories it runs, rebuilt from their checkouts when stale, the API contract and types regenerated
|
|
32
|
+
from the server, and a report of the slowest .NET tests; suwgate dev --help
|
|
32
33
|
suwgate agents-shared writes AGENTS.shared.md, the rules every repository shares, as this suwgate ships it, into the
|
|
33
34
|
repository root (the agents-shared guard fails until the copy is current)
|
|
34
35
|
suwgate verdict <tests|guards|tsconfig|eslint> ...
|
|
@@ -48,6 +49,15 @@ function editHookProject(root) {
|
|
|
48
49
|
const settings = loaded.config.settings["hooks"];
|
|
49
50
|
return { root, profiles: loaded.config.profiles, settings: settings ?? {} };
|
|
50
51
|
}
|
|
52
|
+
function discoverApi(cwd) {
|
|
53
|
+
const root = repositoryRoot(cwd) ?? cwd;
|
|
54
|
+
const loaded = loadConfig(root, catalog);
|
|
55
|
+
if (!("config" in loaded))
|
|
56
|
+
return { problem: `suwgate.yml is not valid: ${loaded.problems.join("; ")}` };
|
|
57
|
+
const { profiles, directories, configure } = loaded.config;
|
|
58
|
+
const layout = discoverApiIn({ root, profiles, directories, contractSettings: configure["api-contract"]?.settings });
|
|
59
|
+
return "problem" in layout ? layout : { root, layout };
|
|
60
|
+
}
|
|
51
61
|
function readInput() {
|
|
52
62
|
try {
|
|
53
63
|
return readFileSync(0, "utf8");
|
|
@@ -86,7 +96,7 @@ async function runStep(argv) {
|
|
|
86
96
|
}
|
|
87
97
|
async function main(argv) {
|
|
88
98
|
if (argv[0] === "dev")
|
|
89
|
-
return dev(argv.slice(1));
|
|
99
|
+
return dev(argv.slice(1), realEnvironment(discoverApi));
|
|
90
100
|
if (argv[0] === "step")
|
|
91
101
|
return runStep(argv.slice(1));
|
|
92
102
|
if (argv[0] === "verdict") {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Exec, type Io } from "./process.ts";
|
|
2
|
+
export declare const API_TYPES_USAGE = "usage: suwgate dev api-types\nRegenerates the API contract and the web app's TypeScript types from the server, the same way the api-contract step of\nsuwgate check generates them to compare, so the two cannot disagree. The contract is where settings.contract of api-contract\nsays (default contract/openapi.json, relative to directories.dotnet); the server is the one project that references\nMicrosoft.Extensions.ApiDescription.Server; the web app is directories.web when it has src/api/openapi.d.ts.\nIt deletes the server's obj/<Name>.OpenApiFiles.cache, builds the server so that the build writes the document (no database\nneeded; the build log is printed when it fails), copies the document to the contract path, and, with a web app, runs\nopenapi-typescript (pnpm install --frozen-lockfile first when node_modules is missing) and writes src/api/openapi.d.ts with a\nfirst line naming the contract and its sha256. Prints the paths written, one per line, relative to the repository root.";
|
|
3
|
+
export interface ApiLayout {
|
|
4
|
+
cwd: string;
|
|
5
|
+
contract: string;
|
|
6
|
+
server: string;
|
|
7
|
+
name: string;
|
|
8
|
+
web: string | null;
|
|
9
|
+
}
|
|
10
|
+
export type ApiDiscovery = {
|
|
11
|
+
root: string;
|
|
12
|
+
layout: ApiLayout;
|
|
13
|
+
} | {
|
|
14
|
+
problem: string;
|
|
15
|
+
};
|
|
16
|
+
export type DiscoverApi = (cwd: string) => ApiDiscovery;
|
|
17
|
+
export declare const API_TYPES_FILE = "src/api/openapi.d.ts";
|
|
18
|
+
export interface ApiTypesEnvironment {
|
|
19
|
+
root: string;
|
|
20
|
+
layout: ApiLayout;
|
|
21
|
+
exec: Exec;
|
|
22
|
+
io: Io;
|
|
23
|
+
}
|
|
24
|
+
export declare function parseApiTypes(args: readonly string[]): void;
|
|
25
|
+
export declare function typesHeader(contract: string, digest: string): string;
|
|
26
|
+
export declare function runApiTypes(environment: ApiTypesEnvironment): Promise<number>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { Failure, UsageError } from "./process.js";
|
|
6
|
+
export const API_TYPES_USAGE = `usage: suwgate dev api-types
|
|
7
|
+
Regenerates the API contract and the web app's TypeScript types from the server, the same way the api-contract step of
|
|
8
|
+
suwgate check generates them to compare, so the two cannot disagree. The contract is where settings.contract of api-contract
|
|
9
|
+
says (default contract/openapi.json, relative to directories.dotnet); the server is the one project that references
|
|
10
|
+
Microsoft.Extensions.ApiDescription.Server; the web app is directories.web when it has src/api/openapi.d.ts.
|
|
11
|
+
It deletes the server's obj/<Name>.OpenApiFiles.cache, builds the server so that the build writes the document (no database
|
|
12
|
+
needed; the build log is printed when it fails), copies the document to the contract path, and, with a web app, runs
|
|
13
|
+
openapi-typescript (pnpm install --frozen-lockfile first when node_modules is missing) and writes src/api/openapi.d.ts with a
|
|
14
|
+
first line naming the contract and its sha256. Prints the paths written, one per line, relative to the repository root.`;
|
|
15
|
+
export const API_TYPES_FILE = "src/api/openapi.d.ts";
|
|
16
|
+
export function parseApiTypes(args) {
|
|
17
|
+
if (args.length > 0)
|
|
18
|
+
throw new UsageError(`suwgate dev api-types takes no arguments\n${API_TYPES_USAGE}`);
|
|
19
|
+
}
|
|
20
|
+
export function typesHeader(contract, digest) {
|
|
21
|
+
return `// Generated from ${contract} (sha256 ${digest}) by suwgate dev api-types; never edit.`;
|
|
22
|
+
}
|
|
23
|
+
async function buildDocument(layout, work, environment) {
|
|
24
|
+
const { exec, io } = environment;
|
|
25
|
+
const server = path.join(layout.cwd, layout.server);
|
|
26
|
+
rmSync(path.join(path.dirname(server), "obj", `${layout.name}.OpenApiFiles.cache`), { force: true });
|
|
27
|
+
const built = await exec([
|
|
28
|
+
"dotnet",
|
|
29
|
+
"build",
|
|
30
|
+
server,
|
|
31
|
+
"--nologo",
|
|
32
|
+
"-v",
|
|
33
|
+
"quiet",
|
|
34
|
+
"-p:OpenApiGenerateDocumentsOnBuild=true",
|
|
35
|
+
`-p:OpenApiDocumentsDirectory=${work}`,
|
|
36
|
+
], { capture: true });
|
|
37
|
+
if (built.status !== 0) {
|
|
38
|
+
io.err(built.stdout);
|
|
39
|
+
throw new Failure(`suwgate dev api-types: dotnet build of ${layout.server} failed (exit ${built.status}), its log is above`);
|
|
40
|
+
}
|
|
41
|
+
const document = path.join(work, `${layout.name}.json`);
|
|
42
|
+
if (!existsSync(document)) {
|
|
43
|
+
throw new Failure(`suwgate dev api-types: the build of ${layout.server} wrote no ${layout.name}.json: does ${layout.server} reference Microsoft.Extensions.ApiDescription.Server?`);
|
|
44
|
+
}
|
|
45
|
+
return document;
|
|
46
|
+
}
|
|
47
|
+
async function generateTypes(web, document, work, exec, io) {
|
|
48
|
+
if (!existsSync(path.join(web, "node_modules"))) {
|
|
49
|
+
const installed = await exec(["pnpm", "install", "--frozen-lockfile"], { cwd: web });
|
|
50
|
+
if (installed.status !== 0) {
|
|
51
|
+
throw new Failure(`suwgate dev api-types: pnpm install --frozen-lockfile failed in ${web} (exit ${installed.status})`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const schema = path.join(work, "schema.d.ts");
|
|
55
|
+
const generated = await exec(["pnpm", "exec", "openapi-typescript", document, "--output", schema], {
|
|
56
|
+
cwd: web,
|
|
57
|
+
capture: true,
|
|
58
|
+
});
|
|
59
|
+
if (generated.status !== 0) {
|
|
60
|
+
io.err(generated.stdout);
|
|
61
|
+
throw new Failure(`suwgate dev api-types: openapi-typescript failed in ${web} (exit ${generated.status}), its output is above`);
|
|
62
|
+
}
|
|
63
|
+
return readFileSync(schema, "utf8");
|
|
64
|
+
}
|
|
65
|
+
export async function runApiTypes(environment) {
|
|
66
|
+
const { root, layout, exec, io } = environment;
|
|
67
|
+
const work = mkdtempSync(path.join(tmpdir(), "suwgate-api-types-"));
|
|
68
|
+
try {
|
|
69
|
+
const document = await buildDocument(layout, work, environment);
|
|
70
|
+
const contractFile = path.join(layout.cwd, layout.contract);
|
|
71
|
+
const contract = path.relative(root, contractFile).split(path.sep).join("/");
|
|
72
|
+
const written = [contractFile];
|
|
73
|
+
let types = null;
|
|
74
|
+
if (layout.web !== null) {
|
|
75
|
+
const digest = createHash("sha256").update(readFileSync(document)).digest("hex");
|
|
76
|
+
const schema = await generateTypes(layout.web, document, work, exec, io);
|
|
77
|
+
types = { file: path.join(layout.web, API_TYPES_FILE), text: `${typesHeader(contract, digest)}\n${schema}` };
|
|
78
|
+
written.push(types.file);
|
|
79
|
+
}
|
|
80
|
+
mkdirSync(path.dirname(contractFile), { recursive: true });
|
|
81
|
+
copyFileSync(document, contractFile);
|
|
82
|
+
if (types !== null)
|
|
83
|
+
writeFileSync(types.file, types.text);
|
|
84
|
+
for (const file of written)
|
|
85
|
+
io.out(path.relative(root, file).split(path.sep).join("/"));
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
rmSync(work, { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -22,4 +22,5 @@ export interface GitCheckout {
|
|
|
22
22
|
linked: boolean;
|
|
23
23
|
}
|
|
24
24
|
export declare function gitCheckout(root: string): GitCheckout;
|
|
25
|
+
export declare function checkoutEnvOf(settings: DevSettings, env: Readonly<Record<string, string | undefined>>): CheckoutEnv;
|
|
25
26
|
export declare function runCheckoutEnv(args: readonly string[], settings: DevSettings, env: Readonly<Record<string, string | undefined>>, io: Io): number;
|
package/dist/dev/checkoutEnv.js
CHANGED
|
@@ -69,20 +69,17 @@ export function gitCheckout(root) {
|
|
|
69
69
|
}
|
|
70
70
|
return { repository: commonDir, linked: gitDir !== commonDir };
|
|
71
71
|
}
|
|
72
|
+
export function checkoutEnvOf(settings, env) {
|
|
73
|
+
const ci = (env["CI"] ?? "") !== "";
|
|
74
|
+
const checkout = ci ? { repository: settings.root, linked: false } : gitCheckout(settings.root);
|
|
75
|
+
return checkoutEnv({ checkoutName: path.basename(settings.root), ...checkout, ci, env, settings });
|
|
76
|
+
}
|
|
72
77
|
export function runCheckoutEnv(args, settings, env, io) {
|
|
73
78
|
if (args.length > 0) {
|
|
74
79
|
io.err(CHECKOUT_ENV_USAGE);
|
|
75
80
|
return 2;
|
|
76
81
|
}
|
|
77
|
-
const
|
|
78
|
-
const checkout = ci ? { repository: settings.root, linked: false } : gitCheckout(settings.root);
|
|
79
|
-
const result = checkoutEnv({
|
|
80
|
-
checkoutName: path.basename(settings.root),
|
|
81
|
-
...checkout,
|
|
82
|
-
ci,
|
|
83
|
-
env,
|
|
84
|
-
settings,
|
|
85
|
-
});
|
|
82
|
+
const result = checkoutEnvOf(settings, env);
|
|
86
83
|
io.out(exportLines(result.variables));
|
|
87
84
|
if (result.notice !== null)
|
|
88
85
|
io.err(result.notice);
|
package/dist/dev/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { type DiscoverApi } from "./apiTypes.ts";
|
|
1
2
|
import { type Exec, type Io } from "./process.ts";
|
|
2
3
|
import { type DevSettings } from "./settings.ts";
|
|
3
4
|
import { type Fetch, type SmokeCiEnvironment } from "./smoke.ts";
|
|
4
|
-
export declare const DEV_USAGE = "usage: suwgate dev <db|logs|smoke|server|tmux|sibling|checkout-env> [arguments] (every one takes --help)\nThe dev scripts of the template as commands, configured by the dev section of suwgate.yml:\n db the local PostgreSQL of the compose file: up | down | reset | psql | logs | status\n logs why did it fail? the last warnings and errors of the server's JSON log\n smoke read-only requests against a running stack; --ci builds and starts its own stack first\n server the server under dotnet watch, restarted when it exits\n tmux the whole local stack in one tmux session: start [--restart] | stop\n sibling the images of a sibling repository, rebuilt from its checkout when stale: <name> image | up | rebuild | down | status\n checkout-env eval \"$(suwgate dev checkout-env)\": own test databases and e2e ports for a linked worktree";
|
|
5
|
+
export declare const DEV_USAGE = "usage: suwgate dev <db|logs|smoke|server|tmux|sibling|api-types|slow-tests|checkout-env> [arguments] (every one takes --help)\nThe dev scripts of the template as commands, configured by the dev section of suwgate.yml:\n db the local PostgreSQL of the compose file: up | down | reset | psql | logs | status\n logs why did it fail? the last warnings and errors of the server's JSON log\n smoke read-only requests against a running stack; --ci builds and starts its own stack first\n server the server under dotnet watch, restarted when it exits\n tmux the whole local stack in one tmux session: start [--restart] | stop\n sibling the images of a sibling repository, rebuilt from its checkout when stale: <name> image | up | rebuild | down | status\n api-types regenerate the API contract and the web app's types from the server, as the api-contract step generates them\n slow-tests a report of the slowest .NET tests: -n <count>, default 10\n checkout-env eval \"$(suwgate dev checkout-env)\": own test databases and e2e ports for a linked worktree";
|
|
5
6
|
export interface DevEnvironment {
|
|
6
7
|
cwd: string;
|
|
7
8
|
env: Readonly<Record<string, string | undefined>>;
|
|
@@ -13,7 +14,9 @@ export interface DevEnvironment {
|
|
|
13
14
|
interactive: boolean;
|
|
14
15
|
ask: (question: string) => Promise<string>;
|
|
15
16
|
loadSettings: (cwd: string) => DevSettings;
|
|
17
|
+
discoverApi: DiscoverApi;
|
|
16
18
|
temporaryFile: SmokeCiEnvironment["temporaryFile"];
|
|
17
19
|
onInterrupt: SmokeCiEnvironment["onInterrupt"];
|
|
18
20
|
}
|
|
19
|
-
export declare function
|
|
21
|
+
export declare function realEnvironment(discoverApi: DiscoverApi): DevEnvironment;
|
|
22
|
+
export declare function dev(argv: readonly string[], environment: DevEnvironment): Promise<number>;
|
package/dist/dev/index.js
CHANGED
|
@@ -3,16 +3,18 @@ import { mkdtempSync, writeFileSync } from "node:fs";
|
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { setTimeout as pause } from "node:timers/promises";
|
|
6
|
+
import { API_TYPES_USAGE, parseApiTypes, runApiTypes } from "./apiTypes.js";
|
|
6
7
|
import { askOnTerminal, DB_USAGE, parseDb, runDb } from "./db.js";
|
|
7
8
|
import { CHECKOUT_ENV_USAGE, runCheckoutEnv } from "./checkoutEnv.js";
|
|
8
9
|
import { LOGS_USAGE, parseLogs, runLogs } from "./logs.js";
|
|
9
10
|
import { exec, Failure, UsageError } from "./process.js";
|
|
10
11
|
import { runServer, SERVER_USAGE } from "./server.js";
|
|
11
12
|
import { loadSettings } from "./settings.js";
|
|
13
|
+
import { parseSlowTests, runSlowTests, SLOW_TESTS_USAGE } from "./slowTests.js";
|
|
12
14
|
import { parseSibling, runSibling, SIBLING_USAGE } from "./sibling.js";
|
|
13
15
|
import { parseSmoke, realFetch, runSmoke, runSmokeCi, SMOKE_USAGE } from "./smoke.js";
|
|
14
16
|
import { parseTmux, runTmux, TMUX_USAGE } from "./tmux.js";
|
|
15
|
-
export const DEV_USAGE = `usage: suwgate dev <db|logs|smoke|server|tmux|sibling|checkout-env> [arguments] (every one takes --help)
|
|
17
|
+
export const DEV_USAGE = `usage: suwgate dev <db|logs|smoke|server|tmux|sibling|api-types|slow-tests|checkout-env> [arguments] (every one takes --help)
|
|
16
18
|
The dev scripts of the template as commands, configured by the dev section of suwgate.yml:
|
|
17
19
|
db the local PostgreSQL of the compose file: up | down | reset | psql | logs | status
|
|
18
20
|
logs why did it fail? the last warnings and errors of the server's JSON log
|
|
@@ -20,6 +22,8 @@ The dev scripts of the template as commands, configured by the dev section of su
|
|
|
20
22
|
server the server under dotnet watch, restarted when it exits
|
|
21
23
|
tmux the whole local stack in one tmux session: start [--restart] | stop
|
|
22
24
|
sibling the images of a sibling repository, rebuilt from its checkout when stale: <name> image | up | rebuild | down | status
|
|
25
|
+
api-types regenerate the API contract and the web app's types from the server, as the api-contract step generates them
|
|
26
|
+
slow-tests a report of the slowest .NET tests: -n <count>, default 10
|
|
23
27
|
checkout-env eval "$(suwgate dev checkout-env)": own test databases and e2e ports for a linked worktree`;
|
|
24
28
|
const USAGES = {
|
|
25
29
|
db: DB_USAGE,
|
|
@@ -28,6 +32,8 @@ const USAGES = {
|
|
|
28
32
|
server: SERVER_USAGE,
|
|
29
33
|
tmux: TMUX_USAGE,
|
|
30
34
|
sibling: SIBLING_USAGE,
|
|
35
|
+
"api-types": API_TYPES_USAGE,
|
|
36
|
+
"slow-tests": SLOW_TESTS_USAGE,
|
|
31
37
|
"checkout-env": CHECKOUT_ENV_USAGE,
|
|
32
38
|
};
|
|
33
39
|
function repositoryRootOf(cwd) {
|
|
@@ -61,7 +67,7 @@ function onInterrupt(cleanup) {
|
|
|
61
67
|
process.off("SIGTERM", stop);
|
|
62
68
|
};
|
|
63
69
|
}
|
|
64
|
-
function realEnvironment() {
|
|
70
|
+
export function realEnvironment(discoverApi) {
|
|
65
71
|
return {
|
|
66
72
|
cwd: process.cwd(),
|
|
67
73
|
env: process.env,
|
|
@@ -75,6 +81,7 @@ function realEnvironment() {
|
|
|
75
81
|
interactive: process.stdin.isTTY,
|
|
76
82
|
ask: askOnTerminal,
|
|
77
83
|
loadSettings: (cwd) => loadSettings(repositoryRootOf(cwd)),
|
|
84
|
+
discoverApi,
|
|
78
85
|
temporaryFile,
|
|
79
86
|
onInterrupt,
|
|
80
87
|
};
|
|
@@ -128,11 +135,20 @@ async function dispatch(command, args, environment) {
|
|
|
128
135
|
const { exec, fetch } = environment;
|
|
129
136
|
return runSibling(parsed, { settings: settings(), exec, fetch, io, pause: environment.pause });
|
|
130
137
|
}
|
|
138
|
+
case "api-types": {
|
|
139
|
+
parseApiTypes(args);
|
|
140
|
+
const found = environment.discoverApi(environment.cwd);
|
|
141
|
+
if ("problem" in found)
|
|
142
|
+
throw new Failure(`suwgate dev api-types: ${found.problem}`, 2);
|
|
143
|
+
return runApiTypes({ root: found.root, layout: found.layout, exec: environment.exec, io });
|
|
144
|
+
}
|
|
145
|
+
case "slow-tests":
|
|
146
|
+
return runSlowTests(parseSlowTests(args), { settings: settings(), env: environment.env, exec: environment.exec, io });
|
|
131
147
|
default:
|
|
132
148
|
return runCheckoutEnv(args, settings(), environment.env, io);
|
|
133
149
|
}
|
|
134
150
|
}
|
|
135
|
-
export async function dev(argv, environment
|
|
151
|
+
export async function dev(argv, environment) {
|
|
136
152
|
const [command, ...args] = argv;
|
|
137
153
|
if (command === undefined || command === "--help") {
|
|
138
154
|
environment.io.out(DEV_USAGE);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Exec, type Io } from "./process.ts";
|
|
2
|
+
import type { DevSettings } from "./settings.ts";
|
|
3
|
+
export declare const SLOW_TESTS_USAGE = "usage: suwgate dev slow-tests [-n <count>]\nWhich .NET tests take the longest? A report, not a check: it never fails for time, and suwgate check never runs it. A test far\nslower than its neighbours usually waits for something (a timeout, a server started per test) and passes anyway.\nSets the environment of suwgate dev checkout-env, starts the database like suwgate dev db up, builds the solution (the one\n*.slnx or *.sln in directories.dotnet, or in the root; none or several: exit 2), runs dotnet test --no-build with an xunit TRX\nreport in .artifacts/slow-tests (its output in run.log there), and lists the slowest tests with how long each took:\n suwgate dev slow-tests the 10 slowest\n suwgate dev slow-tests -n 25 the 25 slowest\nExit 1 when a test fails (the list still comes first), or the build or the database fails.";
|
|
4
|
+
export declare const RESULTS_FOLDER = ".artifacts/slow-tests";
|
|
5
|
+
export interface SlowTestsEnvironment {
|
|
6
|
+
settings: DevSettings;
|
|
7
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
8
|
+
exec: Exec;
|
|
9
|
+
io: Io;
|
|
10
|
+
}
|
|
11
|
+
export interface TestTime {
|
|
12
|
+
seconds: number;
|
|
13
|
+
name: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function parseSlowTests(args: readonly string[]): number;
|
|
16
|
+
export declare function parseTrx(text: string): TestTime[];
|
|
17
|
+
export declare function slowest(times: readonly TestTime[], count: number): string[];
|
|
18
|
+
export declare function runSlowTests(count: number, environment: SlowTestsEnvironment): Promise<number>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { checkoutEnvOf } from "./checkoutEnv.js";
|
|
4
|
+
import { runDb } from "./db.js";
|
|
5
|
+
import { Failure, UsageError } from "./process.js";
|
|
6
|
+
export const SLOW_TESTS_USAGE = `usage: suwgate dev slow-tests [-n <count>]
|
|
7
|
+
Which .NET tests take the longest? A report, not a check: it never fails for time, and suwgate check never runs it. A test far
|
|
8
|
+
slower than its neighbours usually waits for something (a timeout, a server started per test) and passes anyway.
|
|
9
|
+
Sets the environment of suwgate dev checkout-env, starts the database like suwgate dev db up, builds the solution (the one
|
|
10
|
+
*.slnx or *.sln in directories.dotnet, or in the root; none or several: exit 2), runs dotnet test --no-build with an xunit TRX
|
|
11
|
+
report in .artifacts/slow-tests (its output in run.log there), and lists the slowest tests with how long each took:
|
|
12
|
+
suwgate dev slow-tests the 10 slowest
|
|
13
|
+
suwgate dev slow-tests -n 25 the 25 slowest
|
|
14
|
+
Exit 1 when a test fails (the list still comes first), or the build or the database fails.`;
|
|
15
|
+
const DEFAULT_COUNT = 10;
|
|
16
|
+
export const RESULTS_FOLDER = ".artifacts/slow-tests";
|
|
17
|
+
const SECONDS_PER_MINUTE = 60;
|
|
18
|
+
const SECONDS_PER_HOUR = 3600;
|
|
19
|
+
export function parseSlowTests(args) {
|
|
20
|
+
if (args.length === 0)
|
|
21
|
+
return DEFAULT_COUNT;
|
|
22
|
+
const [flag, count, ...rest] = args;
|
|
23
|
+
if (flag !== "-n" || count === undefined || rest.length > 0 || !/^[1-9]\d*$/.test(count))
|
|
24
|
+
throw new UsageError(SLOW_TESTS_USAGE);
|
|
25
|
+
return Number(count);
|
|
26
|
+
}
|
|
27
|
+
const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
|
|
28
|
+
function unescaped(value) {
|
|
29
|
+
return value.replace(/&(?:#(\d+)|#x([0-9a-fA-F]+)|(\w+));/g, (entity, decimal, hex, named) => {
|
|
30
|
+
if (decimal !== undefined)
|
|
31
|
+
return String.fromCodePoint(Number(decimal));
|
|
32
|
+
if (hex !== undefined)
|
|
33
|
+
return String.fromCodePoint(parseInt(hex, 16));
|
|
34
|
+
return ENTITIES[named ?? ""] ?? entity;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function seconds(duration) {
|
|
38
|
+
const [hours = "0", minutes = "0", rest = "0"] = duration.split(":");
|
|
39
|
+
return Number(hours) * SECONDS_PER_HOUR + Number(minutes) * SECONDS_PER_MINUTE + Number(rest);
|
|
40
|
+
}
|
|
41
|
+
const RESULT_TAG = /<[\w.-]*:?[\w.-]*UnitTestResult((?:\s+[\w:.-]+="[^"]*")*)\s*\/?>/g;
|
|
42
|
+
const ATTRIBUTE = /([\w:.-]+)="([^"]*)"/g;
|
|
43
|
+
export function parseTrx(text) {
|
|
44
|
+
return Array.from(text.matchAll(RESULT_TAG), (tag) => {
|
|
45
|
+
const attributes = new Map(Array.from((tag[1] ?? "").matchAll(ATTRIBUTE), (attribute) => [attribute[1], attribute[2]]));
|
|
46
|
+
return { duration: attributes.get("duration"), name: attributes.get("testName") };
|
|
47
|
+
}).flatMap(({ duration, name }) => duration === undefined || duration === "" || name === undefined ? [] : [{ seconds: seconds(duration), name: unescaped(name) }]);
|
|
48
|
+
}
|
|
49
|
+
export function slowest(times, count) {
|
|
50
|
+
const sorted = [...times].sort((first, second) => second.seconds - first.seconds);
|
|
51
|
+
return [
|
|
52
|
+
`${sorted.length} tests, the ${Math.min(count, sorted.length)} slowest:`,
|
|
53
|
+
...sorted.slice(0, count).map((time) => `${time.seconds.toFixed(2).padStart(7)} s ${time.name}`),
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
function solutionOf(settings) {
|
|
57
|
+
const directory = path.join(settings.root, settings.dotnetDirectory ?? "");
|
|
58
|
+
const found = readdirSync(directory)
|
|
59
|
+
.filter((file) => file.endsWith(".slnx") || file.endsWith(".sln"))
|
|
60
|
+
.sort();
|
|
61
|
+
const [solution] = found;
|
|
62
|
+
if (solution !== undefined && found.length === 1)
|
|
63
|
+
return path.join(directory, solution);
|
|
64
|
+
throw new Failure(found.length === 0
|
|
65
|
+
? `suwgate dev slow-tests: no .slnx or .sln in ${directory}: set directories.dotnet in suwgate.yml to the folder of the solution`
|
|
66
|
+
: `suwgate dev slow-tests: ${found.length} solutions in ${directory} (${found.join(", ")}): keep one, or point directories.dotnet at the folder of the one to run`, 2);
|
|
67
|
+
}
|
|
68
|
+
export async function runSlowTests(count, environment) {
|
|
69
|
+
const { settings, io } = environment;
|
|
70
|
+
const solution = solutionOf(settings);
|
|
71
|
+
const checkout = checkoutEnvOf(settings, environment.env);
|
|
72
|
+
if (checkout.notice !== null)
|
|
73
|
+
io.err(checkout.notice);
|
|
74
|
+
const variables = Object.fromEntries(checkout.variables);
|
|
75
|
+
const exec = (argv, options) => environment.exec(argv, { ...options, env: { ...variables, ...options?.env } });
|
|
76
|
+
const results = path.join(settings.root, RESULTS_FOLDER);
|
|
77
|
+
rmSync(results, { recursive: true, force: true });
|
|
78
|
+
mkdirSync(results, { recursive: true });
|
|
79
|
+
const database = await runDb({ kind: "up" }, { settings, exec, io: { out: () => undefined, err: (text) => io.err(text) }, interactive: false, ask: () => Promise.resolve("") });
|
|
80
|
+
if (database !== 0)
|
|
81
|
+
throw new Failure(`suwgate dev slow-tests: the database did not start (exit ${database})`);
|
|
82
|
+
const built = await exec(["dotnet", "build", solution, "--nologo", "-v", "q"], { capture: true });
|
|
83
|
+
if (built.status !== 0) {
|
|
84
|
+
io.err(built.stdout);
|
|
85
|
+
throw new Failure(`suwgate dev slow-tests: dotnet build of ${solution} failed (exit ${built.status}), its output is above`);
|
|
86
|
+
}
|
|
87
|
+
const log = path.join(results, "run.log");
|
|
88
|
+
const tested = await exec(["dotnet", "test", "--solution", solution, "--no-build", "--no-progress", "--report-xunit-trx", "--results-directory", results], { capture: true, quiet: true });
|
|
89
|
+
writeFileSync(log, tested.stdout);
|
|
90
|
+
const times = readdirSync(results)
|
|
91
|
+
.filter((file) => file.endsWith(".trx"))
|
|
92
|
+
.flatMap((file) => parseTrx(readFileSync(path.join(results, file), "utf8")));
|
|
93
|
+
for (const line of slowest(times, count))
|
|
94
|
+
io.out(line);
|
|
95
|
+
if (tested.status !== 0)
|
|
96
|
+
throw new Failure(`suwgate dev slow-tests: a test failed; the run's output is in ${log}`);
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
@@ -30,7 +30,7 @@ export const openApiVersions = {
|
|
|
30
30
|
{
|
|
31
31
|
target: file,
|
|
32
32
|
message: library === undefined
|
|
33
|
-
? `${TOOL} ${tool} loads the server's ${LIBRARY}, which only comes in through a package (Suwbackkit.Server) and can be another release, so the build of the contract fails with a Microsoft.OpenApi version mismatch: pin it beside the tool, ${pin} (
|
|
33
|
+
? `${TOOL} ${tool} loads the server's ${LIBRARY}, which only comes in through a package (Suwbackkit.Server) and can be another release, so the build of the contract fails with a Microsoft.OpenApi version mismatch: pin it beside the tool, ${pin}, and reference it from the server's project (<PackageReference Include="${LIBRARY}" />, which unused-nuget-versions asks for), so an update moves both`
|
|
34
34
|
: `${TOOL} is ${tool} but ${LIBRARY} is ${library}: the tool loads the server's OpenAPI library and fails on a Microsoft.OpenApi version mismatch, so give both the same version`,
|
|
35
35
|
},
|
|
36
36
|
];
|
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import type { Step, StepContext } from "../gate/contract.ts";
|
|
2
2
|
export declare const API_TYPES = "src/api/openapi.d.ts";
|
|
3
3
|
export declare const API_CONTRACT = "api-contract";
|
|
4
|
+
export interface ApiLayout {
|
|
5
|
+
cwd: string;
|
|
6
|
+
contract: string;
|
|
7
|
+
server: string;
|
|
8
|
+
name: string;
|
|
9
|
+
web: string | null;
|
|
10
|
+
}
|
|
11
|
+
export interface ApiProblem {
|
|
12
|
+
problem: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ApiRepository {
|
|
15
|
+
root: string;
|
|
16
|
+
profiles: StepContext["profiles"];
|
|
17
|
+
directories: StepContext["directories"];
|
|
18
|
+
contractSettings: unknown;
|
|
19
|
+
}
|
|
20
|
+
export declare function discoverApiIn(repository: ApiRepository): ApiLayout | ApiProblem;
|
|
4
21
|
export declare function apiContract(context: StepContext): string | null;
|
|
5
22
|
export declare function contractPins(context: StepContext): string | null;
|
|
6
23
|
export declare const contractSteps: readonly Step[];
|
|
@@ -48,19 +48,41 @@ function webApp(context) {
|
|
|
48
48
|
const directory = path.join(context.root, context.directories.web ?? "");
|
|
49
49
|
return existsSync(path.join(directory, API_TYPES)) ? directory : null;
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
function discoverApi(context) {
|
|
52
52
|
const contract = contractOf(context);
|
|
53
|
-
if (!has(context, contract))
|
|
54
|
-
return null;
|
|
55
53
|
const servers = tracked(context, ["*.csproj"]).filter((project) => (read(context, project) ?? "").includes(GENERATOR));
|
|
56
54
|
const [server, ...others] = servers;
|
|
57
55
|
if (server === undefined)
|
|
58
|
-
return
|
|
59
|
-
if (others.length > 0)
|
|
60
|
-
return
|
|
61
|
-
|
|
56
|
+
return { problem: `no project writes ${contract}: reference ${GENERATOR} in the server's project` };
|
|
57
|
+
if (others.length > 0) {
|
|
58
|
+
return {
|
|
59
|
+
problem: `${contract} has more than one project that could write it (${servers.join(", ")}): keep ${GENERATOR} in the server's project only`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return { cwd: context.cwd, contract, server, name: path.basename(server, ".csproj"), web: webApp(context) };
|
|
63
|
+
}
|
|
64
|
+
export function discoverApiIn(repository) {
|
|
65
|
+
const settings = repository.contractSettings === undefined ? {} : { [API_CONTRACT]: repository.contractSettings };
|
|
66
|
+
return discoverApi({
|
|
67
|
+
root: repository.root,
|
|
68
|
+
cwd: path.join(repository.root, repository.directories.dotnet ?? ""),
|
|
69
|
+
quick: false,
|
|
70
|
+
profiles: repository.profiles,
|
|
71
|
+
directories: repository.directories,
|
|
72
|
+
skippedSteps: [],
|
|
73
|
+
skippedGuards: [],
|
|
74
|
+
settings,
|
|
75
|
+
changed: () => [],
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
export function apiContract(context) {
|
|
79
|
+
if (!has(context, contractOf(context)))
|
|
80
|
+
return null;
|
|
81
|
+
const found = discoverApi(context);
|
|
82
|
+
if ("problem" in found)
|
|
83
|
+
return fail(found.problem);
|
|
84
|
+
const { contract, server, name, web } = found;
|
|
62
85
|
const document = `"$work"/${quoted(`${name}.json`)}`;
|
|
63
|
-
const web = webApp(context);
|
|
64
86
|
const types = web === null ? "" : path.join(web, API_TYPES);
|
|
65
87
|
return script([
|
|
66
88
|
NO_IDLE_BUILD_PROCESSES,
|
|
@@ -77,7 +99,7 @@ export function apiContract(context) {
|
|
|
77
99
|
`(cd ${quoted(web)} && pnpm exec openapi-typescript ${document} --output "$work/schema.d.ts" >/dev/null)`,
|
|
78
100
|
`if ! head -n 1 ${quoted(types)} | grep -q "sha256 $digest" || ! tail -n +2 ${quoted(types)} | diff -q - "$work/schema.d.ts" >/dev/null; then stale+=(${quoted(path.relative(context.root, types))}); fi`,
|
|
79
101
|
]),
|
|
80
|
-
'if [ ${#stale[@]} -gt 0 ]; then echo "${stale[*]} out of date with the server\'s API — regenerate them (
|
|
102
|
+
'if [ ${#stale[@]} -gt 0 ]; then echo "${stale[*]} out of date with the server\'s API — regenerate them (suwgate dev api-types)" >&2; exit 1; fi',
|
|
81
103
|
`echo "API contract${web === null ? "" : " and types"} up to date"`,
|
|
82
104
|
]);
|
|
83
105
|
}
|
|
@@ -103,7 +125,7 @@ function apiVersionScript(contract, breaking) {
|
|
|
103
125
|
"trap 'rm -rf \"$work\"' EXIT",
|
|
104
126
|
`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
127
|
`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 (
|
|
128
|
+
`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 (suwgate dev api-types)" >&2; exit 1; fi`,
|
|
107
129
|
`version_of() { node -e 'let text = ""; process.stdin.on("data", (chunk) => (text += chunk)).on("end", () => console.log(JSON.parse(text).version));'; }`,
|
|
108
130
|
`now="$(version_of <${versionFile} 2>/dev/null || true)"`,
|
|
109
131
|
`was="$(git show "$base:./${versionFile}" 2>/dev/null | version_of 2>/dev/null || true)"`,
|
package/dist/profiles/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Profile, type Step } from "../gate/contract.ts";
|
|
2
|
+
export { discoverApiIn } from "./contract.ts";
|
|
2
3
|
export { dotnetGuards } from "./dotnetGuards.ts";
|
|
3
4
|
export { verdict } from "./verdict.ts";
|
|
4
5
|
export declare function stepsFor(profiles: readonly Profile[]): Step[];
|
package/dist/profiles/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import { everyRepositorySteps } from "./everyRepository.js";
|
|
|
6
6
|
import { librarySteps } from "./library.js";
|
|
7
7
|
import { nodeSteps } from "./node.js";
|
|
8
8
|
import { webSteps } from "./web.js";
|
|
9
|
+
export { discoverApiIn } from "./contract.js";
|
|
9
10
|
export { dotnetGuards } from "./dotnetGuards.js";
|
|
10
11
|
export { verdict } from "./verdict.js";
|
|
11
12
|
const BY_PROFILE = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maniumek/suwgate",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "One quality gate for every repository: suwgate check runs the steps of the repository's profiles and its guards, configured in suwgate.yml",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Manomenu",
|