@ryuugg/stanza 0.2.0 → 0.3.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/package.json +1 -1
- package/src/cli.ts +14 -68
- package/src/compiled.ts +2 -2
- package/src/hook.ts +61 -2
- package/src/launcher +2 -2
- package/src/start.ts +49 -0
- package/src/usage.ts +2 -0
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -32,12 +32,15 @@ import {
|
|
|
32
32
|
} from "./files.ts";
|
|
33
33
|
import { explain } from "./engine/explain.ts";
|
|
34
34
|
import { languageOf } from "./languages/index.ts";
|
|
35
|
-
import { blockReason, claudeCodeHooks,
|
|
35
|
+
import { blockReason, claudeCodeHooks, hookCall, rereadLine, type HookCall } from "./hook.ts";
|
|
36
|
+
import { systemIo, type Io } from "./start.ts";
|
|
36
37
|
import type { Changed } from "./engine/model.ts";
|
|
37
38
|
import { RULES } from "./engine/rules.ts";
|
|
38
39
|
import { decode, type Decoded, formatText, withoutMark } from "./step.ts";
|
|
39
40
|
import { compareFindings, type Braces, type Finding, type Mode } from "./engine/types.ts";
|
|
40
|
-
import { columns, flags, usage } from "./usage.ts";
|
|
41
|
+
import { bothBraces, columns, flags, usage } from "./usage.ts";
|
|
42
|
+
|
|
43
|
+
export type { Io } from "./start.ts";
|
|
41
44
|
|
|
42
45
|
declare const STANZA_COMMIT: string | undefined;
|
|
43
46
|
|
|
@@ -58,16 +61,6 @@ interface ExplainArguments {
|
|
|
58
61
|
noBraces: boolean;
|
|
59
62
|
}
|
|
60
63
|
|
|
61
|
-
type Writer = (chunk: string | Uint8Array) => void;
|
|
62
|
-
|
|
63
|
-
export interface Io {
|
|
64
|
-
cwd: string;
|
|
65
|
-
env: Readonly<Record<string, string | undefined>>;
|
|
66
|
-
stdin: () => Uint8Array;
|
|
67
|
-
stdout: Writer;
|
|
68
|
-
stderr: Writer;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
64
|
interface Input {
|
|
72
65
|
path: string;
|
|
73
66
|
read: () => Decoded;
|
|
@@ -91,32 +84,8 @@ interface Formatted {
|
|
|
91
84
|
unreadWidth: Set<string>;
|
|
92
85
|
}
|
|
93
86
|
|
|
94
|
-
function ignoreBrokenPipe(error: NodeJS.ErrnoException): void {
|
|
95
|
-
if (error.code !== "EPIPE") throw error;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function systemIo(): Io {
|
|
99
|
-
// Under Bun a stream write throws EPIPE into a stack trace and exit 1, where console.log swallowed it.
|
|
100
|
-
process.stdout.on("error", ignoreBrokenPipe);
|
|
101
|
-
process.stderr.on("error", ignoreBrokenPipe);
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
cwd: process.cwd(),
|
|
105
|
-
env: process.env,
|
|
106
|
-
stdin: () => readFileSync(0),
|
|
107
|
-
stdout: (chunk) => {
|
|
108
|
-
process.stdout.write(chunk);
|
|
109
|
-
},
|
|
110
|
-
stderr: (chunk) => {
|
|
111
|
-
process.stderr.write(chunk);
|
|
112
|
-
},
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
87
|
const switches = new Set(["--braces", "--changed", "--hunks", "--json", "--no-braces", "--staged"]);
|
|
117
88
|
const standalone = new Set(["--help", "-h", "--version"]);
|
|
118
|
-
const hookSwitches = new Set(["--braces", "--hunks", "--no-braces"]);
|
|
119
|
-
const bothBraces = "use one of --braces or --no-braces";
|
|
120
89
|
|
|
121
90
|
function help(): string {
|
|
122
91
|
const rules = Object.entries(RULES).map(([id, rule]) => [
|
|
@@ -725,16 +694,11 @@ function fixChanged(
|
|
|
725
694
|
}
|
|
726
695
|
|
|
727
696
|
function runStopHook(
|
|
728
|
-
input: { cwd: string;
|
|
697
|
+
input: { cwd: string; written: Set<string> | undefined },
|
|
729
698
|
args: string[],
|
|
730
699
|
io: Io,
|
|
731
700
|
): number {
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
const written =
|
|
735
|
-
input.transcriptPath === undefined ? undefined : writtenFiles(input.transcriptPath);
|
|
736
|
-
|
|
737
|
-
const result = fixChanged(input.cwd, written, args, io);
|
|
701
|
+
const result = fixChanged(input.cwd, input.written, args, io);
|
|
738
702
|
if (typeof result === "number") return result;
|
|
739
703
|
|
|
740
704
|
const reason = blockReason(result.findings, result.rewritten);
|
|
@@ -761,32 +725,14 @@ function runEditHook(input: { cwd: string; paths: string[] }, args: string[], io
|
|
|
761
725
|
}
|
|
762
726
|
|
|
763
727
|
function runHook(args: string[], io: Io): number {
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
(arg, index) => !hookSwitches.has(arg) || args.indexOf(arg) !== index,
|
|
768
|
-
);
|
|
769
|
-
|
|
770
|
-
if (unexpected !== undefined) {
|
|
771
|
-
warn(io, `stanza hook: unexpected argument ${unexpected}\n${usage}`);
|
|
772
|
-
return 1;
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
if (args.includes("--braces") && args.includes("--no-braces")) {
|
|
776
|
-
warn(io, `stanza hook: ${bothBraces}\n${usage}`);
|
|
777
|
-
return 1;
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
const input = hookInput(Buffer.from(io.stdin()).toString("utf8"), io.cwd);
|
|
781
|
-
if ("error" in input) {
|
|
782
|
-
warn(io, `stanza hook: ${input.error}`);
|
|
783
|
-
return 1;
|
|
784
|
-
}
|
|
728
|
+
const call = hookCall(args, io);
|
|
729
|
+
return typeof call === "number" ? call : runHookCall(call, args, io);
|
|
730
|
+
}
|
|
785
731
|
|
|
786
|
-
|
|
787
|
-
if (
|
|
788
|
-
if (
|
|
789
|
-
return runStopHook(
|
|
732
|
+
export function runHookCall(call: HookCall, args: string[], io: Io): number {
|
|
733
|
+
if (call.event === "write") return runWriteHook(call.cwd, call.toolInput, args, io);
|
|
734
|
+
if (call.event === "edit") return runEditHook(call, args, io);
|
|
735
|
+
return runStopHook(call, args, io);
|
|
790
736
|
}
|
|
791
737
|
|
|
792
738
|
export function main(argv: string[], io: Io): number {
|
package/src/compiled.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export async function start(addon: string): Promise<void> {
|
|
2
2
|
// Must precede any oxc-parser import (its loader cannot find the binding in a compiled binary), hence the dynamic import.
|
|
3
3
|
process.env.NAPI_RS_NATIVE_LIBRARY_PATH = addon;
|
|
4
|
-
const {
|
|
5
|
-
process.exitCode =
|
|
4
|
+
const { start } = await import("./start.ts");
|
|
5
|
+
process.exitCode = await start(process.argv.slice(2));
|
|
6
6
|
}
|
package/src/hook.ts
CHANGED
|
@@ -1,7 +1,64 @@
|
|
|
1
|
-
import { readFileSync, readdirSync } from "node:fs";
|
|
1
|
+
import { readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
2
2
|
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
-
import { RULES } from "./engine/rules.ts";
|
|
4
3
|
import type { Finding } from "./engine/types.ts";
|
|
4
|
+
import { isCandidate } from "./languages/index.ts";
|
|
5
|
+
import type { Io } from "./start.ts";
|
|
6
|
+
import { bothBraces, usage } from "./usage.ts";
|
|
7
|
+
|
|
8
|
+
export type HookCall =
|
|
9
|
+
| {
|
|
10
|
+
event: "write";
|
|
11
|
+
cwd: string;
|
|
12
|
+
toolInput: { file_path: string; content: string } & Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
| { event: "edit"; cwd: string; paths: string[] }
|
|
15
|
+
| { event: "stop"; cwd: string; written: Set<string> | undefined };
|
|
16
|
+
|
|
17
|
+
const hookSwitches = new Set(["--braces", "--hunks", "--no-braces"]);
|
|
18
|
+
|
|
19
|
+
function isCandidateFile(path: string): boolean {
|
|
20
|
+
try {
|
|
21
|
+
return isCandidate(basename(realpathSync(path)));
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function hookCall(args: string[], io: Io): HookCall | number {
|
|
28
|
+
if (io.env.AGENT_HOOKS === "0") return 0;
|
|
29
|
+
|
|
30
|
+
const unexpected = args.find(
|
|
31
|
+
(arg, index) => !hookSwitches.has(arg) || args.indexOf(arg) !== index,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
if (unexpected !== undefined) {
|
|
35
|
+
io.stderr(`stanza hook: unexpected argument ${unexpected}\n${usage}\n`);
|
|
36
|
+
return 1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (args.includes("--braces") && args.includes("--no-braces")) {
|
|
40
|
+
io.stderr(`stanza hook: ${bothBraces}\n${usage}\n`);
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const input = hookInput(Buffer.from(io.stdin()).toString("utf8"), io.cwd);
|
|
45
|
+
if ("error" in input) {
|
|
46
|
+
io.stderr(`stanza hook: ${input.error}\n`);
|
|
47
|
+
return 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (input.event === "ignored") return 0;
|
|
51
|
+
if (input.event === "write") return isCandidate(basename(input.toolInput.file_path)) ? input : 0;
|
|
52
|
+
if (input.event === "edit") return input.paths.some(isCandidateFile) ? input : 0;
|
|
53
|
+
if (input.stopHookActive) return 0;
|
|
54
|
+
|
|
55
|
+
const written =
|
|
56
|
+
input.transcriptPath === undefined ? undefined : writtenFiles(input.transcriptPath);
|
|
57
|
+
|
|
58
|
+
if (written !== undefined && ![...written].some(isCandidateFile)) return 0;
|
|
59
|
+
|
|
60
|
+
return { event: "stop", cwd: input.cwd, written };
|
|
61
|
+
}
|
|
5
62
|
|
|
6
63
|
export const claudeCodeHooks =
|
|
7
64
|
'{ "hooks": { "PreToolUse": [{ "matcher": "Write", "hooks": [{ "type": "command", "command": "stanza hook" }] }], "PostToolUse": [{ "matcher": "Edit|MultiEdit", "hooks": [{ "type": "command", "command": "stanza hook" }] }], "Stop": [{ "hooks": [{ "type": "command", "command": "stanza hook" }] }] } }';
|
|
@@ -242,6 +299,8 @@ const failures: Partial<Record<Finding["rule"], string>> = {
|
|
|
242
299
|
export function blockReason(findings: Finding[], rewritten: string[]): string | undefined {
|
|
243
300
|
if (findings.length === 0) return undefined;
|
|
244
301
|
|
|
302
|
+
const { RULES } = require("./engine/rules.ts") as typeof import("./engine/rules.ts");
|
|
303
|
+
|
|
245
304
|
const shown = findings.slice(0, 12);
|
|
246
305
|
const lines = shown.map((finding) => {
|
|
247
306
|
const failure = failures[finding.rule];
|
package/src/launcher
CHANGED
|
@@ -17,5 +17,5 @@ if (!Bun.semver.satisfies(Bun.version, ">=1.4.0")) {
|
|
|
17
17
|
process.exit(process.argv[2] === "hook" ? 1 : 2);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const {
|
|
21
|
-
process.exitCode =
|
|
20
|
+
const { start } = await import("./start.ts");
|
|
21
|
+
process.exitCode = await start(process.argv.slice(2));
|
package/src/start.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { hookCall } from "./hook.ts";
|
|
3
|
+
|
|
4
|
+
type Writer = (chunk: string | Uint8Array) => void;
|
|
5
|
+
|
|
6
|
+
export interface Io {
|
|
7
|
+
cwd: string;
|
|
8
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
9
|
+
stdin: () => Uint8Array;
|
|
10
|
+
stdout: Writer;
|
|
11
|
+
stderr: Writer;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function ignoreBrokenPipe(error: NodeJS.ErrnoException): void {
|
|
15
|
+
if (error.code !== "EPIPE") throw error;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function systemIo(): Io {
|
|
19
|
+
// Under Bun a stream write throws EPIPE into a stack trace and exit 1, where console.log swallowed it.
|
|
20
|
+
process.stdout.on("error", ignoreBrokenPipe);
|
|
21
|
+
process.stderr.on("error", ignoreBrokenPipe);
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
cwd: process.cwd(),
|
|
25
|
+
env: process.env,
|
|
26
|
+
stdin: () => readFileSync(0),
|
|
27
|
+
stdout: (chunk) => {
|
|
28
|
+
process.stdout.write(chunk);
|
|
29
|
+
},
|
|
30
|
+
stderr: (chunk) => {
|
|
31
|
+
process.stderr.write(chunk);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function start(argv: string[]): Promise<number> {
|
|
37
|
+
const io = systemIo();
|
|
38
|
+
if (argv[0] === "hook") {
|
|
39
|
+
const args = argv.slice(1);
|
|
40
|
+
const call = hookCall(args, io);
|
|
41
|
+
if (typeof call === "number") return call;
|
|
42
|
+
|
|
43
|
+
const { runHookCall } = await import("./cli.ts");
|
|
44
|
+
return runHookCall(call, args, io);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { main } = await import("./cli.ts");
|
|
48
|
+
return main(argv, io);
|
|
49
|
+
}
|
package/src/usage.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export const bothBraces = "use one of --braces or --no-braces";
|
|
2
|
+
|
|
1
3
|
export const usage =
|
|
2
4
|
"Usage: stanza (--fix | --check) [--changed [--hunks] | --stdin <path> | [--] <paths...>] [--json] [--braces | --no-braces]\n stanza --check --staged [--hunks] [--json] [--braces | --no-braces]\n stanza explain <file>:<line> [--no-braces]\n stanza hook [--braces | --no-braces] [--hunks]";
|
|
3
5
|
|