@punks/cli 5.2.0 → 5.2.2
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 +2 -2
- package/dist/BASELINE_CHANGELOG.md +46 -1
- package/dist/CHANGELOG.md +37 -0
- package/dist/baseline/scaffold-baseline/baseline.json +15 -5
- package/dist/baseline/scaffold-baseline/data/catalog/packs.json +2 -0
- package/dist/baseline/scaffold-baseline/data/catalog/skills.json +16 -0
- package/dist/baseline/scaffold-baseline/data/hooks/format-edited-file.mjs +459 -318
- package/dist/baseline/scaffold-baseline/data/scripts/commit-gate-runner.d.ts +1 -0
- package/dist/baseline/scaffold-baseline/data/scripts/commit-gate-runner.mjs +98 -27
- package/dist/baseline/scaffold-baseline/data/scripts/commit-gate-runner.test.ts +156 -8
- package/dist/baseline/scaffold-baseline/data/scripts/managed-lint-runner.d.mts +55 -0
- package/dist/baseline/scaffold-baseline/data/scripts/managed-lint-runner.mjs +329 -0
- package/dist/baseline/scaffold-baseline/data/scripts/managed-lint-runner.test.ts +864 -0
- package/dist/baseline/scaffold-baseline/data/scripts/managed-lint.d.mts +97 -0
- package/dist/baseline/scaffold-baseline/data/scripts/managed-lint.mjs +658 -0
- package/dist/baseline/scaffold-baseline/data/scripts/managed-lint.test.ts +614 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/SKILL.md +2 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/references/commands.md +8 -9
- package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/references/managed-lint.md +165 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/references/post-command-flow.md +28 -24
- package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/references/wiki-structure.md +16 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/docs/docs-onboarding/SKILL.md +21 -16
- package/dist/baseline/scaffold-baseline/skills/agnostic/docs/docs-onboarding/agents/openai.yaml +1 -1
- package/dist/baseline/scaffold-baseline/skills/agnostic/docs/docs-onboarding/references/artifact-contract.md +16 -4
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-architecture/SKILL.md +65 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-architecture/assets/ARCHITECTURE-TEMPLATE.md +119 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-architecture/references/artifact-contract.md +200 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-plan/SKILL.md +11 -3
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-plan/references/architecture-convergence.md +19 -4
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-plan/references/plan-schema.md +17 -3
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-spec/SKILL.md +38 -20
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-spec/assets/SPEC-TEMPLATE.md +11 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-spec/references/readiness.md +40 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/create-spec/references/spec-quality-bar.md +8 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/implement-spec/SKILL.md +4 -2
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/implement-spec/references/architecture-conformance.md +10 -6
- package/dist/baseline/scaffold-baseline/skills/agnostic/planning/implement-spec/references/lifecycle.md +23 -10
- package/dist/baseline/scaffold-baseline/skills/agnostic/requirements/brainstorm/SKILL.md +22 -0
- package/dist/baseline/scaffold-baseline/skills/agnostic/requirements/requirements-grill/SKILL.md +2 -1
- package/dist/baseline/scaffold-baseline/skills/agnostic/requirements/requirements-grill/references/artifact-output.md +3 -2
- package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/SKILL.md +6 -3
- package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/plan.md +4 -2
- package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/review.md +5 -1
- package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/router.md +15 -9
- package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/spec.md +13 -26
- package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/references/artifact-state.md +39 -1
- package/dist/baseline/scaffold-baseline/skills/phases/requirements-phase/SKILL.md +31 -14
- package/dist/baseline/scaffold-baseline/skills/phases/review-phase/phases/prepare-review.md +12 -4
- package/dist/baseline/scaffold-baseline.json +5 -5
- package/dist/baseline/scaffold-baseline.tgz +0 -0
- package/dist/data/hooks/format-edited-file.d.mts +5 -0
- package/dist/data/hooks/format-edited-file.mjs +282 -333
- package/dist/data/scripts/commit-gate-runner.mjs +98 -27
- package/dist/data/scripts/managed-lint-runner.d.mts +55 -0
- package/dist/data/scripts/managed-lint-runner.mjs +329 -0
- package/dist/data/scripts/managed-lint.d.mts +97 -0
- package/dist/data/scripts/managed-lint.mjs +658 -0
- package/dist/index.js +86 -1184
- package/dist/skills/agnostic/cli/hi-cli/SKILL.md +2 -0
- package/dist/skills/agnostic/cli/hi-cli/references/commands.md +8 -9
- package/dist/skills/agnostic/cli/hi-cli/references/managed-lint.md +165 -0
- package/dist/skills/agnostic/cli/hi-cli/references/post-command-flow.md +15 -11
- package/dist/skills/agnostic/cli/hi-cli/references/wiki-structure.md +16 -0
- package/dist/skills/agnostic/docs/docs-onboarding/SKILL.md +21 -16
- package/dist/skills/agnostic/docs/docs-onboarding/agents/openai.yaml +1 -1
- package/dist/skills/agnostic/docs/docs-onboarding/references/artifact-contract.md +16 -4
- package/dist/skills/agnostic/planning/create-architecture/SKILL.md +65 -0
- package/dist/skills/agnostic/planning/create-architecture/assets/ARCHITECTURE-TEMPLATE.md +119 -0
- package/dist/skills/agnostic/planning/create-architecture/references/artifact-contract.md +200 -0
- package/dist/skills/agnostic/planning/create-plan/SKILL.md +11 -3
- package/dist/skills/agnostic/planning/create-plan/references/architecture-convergence.md +19 -4
- package/dist/skills/agnostic/planning/create-plan/references/plan-schema.md +17 -3
- package/dist/skills/agnostic/planning/create-spec/SKILL.md +38 -20
- package/dist/skills/agnostic/planning/create-spec/assets/SPEC-TEMPLATE.md +11 -0
- package/dist/skills/agnostic/planning/create-spec/references/readiness.md +40 -0
- package/dist/skills/agnostic/planning/create-spec/references/spec-quality-bar.md +8 -0
- package/dist/skills/agnostic/planning/implement-spec/SKILL.md +4 -2
- package/dist/skills/agnostic/planning/implement-spec/references/architecture-conformance.md +10 -6
- package/dist/skills/agnostic/planning/implement-spec/references/lifecycle.md +23 -10
- package/dist/skills/agnostic/requirements/brainstorm/SKILL.md +22 -0
- package/dist/skills/agnostic/requirements/requirements-grill/SKILL.md +2 -1
- package/dist/skills/agnostic/requirements/requirements-grill/references/artifact-output.md +3 -2
- package/dist/skills/phases/delivery-phase/SKILL.md +6 -3
- package/dist/skills/phases/delivery-phase/phases/plan.md +4 -2
- package/dist/skills/phases/delivery-phase/phases/review.md +5 -1
- package/dist/skills/phases/delivery-phase/phases/router.md +15 -9
- package/dist/skills/phases/delivery-phase/phases/spec.md +13 -26
- package/dist/skills/phases/delivery-phase/references/artifact-state.md +39 -1
- package/dist/skills/phases/requirements-phase/SKILL.md +31 -14
- package/dist/skills/phases/review-phase/phases/prepare-review.md +12 -4
- package/package.json +3 -2
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
|
|
3
4
|
import { execFile as execFileCallback } from "node:child_process";
|
|
4
5
|
import { readFile } from "node:fs/promises";
|
|
@@ -6,7 +7,25 @@ import process from "node:process";
|
|
|
6
7
|
import path from "node:path";
|
|
7
8
|
import { promisify } from "node:util";
|
|
8
9
|
|
|
10
|
+
import { loadManagedLint } from "./managed-lint-runner.mjs";
|
|
11
|
+
|
|
9
12
|
const execFile = promisify(execFileCallback);
|
|
13
|
+
/** @param {unknown} value - Untrusted decoded input. @returns {value is Record<string, unknown>} */
|
|
14
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
15
|
+
/** @param {unknown} value - Decoded generated contract. @returns {value is import("./commit-gate-runner.d.ts").CommitGateRunnerContract} */
|
|
16
|
+
const isContract = (value) =>
|
|
17
|
+
isRecord(value) &&
|
|
18
|
+
typeof value.lint === "string" &&
|
|
19
|
+
typeof value.formatCheck === "string" &&
|
|
20
|
+
["owner", "path", "root"].every(
|
|
21
|
+
(key) => value[key] === undefined || typeof value[key] === "string",
|
|
22
|
+
) &&
|
|
23
|
+
(value.execution === undefined ||
|
|
24
|
+
(isRecord(value.execution) &&
|
|
25
|
+
["lint", "formatCheck"].every(
|
|
26
|
+
(key) => value.execution[key] === undefined || typeof value.execution[key] === "string",
|
|
27
|
+
)));
|
|
28
|
+
/** @param {string} command - Authored check command. @param {string} cwd - Selected owner. @param {NodeJS.ProcessEnv} environment - Staged inputs. */
|
|
10
29
|
|
|
11
30
|
const run = async (command, cwd, environment) => {
|
|
12
31
|
try {
|
|
@@ -17,34 +36,33 @@ const run = async (command, cwd, environment) => {
|
|
|
17
36
|
});
|
|
18
37
|
return { code: 0, launchError: "", stderr: result.stderr, stdout: result.stdout };
|
|
19
38
|
} catch (error) {
|
|
39
|
+
/** @type {unknown} */
|
|
40
|
+
const failure = error;
|
|
41
|
+
const details = isRecord(failure) ? failure : {};
|
|
42
|
+
const message = failure instanceof Error ? failure.message : String(failure);
|
|
20
43
|
const launchError =
|
|
21
|
-
typeof
|
|
44
|
+
typeof details.code === "number"
|
|
22
45
|
? ""
|
|
23
|
-
:
|
|
24
|
-
typeof error.code === "string" ? error.code : "",
|
|
25
|
-
error instanceof Error ? error.message : String(error),
|
|
26
|
-
]
|
|
27
|
-
.filter(Boolean)
|
|
28
|
-
.join(": ");
|
|
46
|
+
: `${typeof details.code === "string" ? details.code : ""}: ${message}`;
|
|
29
47
|
return {
|
|
30
|
-
code: typeof
|
|
48
|
+
code: typeof details.code === "number" ? details.code : 1,
|
|
31
49
|
launchError,
|
|
32
|
-
stderr:
|
|
33
|
-
stdout:
|
|
50
|
+
stderr: typeof details.stderr === "string" ? details.stderr : "",
|
|
51
|
+
stdout: typeof details.stdout === "string" ? details.stdout : "",
|
|
34
52
|
};
|
|
35
53
|
}
|
|
36
54
|
};
|
|
37
55
|
|
|
38
56
|
const stagedPaths = async () => {
|
|
39
|
-
const { stdout } = await execFile(
|
|
40
|
-
"
|
|
41
|
-
"--cached",
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"-z",
|
|
45
|
-
]);
|
|
57
|
+
const { stdout } = await execFile(
|
|
58
|
+
"git",
|
|
59
|
+
["diff", "--cached", "--name-status", "--find-renames", "-z"],
|
|
60
|
+
{},
|
|
61
|
+
);
|
|
46
62
|
const entries = stdout.split("\0").filter(Boolean);
|
|
63
|
+
/** @type {Set<string>} */
|
|
47
64
|
const triggers = new Set();
|
|
65
|
+
/** @type {Set<string>} */
|
|
48
66
|
const files = new Set();
|
|
49
67
|
for (let index = 0; index < entries.length; index += 2) {
|
|
50
68
|
const status = entries[index];
|
|
@@ -65,12 +83,14 @@ const stagedPaths = async () => {
|
|
|
65
83
|
};
|
|
66
84
|
|
|
67
85
|
const activeWorktreeRoot = async () => {
|
|
68
|
-
const { stdout } = await execFile("git", ["rev-parse", "--show-toplevel"]);
|
|
86
|
+
const { stdout } = await execFile("git", ["rev-parse", "--show-toplevel"], {});
|
|
69
87
|
return stdout.replace(/\n$/u, "");
|
|
70
88
|
};
|
|
71
89
|
|
|
90
|
+
/** @param {string} value - A filename passed through an explicit authored shell template. */
|
|
72
91
|
const shellQuote = (value) => `'${value.replaceAll("'", "'\\''")}'`;
|
|
73
92
|
|
|
93
|
+
/** @param {string} name - Public command option. */
|
|
74
94
|
const argument = (name) => {
|
|
75
95
|
const index = process.argv.indexOf(name);
|
|
76
96
|
return index === -1 ? null : (process.argv[index + 1] ?? null);
|
|
@@ -83,14 +103,52 @@ if (contractsPath === null) {
|
|
|
83
103
|
} else {
|
|
84
104
|
try {
|
|
85
105
|
const contractsContent = await readFile(contractsPath, "utf-8");
|
|
86
|
-
|
|
106
|
+
/** @type {unknown} */
|
|
107
|
+
const decodedContracts = JSON.parse(contractsContent);
|
|
108
|
+
if (!Array.isArray(decodedContracts) || !decodedContracts.every(isContract)) {
|
|
109
|
+
throw new Error("Commit Gate contracts must contain explicit lint and formatCheck commands.");
|
|
110
|
+
}
|
|
111
|
+
const contracts = decodedContracts;
|
|
87
112
|
const worktreeRoot = await activeWorktreeRoot();
|
|
88
113
|
const staged = await stagedPaths();
|
|
89
114
|
if (staged.triggers.length === 0) {
|
|
90
115
|
process.exit(0);
|
|
91
116
|
}
|
|
92
117
|
|
|
118
|
+
const resolution = await loadManagedLint(worktreeRoot, staged.triggers);
|
|
119
|
+
if (resolution.status === "disabled") {
|
|
120
|
+
process.exit(0);
|
|
121
|
+
}
|
|
122
|
+
if (resolution.status !== "ready") {
|
|
123
|
+
throw new Error(resolution.diagnostics.join("\n"));
|
|
124
|
+
}
|
|
125
|
+
const routes = new Map(resolution.routes.map((route) => [route.scope, route]));
|
|
126
|
+
const eligible = new Set(resolution.routes.flatMap((route) => route.files));
|
|
127
|
+
staged.triggers = staged.triggers.filter(
|
|
128
|
+
(file) =>
|
|
129
|
+
eligible.has(file) ||
|
|
130
|
+
resolution.routes.some(
|
|
131
|
+
(route) =>
|
|
132
|
+
route.triggered &&
|
|
133
|
+
(route.inputs.includes(file) ||
|
|
134
|
+
route.config === file ||
|
|
135
|
+
file === ".devpunks/settings.json" ||
|
|
136
|
+
file === ".devpunks/specs/lint/selection.json"),
|
|
137
|
+
),
|
|
138
|
+
);
|
|
139
|
+
staged.files = staged.files.filter((file) => eligible.has(file));
|
|
140
|
+
if (staged.triggers.length === 0) {
|
|
141
|
+
process.exit(0);
|
|
142
|
+
}
|
|
143
|
+
const complete = resolution.routes.some((route) => route.triggered)
|
|
144
|
+
? await loadManagedLint(worktreeRoot)
|
|
145
|
+
: resolution;
|
|
146
|
+
if (complete.status !== "ready") {
|
|
147
|
+
throw new Error(complete.diagnostics.join("\n"));
|
|
148
|
+
}
|
|
149
|
+
/** @type {Map<string, {checks: {kind: string, owner: string}[], command: string, cwd: string, files: string[]}>} */
|
|
93
150
|
const commands = new Map();
|
|
151
|
+
/** @type {readonly ("lint" | "formatCheck")[]} */
|
|
94
152
|
const kinds = ["lint", "formatCheck"];
|
|
95
153
|
const repositoryKinds = new Set(
|
|
96
154
|
kinds.filter((kind) =>
|
|
@@ -100,19 +158,32 @@ if (contractsPath === null) {
|
|
|
100
158
|
const covered = new Map(kinds.map((kind) => [kind, new Set()]));
|
|
101
159
|
const coverageFailures = [];
|
|
102
160
|
for (const contract of contracts) {
|
|
103
|
-
const relativePath = contract.path ?? ".";
|
|
161
|
+
const relativePath = path.posix.normalize(contract.path ?? ".");
|
|
104
162
|
const ownerCwd = path.resolve(worktreeRoot, relativePath);
|
|
105
163
|
const owner = contract.owner ?? ownerCwd;
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
164
|
+
const route = routes.get(relativePath);
|
|
165
|
+
const ownerTriggers = staged.triggers.filter(
|
|
166
|
+
(file) =>
|
|
167
|
+
(route?.files.includes(file) ?? false) ||
|
|
168
|
+
(route?.triggered === true &&
|
|
169
|
+
(route.inputs.includes(file) ||
|
|
170
|
+
route.config === file ||
|
|
171
|
+
file === ".devpunks/settings.json" ||
|
|
172
|
+
file === ".devpunks/specs/lint/selection.json")),
|
|
173
|
+
);
|
|
174
|
+
const ownerFiles =
|
|
175
|
+
route?.triggered === true
|
|
176
|
+
? (complete.routes.find((item) => item.scope === relativePath)?.files ?? [])
|
|
177
|
+
: staged.files.filter((file) => route?.files.includes(file) ?? false);
|
|
113
178
|
for (const kind of kinds) {
|
|
114
179
|
const template = contract[kind];
|
|
115
180
|
const mode = contract.execution?.[kind];
|
|
181
|
+
const triggers = mode === "repository" ? staged.triggers : ownerTriggers;
|
|
182
|
+
const routedFiles = mode === "repository" ? staged.files : ownerFiles;
|
|
183
|
+
const files = routedFiles.map((file) =>
|
|
184
|
+
path.relative(ownerCwd, path.resolve(worktreeRoot, file)).split(path.sep).join("/"),
|
|
185
|
+
);
|
|
186
|
+
const fileArguments = files.map(shellQuote).join(" ");
|
|
116
187
|
if (repositoryKinds.has(kind) && mode !== "repository") {
|
|
117
188
|
continue;
|
|
118
189
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ManagedLintResolution, ManagedLintCommand } from "./managed-lint.mjs";
|
|
2
|
+
import type { SpawnSyncReturns } from "node:child_process";
|
|
3
|
+
|
|
4
|
+
export interface ManagedLintRunOptions {
|
|
5
|
+
readonly root: string;
|
|
6
|
+
readonly scope?: string | undefined;
|
|
7
|
+
readonly paths?: readonly string[] | undefined;
|
|
8
|
+
readonly output?: "human" | "json" | undefined;
|
|
9
|
+
}
|
|
10
|
+
export interface ManagedLintDiagnostic {
|
|
11
|
+
readonly message: string;
|
|
12
|
+
readonly filename?: string;
|
|
13
|
+
readonly code?: string;
|
|
14
|
+
readonly severity?: string;
|
|
15
|
+
readonly [key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
export type ReadyManagedLintCommand = Extract<ManagedLintCommand, { readonly status: "ready" }>;
|
|
18
|
+
export interface ManagedLintExecutionOptions {
|
|
19
|
+
readonly root: string;
|
|
20
|
+
/** A resolver-produced command, optionally with --fix for a controlled mirror. */
|
|
21
|
+
readonly command: ReadyManagedLintCommand;
|
|
22
|
+
readonly output?: "human" | "json";
|
|
23
|
+
}
|
|
24
|
+
/** Execute and classify one local command using the same rules as root/package runs. */
|
|
25
|
+
export function executeManagedLintCommand(
|
|
26
|
+
options: ManagedLintExecutionOptions,
|
|
27
|
+
): ManagedLintExecution;
|
|
28
|
+
export interface ManagedLintExecution extends Readonly<
|
|
29
|
+
Pick<SpawnSyncReturns<string>, "status" | "signal" | "error">
|
|
30
|
+
> {
|
|
31
|
+
/** Normalized classification; native process evidence remains in status/signal/error. */
|
|
32
|
+
readonly code: 0 | 1 | 2;
|
|
33
|
+
readonly command: ReadyManagedLintCommand;
|
|
34
|
+
readonly scope: string;
|
|
35
|
+
readonly stdout: string;
|
|
36
|
+
readonly stderr: string;
|
|
37
|
+
readonly diagnostics?: readonly ManagedLintDiagnostic[];
|
|
38
|
+
readonly failure?: {
|
|
39
|
+
readonly kind: "config-load" | "process" | "malformed-output";
|
|
40
|
+
readonly phase: "config" | "lint";
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface ManagedLintRunResult {
|
|
44
|
+
readonly code: 0 | 1 | 2;
|
|
45
|
+
readonly status: "clean" | "findings" | "operational-failure" | "conflict" | "selection-needed";
|
|
46
|
+
readonly diagnostics?: readonly string[];
|
|
47
|
+
readonly results?: readonly ManagedLintExecution[];
|
|
48
|
+
}
|
|
49
|
+
export function loadManagedLint(
|
|
50
|
+
root: string,
|
|
51
|
+
paths?: readonly string[],
|
|
52
|
+
scope?: string,
|
|
53
|
+
): Promise<ManagedLintResolution>;
|
|
54
|
+
export function managedLintEnvironment(root: string, cwd: string): NodeJS.ProcessEnv;
|
|
55
|
+
export function runManagedLint(options: ManagedLintRunOptions): Promise<ManagedLintRunResult>;
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { readFile, access } from "node:fs/promises";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { resolveManagedLint, buildManagedLintCommand } from "./managed-lint.mjs";
|
|
7
|
+
|
|
8
|
+
/** @param {unknown} error - A filesystem or subprocess boundary failure. */
|
|
9
|
+
const missingFile = (error) => error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
10
|
+
/** @param {unknown} error - A filesystem or subprocess boundary failure. */
|
|
11
|
+
const errorMessage = (error) => (error instanceof Error ? error.message : String(error));
|
|
12
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
13
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
14
|
+
/** @param {unknown} value @returns {value is import("./managed-lint-runner.mjs").ManagedLintDiagnostic} */
|
|
15
|
+
const isDiagnostic = (value) =>
|
|
16
|
+
isRecord(value) &&
|
|
17
|
+
typeof value.message === "string" &&
|
|
18
|
+
["filename", "code", "severity"].every(
|
|
19
|
+
(key) => value[key] === undefined || typeof value[key] === "string",
|
|
20
|
+
);
|
|
21
|
+
/** @param {string} file @param {unknown} [missingValue] @returns {Promise<unknown>} */
|
|
22
|
+
const readJson = async (file, missingValue = null) => {
|
|
23
|
+
try {
|
|
24
|
+
/** @type {unknown} */
|
|
25
|
+
const value = JSON.parse(await readFile(file, "utf-8"));
|
|
26
|
+
return value;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (missingFile(error)) {
|
|
29
|
+
return missingValue;
|
|
30
|
+
}
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
/** @param {string} root @param {readonly string[]} [paths] @param {string} [scope] */
|
|
35
|
+
export const loadManagedLint = async (root, paths, scope) =>
|
|
36
|
+
await resolveManagedLint({
|
|
37
|
+
root,
|
|
38
|
+
selection: await readJson(path.join(root, ".devpunks/specs/lint/selection.json")),
|
|
39
|
+
settings: await readJson(path.join(root, ".devpunks/settings.json"), {}),
|
|
40
|
+
...(paths === undefined ? {} : { paths }),
|
|
41
|
+
...(scope === undefined ? {} : { scope }),
|
|
42
|
+
});
|
|
43
|
+
/** @param {string} cwd @returns {Promise<string>} */
|
|
44
|
+
const discoverRoot = async (cwd) => {
|
|
45
|
+
try {
|
|
46
|
+
await access(path.join(cwd, ".devpunks/settings.json"));
|
|
47
|
+
return cwd;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (!missingFile(error)) {
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const parent = path.dirname(cwd);
|
|
54
|
+
if (parent === cwd) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
"Select software scopes in .devpunks/settings.json lint.scopes, or pass --root.",
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return await discoverRoot(parent);
|
|
60
|
+
};
|
|
61
|
+
/** @param {string} root @param {string} cwd @returns {NodeJS.ProcessEnv} */
|
|
62
|
+
export const managedLintEnvironment = (root, cwd) => {
|
|
63
|
+
const bins = [];
|
|
64
|
+
let current = cwd;
|
|
65
|
+
while (true) {
|
|
66
|
+
bins.push(path.join(current, "node_modules/.bin"));
|
|
67
|
+
if (current === root) {
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
const parent = path.dirname(current);
|
|
71
|
+
if (parent === current) {
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
current = parent;
|
|
75
|
+
}
|
|
76
|
+
bins.push(path.dirname(process.execPath));
|
|
77
|
+
const node = spawnSync("node", ["--print", "process.execPath"], { encoding: "utf-8" });
|
|
78
|
+
const nodePath = node.stdout?.trim();
|
|
79
|
+
if (node.status === 0 && nodePath !== undefined && path.isAbsolute(nodePath)) {
|
|
80
|
+
bins.push(path.dirname(nodePath));
|
|
81
|
+
}
|
|
82
|
+
return { ...process.env, PATH: [...new Set(bins)].join(path.delimiter) };
|
|
83
|
+
};
|
|
84
|
+
/** @param {import("./managed-lint-runner.mjs").ManagedLintRunOptions} options @returns {Promise<import("./managed-lint-runner.mjs").ManagedLintRunResult>} */
|
|
85
|
+
export const runManagedLint = async (options) => {
|
|
86
|
+
try {
|
|
87
|
+
return await executeManagedLint(options);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
return { code: 2, diagnostics: [errorMessage(error)], status: "conflict" };
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/** @param {import("./managed-lint-runner.mjs").ManagedLintRunOptions} options @returns {Promise<import("./managed-lint-runner.mjs").ManagedLintRunResult>} */
|
|
93
|
+
const executeManagedLint = async ({ root, scope, paths, output = "human" }) => {
|
|
94
|
+
let resolution = await loadManagedLint(root, paths, scope);
|
|
95
|
+
if (resolution.status === "disabled") {
|
|
96
|
+
return { code: 0, results: [], status: "clean" };
|
|
97
|
+
}
|
|
98
|
+
if (resolution.status !== "ready") {
|
|
99
|
+
return { ...resolution, code: 2 };
|
|
100
|
+
}
|
|
101
|
+
if (resolution.routes.some((route) => route.triggered)) {
|
|
102
|
+
const complete = await loadManagedLint(root);
|
|
103
|
+
if (complete.status !== "ready") {
|
|
104
|
+
return { ...complete, code: 2 };
|
|
105
|
+
}
|
|
106
|
+
resolution = {
|
|
107
|
+
...resolution,
|
|
108
|
+
routes: resolution.routes.map((route) =>
|
|
109
|
+
route.triggered
|
|
110
|
+
? (complete.routes.find((item) => item.scope === route.scope) ?? route)
|
|
111
|
+
: route,
|
|
112
|
+
),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const commands = await Promise.all(
|
|
116
|
+
resolution.routes
|
|
117
|
+
.filter((item) => scope === undefined || scope === item.scope)
|
|
118
|
+
.map(async (route) => await buildManagedLintCommand({ output: "json", root, route })),
|
|
119
|
+
);
|
|
120
|
+
const failure = commands.find(
|
|
121
|
+
(command) => command.status !== "ready" && command.status !== "empty",
|
|
122
|
+
);
|
|
123
|
+
if (failure !== undefined) {
|
|
124
|
+
return { ...failure, code: 2, results: [] };
|
|
125
|
+
}
|
|
126
|
+
const results = commands
|
|
127
|
+
.filter((command) => command.status === "ready")
|
|
128
|
+
.map((command) => executeManagedLintCommand({ command, output, root }));
|
|
129
|
+
/** @type {0 | 1 | 2} */
|
|
130
|
+
let code = 0;
|
|
131
|
+
if (results.some((result) => result.code !== 0)) {
|
|
132
|
+
code = 1;
|
|
133
|
+
}
|
|
134
|
+
if (results.some((result) => result.code === 2)) {
|
|
135
|
+
code = 2;
|
|
136
|
+
}
|
|
137
|
+
/** @type {readonly ["clean", "findings", "operational-failure"]} */
|
|
138
|
+
const statuses = ["clean", "findings", "operational-failure"];
|
|
139
|
+
return { code, results, status: statuses[code] };
|
|
140
|
+
};
|
|
141
|
+
/** @param {string} stdout - Structured output from the local Oxlint subprocess.
|
|
142
|
+
* @returns {{ diagnostics: readonly import("./managed-lint-runner.mjs").ManagedLintDiagnostic[], empty: boolean } | null} A valid report, or null for malformed output.
|
|
143
|
+
*/
|
|
144
|
+
const readLintOutput = (stdout) => {
|
|
145
|
+
const notice = [
|
|
146
|
+
"No files found matching the given patterns.",
|
|
147
|
+
"No files found to lint. Please check your paths and ignore patterns.",
|
|
148
|
+
].find((candidate) => stdout.startsWith(`${candidate}\n`));
|
|
149
|
+
try {
|
|
150
|
+
/** @type {unknown} */
|
|
151
|
+
const value = JSON.parse(notice === undefined ? stdout : stdout.slice(notice.length + 1));
|
|
152
|
+
if (
|
|
153
|
+
isRecord(value) &&
|
|
154
|
+
Array.isArray(value.diagnostics) &&
|
|
155
|
+
value.diagnostics.every(isDiagnostic) &&
|
|
156
|
+
(notice === undefined || (value.diagnostics.length === 0 && value.number_of_files === 0))
|
|
157
|
+
) {
|
|
158
|
+
return { diagnostics: value.diagnostics, empty: notice !== undefined };
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
// Malformed/config output is operational, even if the subprocess exits zero.
|
|
162
|
+
}
|
|
163
|
+
return null;
|
|
164
|
+
};
|
|
165
|
+
/** @param {string} stderr - Node's benign warning for a typeless package's ESM config. */
|
|
166
|
+
const hasOnlyModuleTypeWarnings = (stderr) =>
|
|
167
|
+
stderr
|
|
168
|
+
.split(/\r?\n/u)
|
|
169
|
+
.map((line) => line.trim())
|
|
170
|
+
.filter((line) => line.length > 0)
|
|
171
|
+
.every(
|
|
172
|
+
(line) =>
|
|
173
|
+
/^\(node:\d+\) \[MODULE_TYPELESS_PACKAGE_JSON\] Warning: Module type of .+ is not specified and it doesn't parse as CommonJS\.$/u.test(
|
|
174
|
+
line,
|
|
175
|
+
) ||
|
|
176
|
+
line ===
|
|
177
|
+
"Reparsing as ES module because module syntax was detected. This incurs a performance overhead." ||
|
|
178
|
+
/^To eliminate this warning, add "type": "module" to .+\.$/u.test(line) ||
|
|
179
|
+
line === "(Use `node --trace-warnings ...` to show where the warning was created)",
|
|
180
|
+
);
|
|
181
|
+
/** @param {import("node:child_process").SpawnSyncReturns<string>} result - The local Oxlint process result.
|
|
182
|
+
* @param {ReturnType<typeof readLintOutput>} report - Its validated structured output.
|
|
183
|
+
* @returns {0 | 1 | 2} The public clean, findings, or operational-failure exit code.
|
|
184
|
+
*/
|
|
185
|
+
const lintExitCode = (result, report) => {
|
|
186
|
+
if (result.error !== undefined || result.signal !== null || report === null) {
|
|
187
|
+
return 2;
|
|
188
|
+
}
|
|
189
|
+
if (!hasOnlyModuleTypeWarnings(result.stderr ?? "")) {
|
|
190
|
+
return 2;
|
|
191
|
+
}
|
|
192
|
+
if (result.status === 0) {
|
|
193
|
+
return 0;
|
|
194
|
+
}
|
|
195
|
+
if (result.status !== 1) {
|
|
196
|
+
return 2;
|
|
197
|
+
}
|
|
198
|
+
if (report.empty) {
|
|
199
|
+
return 0;
|
|
200
|
+
}
|
|
201
|
+
return report.diagnostics.length > 0 ? 1 : 2;
|
|
202
|
+
};
|
|
203
|
+
/** @param {import("node:child_process").SpawnSyncReturns<string>} result - Original process evidence.
|
|
204
|
+
* @param {ReturnType<typeof readLintOutput>} report - Validated structured output.
|
|
205
|
+
* @returns {NonNullable<import("./managed-lint-runner.mjs").ManagedLintExecution["failure"]>} The operational failure's presentation context.
|
|
206
|
+
*/
|
|
207
|
+
const lintFailure = (result, report) => {
|
|
208
|
+
if (result.error !== undefined || result.signal !== null || result.status === null) {
|
|
209
|
+
return { kind: "process", phase: "lint" };
|
|
210
|
+
}
|
|
211
|
+
if (
|
|
212
|
+
result.status !== 0 &&
|
|
213
|
+
/^Failed to parse oxlint configuration file\./mu.test(result.stdout ?? "")
|
|
214
|
+
) {
|
|
215
|
+
return { kind: "config-load", phase: "config" };
|
|
216
|
+
}
|
|
217
|
+
const stderr = result.stderr ?? "";
|
|
218
|
+
if (!hasOnlyModuleTypeWarnings(stderr) && /config|load/iu.test(stderr)) {
|
|
219
|
+
return { kind: "config-load", phase: "lint" };
|
|
220
|
+
}
|
|
221
|
+
return { kind: report === null ? "malformed-output" : "process", phase: "lint" };
|
|
222
|
+
};
|
|
223
|
+
/** @param {import("./managed-lint-runner.mjs").ManagedLintExecutionOptions} options @returns {import("./managed-lint-runner.mjs").ManagedLintExecution} */
|
|
224
|
+
export const executeManagedLintCommand = ({ root, command, output = "json" }) => {
|
|
225
|
+
const result = spawnSync(command.executable, command.args, {
|
|
226
|
+
cwd: command.cwd,
|
|
227
|
+
encoding: "utf-8",
|
|
228
|
+
env: managedLintEnvironment(root, command.cwd),
|
|
229
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
230
|
+
});
|
|
231
|
+
const report = readLintOutput(result.stdout ?? "");
|
|
232
|
+
const stderr = result.stderr ?? "";
|
|
233
|
+
const diagnostics = report?.diagnostics;
|
|
234
|
+
const code = lintExitCode(result, report);
|
|
235
|
+
const stdout =
|
|
236
|
+
output === "json" || code === 2 || diagnostics === undefined
|
|
237
|
+
? (result.stdout ?? "")
|
|
238
|
+
: diagnostics
|
|
239
|
+
.map(
|
|
240
|
+
(item) =>
|
|
241
|
+
`${item.filename ?? command.scope}: ${item.severity ?? "error"}: ${item.message} (${item.code ?? "lint"})`,
|
|
242
|
+
)
|
|
243
|
+
.join("\n") + (diagnostics.length > 0 ? "\n" : "");
|
|
244
|
+
return {
|
|
245
|
+
code,
|
|
246
|
+
command,
|
|
247
|
+
error: result.error,
|
|
248
|
+
scope: command.scope,
|
|
249
|
+
signal: result.signal,
|
|
250
|
+
status: result.status,
|
|
251
|
+
stderr: stderr === "" ? (result.error?.message ?? "") : stderr,
|
|
252
|
+
stdout,
|
|
253
|
+
...(code === 2 ? { failure: lintFailure(result, report) } : {}),
|
|
254
|
+
...(diagnostics === undefined ? {} : { diagnostics }),
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const main = async () => {
|
|
259
|
+
const args = process.argv.slice(2);
|
|
260
|
+
let root;
|
|
261
|
+
let scope;
|
|
262
|
+
/** @type {"human" | "json"} */
|
|
263
|
+
let output = "human";
|
|
264
|
+
let paths;
|
|
265
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
266
|
+
const arg = args[index];
|
|
267
|
+
if (arg === "--") {
|
|
268
|
+
paths = args.slice(index + 1);
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
if (!["--root", "--scope", "--format"].includes(arg) || args[index + 1] === undefined) {
|
|
272
|
+
throw new Error(`Unknown or incomplete argument: ${arg}`);
|
|
273
|
+
}
|
|
274
|
+
index += 1;
|
|
275
|
+
const value = args[index];
|
|
276
|
+
if (value.startsWith("--")) {
|
|
277
|
+
throw new Error(`Missing value for ${arg}`);
|
|
278
|
+
}
|
|
279
|
+
if (arg === "--root") {
|
|
280
|
+
root = path.resolve(value);
|
|
281
|
+
}
|
|
282
|
+
if (arg === "--scope") {
|
|
283
|
+
scope = value;
|
|
284
|
+
}
|
|
285
|
+
if (arg === "--format") {
|
|
286
|
+
if (value !== "human" && value !== "json") {
|
|
287
|
+
throw new Error("--format must be human or json");
|
|
288
|
+
}
|
|
289
|
+
output = value;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (!["human", "json"].includes(output)) {
|
|
293
|
+
throw new Error("--format must be human or json");
|
|
294
|
+
}
|
|
295
|
+
const resolvedRoot = root ?? (await discoverRoot(process.cwd()));
|
|
296
|
+
const result = await runManagedLint({
|
|
297
|
+
output,
|
|
298
|
+
paths: paths?.map((file) =>
|
|
299
|
+
path.relative(resolvedRoot, path.resolve(file)).split(path.sep).join("/"),
|
|
300
|
+
),
|
|
301
|
+
root: resolvedRoot,
|
|
302
|
+
scope,
|
|
303
|
+
});
|
|
304
|
+
if (output === "json") {
|
|
305
|
+
process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
306
|
+
} else {
|
|
307
|
+
for (const owner of result.results ?? []) {
|
|
308
|
+
process.stdout.write(owner.stdout);
|
|
309
|
+
process.stderr.write(owner.stderr);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
for (const diagnostic of result.diagnostics ?? []) {
|
|
313
|
+
process.stderr.write(`${diagnostic}\n`);
|
|
314
|
+
}
|
|
315
|
+
process.exitCode = result.code;
|
|
316
|
+
};
|
|
317
|
+
// Bundling rewrites import.meta.filename to the host CLI; retain the standalone asset identity.
|
|
318
|
+
if (
|
|
319
|
+
process.argv[1] !== undefined &&
|
|
320
|
+
path.basename(process.argv[1]) === "managed-lint-runner.mjs" &&
|
|
321
|
+
path.resolve(process.argv[1]) === import.meta.filename
|
|
322
|
+
) {
|
|
323
|
+
try {
|
|
324
|
+
await main();
|
|
325
|
+
} catch (error) {
|
|
326
|
+
console.error(errorMessage(error));
|
|
327
|
+
process.exitCode = 2;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** Derived member of the existing .devpunks/specs/lint/selection.json artifact. */
|
|
2
|
+
export interface ManagedLintSelection {
|
|
3
|
+
readonly version: 1;
|
|
4
|
+
readonly scopes: readonly string[];
|
|
5
|
+
readonly exclude: readonly string[];
|
|
6
|
+
readonly routes: readonly ManagedLintRoute[];
|
|
7
|
+
}
|
|
8
|
+
export interface ManagedQualityPathOptions {
|
|
9
|
+
/** Normalized repository-relative POSIX path. */
|
|
10
|
+
readonly path: string;
|
|
11
|
+
/** Repository-relative exclusions already validated at the settings boundary. */
|
|
12
|
+
readonly exclude: readonly string[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Pure shared hard and persisted exclusions, independent of package selection.
|
|
16
|
+
* @throws {TypeError} If path fails the managed lint repository-relative path boundary.
|
|
17
|
+
*/
|
|
18
|
+
export function isManagedQualityPathExcluded(options: ManagedQualityPathOptions): boolean;
|
|
19
|
+
|
|
20
|
+
export interface ManagedLintPolicyInput {
|
|
21
|
+
readonly path: string;
|
|
22
|
+
readonly sha256: string;
|
|
23
|
+
}
|
|
24
|
+
export interface ManagedLintRoute {
|
|
25
|
+
/** Exact repository-relative package root, or explicit standalone ".". */
|
|
26
|
+
readonly scope: string;
|
|
27
|
+
/** Repository-relative effective oxlint.config.ts. */
|
|
28
|
+
readonly config: string;
|
|
29
|
+
readonly tool: { readonly package: "oxlint"; readonly version: string };
|
|
30
|
+
/** null preserves an explicitly represented unrestricted warning threshold. */
|
|
31
|
+
readonly maxWarnings: number | null;
|
|
32
|
+
/** Repository-relative transitive policy, preset, plugin and toolchain inputs. */
|
|
33
|
+
readonly inputs: readonly string[];
|
|
34
|
+
/** Hashes of authored inputs whose semantics were compiled into the effective policy. */
|
|
35
|
+
readonly policyInputs?: readonly ManagedLintPolicyInput[];
|
|
36
|
+
}
|
|
37
|
+
export interface ResolvedManagedLintRoute extends ManagedLintRoute {
|
|
38
|
+
readonly files: readonly string[];
|
|
39
|
+
readonly boundaries: readonly string[];
|
|
40
|
+
/** A shared dependency changed: adapter should validate the complete owner. */
|
|
41
|
+
readonly triggered: boolean;
|
|
42
|
+
}
|
|
43
|
+
export interface ManagedLintFailure {
|
|
44
|
+
readonly status: "selection-needed" | "conflict";
|
|
45
|
+
readonly diagnostics: readonly string[];
|
|
46
|
+
}
|
|
47
|
+
export type ManagedLintResolution =
|
|
48
|
+
| ManagedLintFailure
|
|
49
|
+
| {
|
|
50
|
+
readonly status: "disabled";
|
|
51
|
+
readonly diagnostics: readonly string[];
|
|
52
|
+
}
|
|
53
|
+
| {
|
|
54
|
+
readonly status: "ready";
|
|
55
|
+
readonly routes: readonly ResolvedManagedLintRoute[];
|
|
56
|
+
readonly excludedPaths: readonly string[];
|
|
57
|
+
};
|
|
58
|
+
/** No execution or publication. Omitted paths enumerate existing eligible files; [] selects none. */
|
|
59
|
+
export interface ResolveManagedLintOptions {
|
|
60
|
+
/** When provided, validate this selected owner even for excluded-only requests. */
|
|
61
|
+
readonly scope?: string;
|
|
62
|
+
readonly root: string;
|
|
63
|
+
readonly settings: unknown;
|
|
64
|
+
readonly selection: unknown;
|
|
65
|
+
readonly paths?: readonly string[];
|
|
66
|
+
}
|
|
67
|
+
export function resolveManagedLint(
|
|
68
|
+
options: ResolveManagedLintOptions,
|
|
69
|
+
): Promise<ManagedLintResolution>;
|
|
70
|
+
export type ManagedLintCommand =
|
|
71
|
+
| ManagedLintFailure
|
|
72
|
+
| {
|
|
73
|
+
readonly status: "empty";
|
|
74
|
+
readonly diagnostics: readonly string[];
|
|
75
|
+
}
|
|
76
|
+
| {
|
|
77
|
+
readonly status: "ready";
|
|
78
|
+
readonly executable: string;
|
|
79
|
+
readonly args: readonly string[];
|
|
80
|
+
readonly cwd: string;
|
|
81
|
+
readonly config: string;
|
|
82
|
+
readonly toolVersion: string;
|
|
83
|
+
readonly scope: string;
|
|
84
|
+
readonly logicalSourcePath: string | undefined;
|
|
85
|
+
readonly physicalInputPath: string | undefined;
|
|
86
|
+
};
|
|
87
|
+
/** A different physical path is rejected until an adapter provides a controlled mirror root. */
|
|
88
|
+
export interface BuildManagedLintCommandOptions {
|
|
89
|
+
readonly root: string;
|
|
90
|
+
readonly route: ResolvedManagedLintRoute;
|
|
91
|
+
readonly output?: "human" | "json";
|
|
92
|
+
readonly logicalSourcePath?: string;
|
|
93
|
+
readonly physicalInputPath?: string;
|
|
94
|
+
}
|
|
95
|
+
export function buildManagedLintCommand(
|
|
96
|
+
options: BuildManagedLintCommandOptions,
|
|
97
|
+
): Promise<ManagedLintCommand>;
|