@kaynooo/cli-guard 0.11.0 → 0.11.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/dist/cli.js +148 -73
- package/package.json +8 -8
package/dist/cli.js
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/cli.ts
|
|
5
|
-
import { appendFileSync as appendFileSync2, existsSync as
|
|
6
|
-
import { homedir as
|
|
5
|
+
import { appendFileSync as appendFileSync2, existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync5, rmSync, statSync, writeFileSync } from "fs";
|
|
6
|
+
import { homedir as homedir3 } from "os";
|
|
7
7
|
import { dirname as dirname4 } from "path";
|
|
8
|
-
import
|
|
8
|
+
import process14 from "process";
|
|
9
9
|
// ../kit/src/args.ts
|
|
10
|
-
import
|
|
10
|
+
import process7 from "process";
|
|
11
11
|
import { parseArgs } from "util";
|
|
12
12
|
|
|
13
13
|
// ../kit/src/mode.ts
|
|
@@ -29,7 +29,7 @@ function outputMode(stream = process2.stdout, env = process2.env) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
// ../kit/src/style.ts
|
|
32
|
-
import
|
|
32
|
+
import process4 from "process";
|
|
33
33
|
|
|
34
34
|
// ../../node_modules/.bun/@kaynooo+utils@3.0.0+4c7ddab9986847c5/node_modules/@kaynooo/utils/dist/chunk-x450vn3m.js
|
|
35
35
|
var G = /^[a-f0-9]+$/i;
|
|
@@ -86,6 +86,41 @@ function Fe(r = {}) {
|
|
|
86
86
|
}
|
|
87
87
|
var o = Fe();
|
|
88
88
|
|
|
89
|
+
// ../kit/src/user-config.ts
|
|
90
|
+
import { existsSync, readFileSync } from "fs";
|
|
91
|
+
import { homedir } from "os";
|
|
92
|
+
import { join } from "path";
|
|
93
|
+
import process3 from "process";
|
|
94
|
+
function configBase(where = {}) {
|
|
95
|
+
const xdg = where.xdg ?? (where.home === undefined ? process3.env.XDG_CONFIG_HOME : undefined);
|
|
96
|
+
return xdg !== undefined && xdg !== "" ? xdg : join(where.home ?? homedir(), ".config");
|
|
97
|
+
}
|
|
98
|
+
function userConfigPath(bin, where = {}) {
|
|
99
|
+
return join(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
100
|
+
}
|
|
101
|
+
function userConfigReadPath(bin, legacy = [], where = {}) {
|
|
102
|
+
const current = userConfigPath(bin, where);
|
|
103
|
+
if (existsSync(current))
|
|
104
|
+
return current;
|
|
105
|
+
const base = configBase(where);
|
|
106
|
+
return legacy.map((parts) => join(base, ...parts)).find((path) => existsSync(path)) ?? current;
|
|
107
|
+
}
|
|
108
|
+
function readUserConfig(bin, where = {}, legacy = []) {
|
|
109
|
+
const path = userConfigReadPath(bin, legacy, where);
|
|
110
|
+
if (!existsSync(path))
|
|
111
|
+
return {};
|
|
112
|
+
try {
|
|
113
|
+
const parsed = Bun.JSONC.parse(readFileSync(path, "utf8"));
|
|
114
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : {};
|
|
115
|
+
} catch {
|
|
116
|
+
return {};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function configuredNumber(bin, key, fallback, where = {}, legacy = []) {
|
|
120
|
+
const value = readUserConfig(bin, where, legacy)[key];
|
|
121
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : fallback;
|
|
122
|
+
}
|
|
123
|
+
|
|
89
124
|
// ../kit/src/theme.ts
|
|
90
125
|
var ENDS = new Map;
|
|
91
126
|
var STYLES = new Set(["bold", "dim", "italic", "underline", "inverse", "strikethrough", "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "gray", "grey", "blackBright", "redBright", "greenBright", "yellowBright", "blueBright", "magentaBright", "cyanBright", "whiteBright"]);
|
|
@@ -93,10 +128,10 @@ var MAX_FILE_BYTES = 64 * 1024;
|
|
|
93
128
|
var warned = new Set;
|
|
94
129
|
|
|
95
130
|
// ../kit/src/style.ts
|
|
96
|
-
function takesControlCodes(stream, env =
|
|
131
|
+
function takesControlCodes(stream, env = process4.env) {
|
|
97
132
|
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
|
|
98
133
|
}
|
|
99
|
-
function paint(stream, env =
|
|
134
|
+
function paint(stream, env = process4.env) {
|
|
100
135
|
return Fe({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
101
136
|
}
|
|
102
137
|
var STATUS_WORDS = {
|
|
@@ -116,27 +151,27 @@ function upTo(text, separator) {
|
|
|
116
151
|
var ESCAPE = String.fromCharCode(27);
|
|
117
152
|
|
|
118
153
|
// ../kit/src/help.ts
|
|
119
|
-
import
|
|
154
|
+
import process5 from "process";
|
|
120
155
|
var OPTIONAL = /\[[^\]]+\]/g;
|
|
121
156
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
122
157
|
var FULL_STOP = /\.$/;
|
|
123
|
-
function usageLine(usage, colors = paint(
|
|
158
|
+
function usageLine(usage, colors = paint(process5.stdout)) {
|
|
124
159
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
125
160
|
}
|
|
126
|
-
function helpText(usage, options, colors = paint(
|
|
161
|
+
function helpText(usage, options, colors = paint(process5.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
127
162
|
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
128
163
|
`);
|
|
129
164
|
}
|
|
130
|
-
function exitRows(exits, colors = paint(
|
|
165
|
+
function exitRows(exits, colors = paint(process5.stdout)) {
|
|
131
166
|
if (exits.length === 0)
|
|
132
167
|
return [];
|
|
133
168
|
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
134
169
|
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
135
170
|
}
|
|
136
|
-
function replacesRows(replaces, colors = paint(
|
|
171
|
+
function replacesRows(replaces, colors = paint(process5.stdout)) {
|
|
137
172
|
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
138
173
|
}
|
|
139
|
-
function wantsHelpJson(env =
|
|
174
|
+
function wantsHelpJson(env = process5.env) {
|
|
140
175
|
return env.KIT_HELP === "json";
|
|
141
176
|
}
|
|
142
177
|
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
@@ -155,12 +190,12 @@ function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
|
155
190
|
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
156
191
|
};
|
|
157
192
|
}
|
|
158
|
-
function exampleRows(examples, colors = paint(
|
|
193
|
+
function exampleRows(examples, colors = paint(process5.stdout)) {
|
|
159
194
|
if (examples.length === 0 || outputMode() === "agent")
|
|
160
195
|
return [];
|
|
161
196
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
162
197
|
}
|
|
163
|
-
function optionRows(options, colors = paint(
|
|
198
|
+
function optionRows(options, colors = paint(process5.stdout)) {
|
|
164
199
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
165
200
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
166
201
|
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
@@ -192,10 +227,10 @@ function closest(name, names) {
|
|
|
192
227
|
return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
|
|
193
228
|
}
|
|
194
229
|
// ../kit/src/input.ts
|
|
195
|
-
import
|
|
230
|
+
import process6 from "process";
|
|
196
231
|
async function readInput(path, { noStdin = "nothing is piped on stdin" } = {}) {
|
|
197
232
|
if (path === "-") {
|
|
198
|
-
if (
|
|
233
|
+
if (process6.stdin.isTTY)
|
|
199
234
|
fail(noStdin);
|
|
200
235
|
return Bun.stdin.text();
|
|
201
236
|
}
|
|
@@ -253,7 +288,7 @@ var OUTPUT_OPTIONS = {
|
|
|
253
288
|
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
254
289
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
255
290
|
};
|
|
256
|
-
function applyOutput(color, plain, env =
|
|
291
|
+
function applyOutput(color, plain, env = process7.env) {
|
|
257
292
|
const when = plain === true ? "never" : color ?? "auto";
|
|
258
293
|
if (when === "auto")
|
|
259
294
|
return;
|
|
@@ -422,9 +457,33 @@ function nearestOption(message, options, allowNegative) {
|
|
|
422
457
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
423
458
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
424
459
|
}
|
|
460
|
+
// ../kit/src/argv.ts
|
|
461
|
+
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
462
|
+
import { resolve } from "path";
|
|
463
|
+
import process8 from "process";
|
|
464
|
+
function commandLine() {
|
|
465
|
+
try {
|
|
466
|
+
return readFileSync2("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
467
|
+
} catch {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
function realPath(word) {
|
|
472
|
+
try {
|
|
473
|
+
return realpathSync(resolve(word));
|
|
474
|
+
} catch {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
function scriptArgs(argv = process8.argv.slice(2), cmdline = commandLine(), script = process8.argv[1]) {
|
|
479
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
480
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve(word) === script || realPath(word) === target));
|
|
481
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
482
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
483
|
+
}
|
|
425
484
|
// ../kit/src/process.ts
|
|
426
485
|
import { constants } from "os";
|
|
427
|
-
import
|
|
486
|
+
import process9 from "process";
|
|
428
487
|
var KILL_GRACE_MS = 2000;
|
|
429
488
|
function exitCodeOf(code, signal) {
|
|
430
489
|
if (code !== null)
|
|
@@ -444,7 +503,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
444
503
|
return;
|
|
445
504
|
try {
|
|
446
505
|
if (grouped)
|
|
447
|
-
|
|
506
|
+
process9.kill(-proc.pid, killing);
|
|
448
507
|
else
|
|
449
508
|
proc.kill(killing);
|
|
450
509
|
} catch {}
|
|
@@ -518,15 +577,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
518
577
|
var RESET_STYLE = `${ESC}[0m`;
|
|
519
578
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
520
579
|
// ../kit/src/cli.ts
|
|
521
|
-
import { readFileSync, realpathSync } from "fs";
|
|
522
|
-
import { dirname as dirname2, join as
|
|
523
|
-
import
|
|
580
|
+
import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
|
|
581
|
+
import { dirname as dirname2, join as join3 } from "path";
|
|
582
|
+
import process11 from "process";
|
|
524
583
|
|
|
525
584
|
// ../kit/src/usage.ts
|
|
526
585
|
import { appendFileSync, mkdirSync } from "fs";
|
|
527
|
-
import { homedir } from "os";
|
|
528
|
-
import { dirname, isAbsolute, join } from "path";
|
|
529
|
-
import
|
|
586
|
+
import { homedir as homedir2 } from "os";
|
|
587
|
+
import { dirname, isAbsolute, join as join2 } from "path";
|
|
588
|
+
import process10 from "process";
|
|
530
589
|
|
|
531
590
|
// ../kit/src/logs.ts
|
|
532
591
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -540,14 +599,14 @@ var usageError;
|
|
|
540
599
|
function noteUsageError(message) {
|
|
541
600
|
usageError = message;
|
|
542
601
|
}
|
|
543
|
-
function stateHome(env =
|
|
602
|
+
function stateHome(env = process10.env) {
|
|
544
603
|
const dir = env.XDG_STATE_HOME;
|
|
545
|
-
return dir !== undefined && isAbsolute(dir) ? dir :
|
|
604
|
+
return dir !== undefined && isAbsolute(dir) ? dir : join2(homedir2(), ".local", "state");
|
|
546
605
|
}
|
|
547
|
-
function usagePath(env =
|
|
548
|
-
return
|
|
606
|
+
function usagePath(env = process10.env) {
|
|
607
|
+
return join2(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
549
608
|
}
|
|
550
|
-
function usageRecorded(env =
|
|
609
|
+
function usageRecorded(env = process10.env) {
|
|
551
610
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
552
611
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
553
612
|
}
|
|
@@ -561,7 +620,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
561
620
|
usageError = undefined;
|
|
562
621
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
563
622
|
}
|
|
564
|
-
function recordUsage(entry, env =
|
|
623
|
+
function recordUsage(entry, env = process10.env) {
|
|
565
624
|
if (!usageRecorded(env))
|
|
566
625
|
return;
|
|
567
626
|
try {
|
|
@@ -652,8 +711,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
652
711
|
`);
|
|
653
712
|
}
|
|
654
713
|
function printError(prefix, message, hint, cause) {
|
|
655
|
-
const colors = paint(
|
|
656
|
-
const styled = takesControlCodes(
|
|
714
|
+
const colors = paint(process11.stderr);
|
|
715
|
+
const styled = takesControlCodes(process11.stderr);
|
|
657
716
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
658
717
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
659
718
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -696,11 +755,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
696
755
|
}
|
|
697
756
|
}
|
|
698
757
|
if (name === undefined && level.path.length > 1) {
|
|
699
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
758
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process11.stderr));
|
|
700
759
|
return 2;
|
|
701
760
|
}
|
|
702
761
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
703
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
762
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process11.stdout));
|
|
704
763
|
return 0;
|
|
705
764
|
}
|
|
706
765
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -756,24 +815,24 @@ function candidateLine(candidate) {
|
|
|
756
815
|
}
|
|
757
816
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
758
817
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
759
|
-
|
|
818
|
+
const typed = [];
|
|
760
819
|
let at = 0;
|
|
761
820
|
while (at < before.length) {
|
|
762
821
|
const word = before[at];
|
|
763
822
|
if (word === "--") {
|
|
764
|
-
|
|
823
|
+
typed.push(...before.slice(at + 1));
|
|
765
824
|
break;
|
|
766
825
|
}
|
|
767
826
|
if (!word.startsWith("-"))
|
|
768
|
-
|
|
827
|
+
typed.push(word);
|
|
769
828
|
else if (takesValue(word))
|
|
770
829
|
at++;
|
|
771
830
|
at++;
|
|
772
831
|
}
|
|
773
832
|
const last = positionals.at(-1);
|
|
774
|
-
if (
|
|
775
|
-
return positionals[
|
|
776
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
833
|
+
if (typed.length < positionals.length)
|
|
834
|
+
return { name: positionals[typed.length], typed };
|
|
835
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
777
836
|
}
|
|
778
837
|
async function leafCandidates(run, words) {
|
|
779
838
|
const completing = await completingOf(run);
|
|
@@ -781,12 +840,12 @@ async function leafCandidates(run, words) {
|
|
|
781
840
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
782
841
|
const flag = words.at(-2);
|
|
783
842
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
843
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
784
844
|
if (option?.type === "string") {
|
|
785
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
845
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
786
846
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
787
847
|
}
|
|
788
|
-
const
|
|
789
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
848
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
790
849
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
791
850
|
}
|
|
792
851
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
@@ -817,9 +876,9 @@ async function printCandidates(found) {
|
|
|
817
876
|
return 0;
|
|
818
877
|
}
|
|
819
878
|
function packageVersion(entry = Bun.main) {
|
|
820
|
-
for (let dir = dirname2(
|
|
879
|
+
for (let dir = dirname2(realpathSync2(entry));; dir = dirname2(dir)) {
|
|
821
880
|
try {
|
|
822
|
-
const { version } = JSON.parse(
|
|
881
|
+
const { version } = JSON.parse(readFileSync3(join3(dir, "package.json"), "utf8"));
|
|
823
882
|
if (typeof version === "string")
|
|
824
883
|
return version;
|
|
825
884
|
} catch {}
|
|
@@ -827,7 +886,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
827
886
|
return "0.0.0";
|
|
828
887
|
}
|
|
829
888
|
}
|
|
830
|
-
async function runCli(cli, argv =
|
|
889
|
+
async function runCli(cli, argv = scriptArgs()) {
|
|
831
890
|
const version = cli.version ?? packageVersion();
|
|
832
891
|
if (argv[0] === COMPLETE)
|
|
833
892
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
@@ -856,6 +915,11 @@ function commandPath(scope, argv) {
|
|
|
856
915
|
}
|
|
857
916
|
// ../kit/src/folders.ts
|
|
858
917
|
var SKIPPED_FOLDERS = new Set(["node_modules", "vendor", "dist", "build", "target", "coverage", "venv", "__pycache__"]);
|
|
918
|
+
function ignoredFolders(extra = [], where = {}) {
|
|
919
|
+
const configured = readUserConfig("config", where).ignore;
|
|
920
|
+
const names = Array.isArray(configured) ? configured.filter((name) => typeof name === "string" && name !== "") : [];
|
|
921
|
+
return new Set([...SKIPPED_FOLDERS, ...extra, ...names]);
|
|
922
|
+
}
|
|
859
923
|
// ../kit/src/secrets.ts
|
|
860
924
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
861
925
|
// ../kit/src/sessions.ts
|
|
@@ -1923,7 +1987,7 @@ function printsTrackedEnv(program, args) {
|
|
|
1923
1987
|
// ../env/src/files.ts
|
|
1924
1988
|
var ENV_FILE = /^(?:\.env(?:\..+)?|.+\.env|\.envrc)$/;
|
|
1925
1989
|
var TEMPLATE = /\.(?:example|sample|template|dist|defaults)$/;
|
|
1926
|
-
var SKIPPED =
|
|
1990
|
+
var SKIPPED = ignoredFolders([".git", ".kay"]);
|
|
1927
1991
|
|
|
1928
1992
|
// ../env/src/guard-paths.ts
|
|
1929
1993
|
var REDIRECT = /^\d*[<>]+&?/;
|
|
@@ -2294,7 +2358,10 @@ function commitMessageViolation(message) {
|
|
|
2294
2358
|
}
|
|
2295
2359
|
|
|
2296
2360
|
// ../out/src/fold.ts
|
|
2297
|
-
var FOLD_LINES = 150;
|
|
2361
|
+
var FOLD_LINES = configuredNumber("out", "lines", 150);
|
|
2362
|
+
var HEAD = configuredNumber("out", "head", 40);
|
|
2363
|
+
var TAIL = configuredNumber("out", "tail", 60);
|
|
2364
|
+
var MAX_WIDTH = configuredNumber("out", "width", 300);
|
|
2298
2365
|
|
|
2299
2366
|
// ../out/src/wrap.ts
|
|
2300
2367
|
var ASSIGNMENT4 = /^\w+=/;
|
|
@@ -2425,9 +2492,9 @@ function wrap(command) {
|
|
|
2425
2492
|
}
|
|
2426
2493
|
|
|
2427
2494
|
// ../pkg/src/added.ts
|
|
2428
|
-
import { existsSync, readdirSync, readFileSync as
|
|
2429
|
-
import { dirname as dirname3, join as
|
|
2430
|
-
import
|
|
2495
|
+
import { existsSync as existsSync2, readdirSync, readFileSync as readFileSync4 } from "fs";
|
|
2496
|
+
import { dirname as dirname3, join as join4 } from "path";
|
|
2497
|
+
import process13 from "process";
|
|
2431
2498
|
var REDIRECT2 = /^\d*[<>]/;
|
|
2432
2499
|
var BARE_REDIRECT = /^\d*(?:[<>]|>>)$/;
|
|
2433
2500
|
var ADDS = {
|
|
@@ -2451,7 +2518,7 @@ var PUBLISH = /\b(?:ft|cli\.ts)\s+release\b|\b(?:bun|npm|pnpm|yarn)\s+publish\b|
|
|
|
2451
2518
|
var EXACT_SPEC = /^(?:@[^/]+\/)?[^@]+@\d+\.\d+\.\d+(?:[-+][\w.+-]+)?$/;
|
|
2452
2519
|
function manifestName(folder) {
|
|
2453
2520
|
try {
|
|
2454
|
-
const { name } = JSON.parse(
|
|
2521
|
+
const { name } = JSON.parse(readFileSync4(join4(folder, "package.json"), "utf8"));
|
|
2455
2522
|
return typeof name === "string" ? name : undefined;
|
|
2456
2523
|
} catch {
|
|
2457
2524
|
return;
|
|
@@ -2459,17 +2526,17 @@ function manifestName(folder) {
|
|
|
2459
2526
|
}
|
|
2460
2527
|
function ownPackages() {
|
|
2461
2528
|
const names = new Set;
|
|
2462
|
-
let folder =
|
|
2529
|
+
let folder = process13.cwd();
|
|
2463
2530
|
while (true) {
|
|
2464
2531
|
const name = manifestName(folder);
|
|
2465
2532
|
if (name !== undefined)
|
|
2466
2533
|
names.add(name);
|
|
2467
|
-
for (const member of
|
|
2468
|
-
const memberName = manifestName(
|
|
2534
|
+
for (const member of existsSync2(join4(folder, "packages")) ? readdirSync(join4(folder, "packages")) : []) {
|
|
2535
|
+
const memberName = manifestName(join4(folder, "packages", member));
|
|
2469
2536
|
if (memberName !== undefined)
|
|
2470
2537
|
names.add(memberName);
|
|
2471
2538
|
}
|
|
2472
|
-
if (
|
|
2539
|
+
if (existsSync2(join4(folder, ".git")) || dirname3(folder) === folder)
|
|
2473
2540
|
return names;
|
|
2474
2541
|
folder = dirname3(folder);
|
|
2475
2542
|
}
|
|
@@ -2660,9 +2727,14 @@ var STAGE_BY_PATH = `Stage by path: the tree carries unrelated work and peer ses
|
|
|
2660
2727
|
var STASH = "Never `git stash`: a pop with nothing saved applies someone else's old stash. To prove a test fails without your change, `proof --keep <test> -- <cmd>` runs it at HEAD in a temporary worktree and again here; `base -- <cmd>` runs any command at HEAD (`--ref <rev>` another commit), the working tree untouched. To set edits aside before a cherry-pick, a rebase or a checkout, commit them with `gc` first, or do the other work in `git worktree add`.";
|
|
2661
2728
|
var ADD_ALL = `\`git add\` over the whole tree is refused. ${STAGE_BY_PATH}`;
|
|
2662
2729
|
var COMMIT_ALL = `\`git commit -a\` stages the whole tree and is refused. ${STAGE_BY_PATH}`;
|
|
2663
|
-
var PUSH = "`git push`
|
|
2730
|
+
var PUSH = "`git push` and `gp` run only when the user asked for a push in their latest prompt: say the commit is ready and let them ask.";
|
|
2664
2731
|
var CONFIG_OVERRIDE = "Never set git config that runs a program or defines a command (`-c alias.*`, `--config-env`, `core.sshCommand`, `core.hooksPath`, `core.pager`, credential helpers, `git config alias.*`): it sidesteps every rule here. Run the git command itself.";
|
|
2665
2732
|
var TAGS_APART = "A tag goes out with its branch in one `git push --atomic origin <branch> <tag>`, never a separate `git push --tags`.";
|
|
2733
|
+
function gitCommandOf(program, args) {
|
|
2734
|
+
if (program === "gp")
|
|
2735
|
+
return { name: "push", rest: args.includes("--") ? args.slice(args.indexOf("--") + 1) : [] };
|
|
2736
|
+
return program === "git" ? subcommandOf(args) : undefined;
|
|
2737
|
+
}
|
|
2666
2738
|
function subcommandOf(args) {
|
|
2667
2739
|
for (let i = 0;i < args.length; i++) {
|
|
2668
2740
|
const arg = args[i] ?? "";
|
|
@@ -2732,11 +2804,12 @@ function gitViolation(command, isPushAsked = false) {
|
|
|
2732
2804
|
const { program, args, assignments } = parseCommand(piece.command);
|
|
2733
2805
|
if (assignments.some((assignment) => RISKY_ENV.test(assignment)))
|
|
2734
2806
|
return CONFIG_OVERRIDE;
|
|
2735
|
-
|
|
2807
|
+
const git = gitCommandOf(program, args);
|
|
2808
|
+
if (git === undefined)
|
|
2736
2809
|
continue;
|
|
2737
|
-
if (overridesConfig(args))
|
|
2810
|
+
if (program === "git" && overridesConfig(args))
|
|
2738
2811
|
return CONFIG_OVERRIDE;
|
|
2739
|
-
const { name, rest } =
|
|
2812
|
+
const { name, rest } = git;
|
|
2740
2813
|
if (name === "config" && rest.length > 1 && rest.some((arg) => RISKY_CONFIG.test(arg)) && !rest.some((arg) => READS_CONFIG.has(arg)))
|
|
2741
2814
|
return CONFIG_OVERRIDE;
|
|
2742
2815
|
if (name === "stash" && !READ_ONLY_STASH.has(rest[0] ?? ""))
|
|
@@ -3020,10 +3093,10 @@ function destructiveViolation(command) {
|
|
|
3020
3093
|
const { program, args } = parseCommand(piece.command);
|
|
3021
3094
|
if (program === "rm" && removesBroadly(args))
|
|
3022
3095
|
return BROAD_RM;
|
|
3023
|
-
|
|
3096
|
+
const git = gitCommandOf(program, args);
|
|
3097
|
+
if (git === undefined)
|
|
3024
3098
|
continue;
|
|
3025
|
-
const
|
|
3026
|
-
const violation = gitDestructive(name, rest);
|
|
3099
|
+
const violation = gitDestructive(git.name, git.rest);
|
|
3027
3100
|
if (violation !== null)
|
|
3028
3101
|
return violation;
|
|
3029
3102
|
}
|
|
@@ -3573,8 +3646,10 @@ var BINS = {
|
|
|
3573
3646
|
sub: "`sub --from <text> --to <text> <file>...`: replaces text, writing nothing unless each match count is as expected (1, `-n N`, `--all`), `--regex`, `--dry-run --diff`; multi-line text goes as SEARCH/REPLACE blocks on stdin from a quoted heredoc (`sub <file> <<'EOF'`, see `sub --help`), no escaping; use it instead of sed -i, a bun -e or python editor.",
|
|
3574
3647
|
base: "`base [--ref <rev>] [--keep <path>...] -- <cmd>`: runs a command at HEAD (or a ref) in a temporary worktree, removed afterwards, ignored env files and node_modules/vendor linked; use it instead of git stash to see whether a test fails without your change (`--keep` the new test).",
|
|
3575
3648
|
proof: "`proof [--ref <rev>] [--keep <path>...] -- <cmd>`: runs the command at HEAD (or a ref) in a temporary worktree, then in the working tree, and exits 0 only when it fails before and passes now, printing the failing side's output; use it to prove a new test fails without your change (`--keep` the test).",
|
|
3576
|
-
gst: "`gst [--log N]`: branch, upstream, changed counts and HEAD in one line, then the changed paths by staged, unstaged and untracked; use it instead of `git status --short` and `git log --oneline`.",
|
|
3649
|
+
gst: "`gst [--log N]`: branch, upstream, changed counts and HEAD in one line, then the changed paths by staged, unstaged and untracked, `gst --repos` one line per repository nested below; use it instead of `git status --short` and `git log --oneline`.",
|
|
3577
3650
|
gc: "`gc <title> [-m <line>]... [--ticket <line>]... -- <path>...`: commits exactly those paths, refusing with nothing staged when the message breaks a commit rule, a path has no change or other files are already staged; use it instead of chaining git add, status and commit.",
|
|
3651
|
+
gp: "`gp [--repos] [dir]...`: pushes the current branch when it is ahead of its upstream, with `-u` to origin when it has none, `--repos` every repository nested below too, `--dry-run` saying what would go, flags for git push after `--`; only when the user asked for a push, like `git push`.",
|
|
3652
|
+
gb: "`gb [branch]`: switches to a branch, a remote one getting its tracking branch, `gb -c <name>` creating one; without a terminal it lists the branches (`--json`), with how each stands against its upstream and whether it is merged.",
|
|
3578
3653
|
waitfor: "`waitfor --file <log> --until <regex> [--fail <regex>]`, `--port <n>`, `--url <url>`, `--pid <n>` (`-t 5m`): waits until the line shows, the port listens, the URL answers 2xx or the process exits, exits 1 on a --fail line with the lines around it, 2 on a timeout with the last lines; `waitfor task <id>` waits for a background Bash task and prints its exit code, last lines and failure lines; use it instead of sleep and tail, `until ! pgrep -f` or polling loops.",
|
|
3579
3654
|
slot: "`slot [-l <lock>] -- <cmd>`: waits for one of the machine-wide slots shared by every session (half the CPUs), then runs the command; `--lock <name>` also holds a named lock for what must not run twice (a shared test database), `slot ls` lists holders and waiters; run heavy jobs (phpunit, playwright, vue-tsc, a full test suite) through it instead of a flock wrapper.",
|
|
3580
3655
|
gl: "`gl ci [ref]` waits for the GitLab pipeline covering a commit and prints each failed job's tests (exit 0/1/2), `gl runs`, `gl logs <pipeline> [-j job] [--full]`; use it instead of `glab ci` or `glab api .../pipelines/<id>/jobs` polled in a sleep loop.",
|
|
@@ -3732,7 +3807,7 @@ var MAX_READ = 1e6;
|
|
|
3732
3807
|
function readText(path) {
|
|
3733
3808
|
try {
|
|
3734
3809
|
const stats = statSync(path, { throwIfNoEntry: false });
|
|
3735
|
-
return stats?.isFile() === true && stats.size <= MAX_READ ?
|
|
3810
|
+
return stats?.isFile() === true && stats.size <= MAX_READ ? readFileSync5(path, "utf8") : null;
|
|
3736
3811
|
} catch {
|
|
3737
3812
|
return null;
|
|
3738
3813
|
}
|
|
@@ -3751,14 +3826,14 @@ ${checkUsage}`);
|
|
|
3751
3826
|
const given = positionals[0] ?? "-";
|
|
3752
3827
|
const command = given === "-" ? await readInput("-", { noStdin: "give the command, or pipe it on stdin" }) : given;
|
|
3753
3828
|
const context = {
|
|
3754
|
-
cwd: values.cwd ??
|
|
3755
|
-
home:
|
|
3829
|
+
cwd: values.cwd ?? process14.cwd(),
|
|
3830
|
+
home: homedir3(),
|
|
3756
3831
|
isPushAsked: values["push-asked"] === true,
|
|
3757
3832
|
canRewrite: true,
|
|
3758
3833
|
canWrap: values["no-wrap"] !== true && Bun.which("out") !== null,
|
|
3759
3834
|
canSub: Bun.which("sub") !== null,
|
|
3760
3835
|
read: readText,
|
|
3761
|
-
exists:
|
|
3836
|
+
exists: existsSync3
|
|
3762
3837
|
};
|
|
3763
3838
|
const verdict = await checkCommand(command, context);
|
|
3764
3839
|
if (values.json === true)
|
|
@@ -3803,7 +3878,7 @@ ${" ".repeat(16)} -> ${rule.route}${rule.runs === "hook" ? " (hook only)" : ""}`
|
|
|
3803
3878
|
return 0;
|
|
3804
3879
|
}
|
|
3805
3880
|
function machine() {
|
|
3806
|
-
const home =
|
|
3881
|
+
const home = homedir3();
|
|
3807
3882
|
const stateDir = `${stateHome()}/guard`;
|
|
3808
3883
|
return {
|
|
3809
3884
|
home,
|
|
@@ -3811,7 +3886,7 @@ function machine() {
|
|
|
3811
3886
|
has: (bin) => Bun.which(bin) !== null,
|
|
3812
3887
|
run: async (argv, cwd) => runCommand(argv, { cwd }),
|
|
3813
3888
|
read: readText,
|
|
3814
|
-
exists:
|
|
3889
|
+
exists: existsSync3,
|
|
3815
3890
|
write: (path, text) => {
|
|
3816
3891
|
mkdirSync2(dirname4(path), { recursive: true });
|
|
3817
3892
|
writeFileSync(path, text);
|
|
@@ -3837,7 +3912,7 @@ ${hookUsage}`, 1);
|
|
|
3837
3912
|
console.log(JSON.stringify(reply));
|
|
3838
3913
|
return 0;
|
|
3839
3914
|
}
|
|
3840
|
-
|
|
3915
|
+
process14.exitCode = await runCli({
|
|
3841
3916
|
name: "guard",
|
|
3842
3917
|
usage: false,
|
|
3843
3918
|
about: "the safety rules a Claude Code Bash call is held to, each refusal naming the route",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-guard",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.11.
|
|
4
|
+
"version": "0.11.1",
|
|
5
5
|
"description": "guard: the safety rules a Claude Code Bash call is held to (git, remote machines, SQL writes, env files, package managers, commit messages), each refusal naming the route.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -36,12 +36,12 @@
|
|
|
36
36
|
"guard": "$npm_execpath src/cli.ts"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@kaynooo/cli-db": "0.12.
|
|
40
|
-
"@kaynooo/cli-edit": "0.9.
|
|
41
|
-
"@kaynooo/cli-env": "0.13.
|
|
42
|
-
"@kaynooo/cli-git": "0.13.
|
|
43
|
-
"@kaynooo/cli-kit": "0.
|
|
44
|
-
"@kaynooo/cli-out": "0.
|
|
45
|
-
"@kaynooo/cli-pkg": "0.
|
|
39
|
+
"@kaynooo/cli-db": "0.12.1",
|
|
40
|
+
"@kaynooo/cli-edit": "0.9.1",
|
|
41
|
+
"@kaynooo/cli-env": "0.13.1",
|
|
42
|
+
"@kaynooo/cli-git": "0.13.1",
|
|
43
|
+
"@kaynooo/cli-kit": "0.38.1",
|
|
44
|
+
"@kaynooo/cli-out": "0.14.3",
|
|
45
|
+
"@kaynooo/cli-pkg": "0.13.3"
|
|
46
46
|
}
|
|
47
47
|
}
|