@kaynooo/cli-env 0.13.0 → 0.13.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 +1 -0
- package/dist/ev.js +175 -127
- package/dist/redact.js +104 -53
- package/package.json +2 -2
- package/src/commands/audit.ts +8 -6
- package/src/commands/compare.ts +7 -6
- package/src/commands/edit.ts +30 -21
- package/src/commands/pins.ts +5 -3
- package/src/commands/read.ts +13 -12
- package/src/commands/redact.ts +5 -2
- package/src/files.ts +2 -2
package/dist/redact.js
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/redact.ts
|
|
5
|
-
import
|
|
5
|
+
import process16 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process10 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
9
9
|
|
|
10
10
|
// ../kit/src/format.ts
|
|
11
11
|
import process7 from "process";
|
|
12
|
+
|
|
12
13
|
// ../../node_modules/.bun/@kaynooo+utils@3.0.0+4c7ddab9986847c5/node_modules/@kaynooo/utils/dist/chunk-rxztfxgv.js
|
|
13
14
|
var N = new Map;
|
|
14
15
|
var Nt = /\s+\S*$/;
|
|
@@ -26,6 +27,7 @@ function ko(t, e, r = "...") {
|
|
|
26
27
|
}
|
|
27
28
|
var be = new Map(["\u20AC", "", "\u201A", "\u0192", "\u201E", "\u2026", "\u2020", "\u2021", "\u02C6", "\u2030", "\u0160", "\u2039", "\u0152", "", "\u017D", "", "", "\u2018", "\u2019", "\u201C", "\u201D", "\u2022", "\u2013", "\u2014", "\u02DC", "\u2122", "\u0161", "\u203A", "\u0153", "", "\u017E", "\u0178"].map((t, e) => [t, 128 + e]).filter(([t]) => t));
|
|
28
29
|
var z = new Map;
|
|
30
|
+
|
|
29
31
|
// ../kit/src/mode.ts
|
|
30
32
|
import process2 from "process";
|
|
31
33
|
var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
|
|
@@ -71,7 +73,7 @@ function C(r, e = globalThis.process?.env ?? {}) {
|
|
|
71
73
|
return false;
|
|
72
74
|
return r?.isTTY === true;
|
|
73
75
|
}
|
|
74
|
-
function
|
|
76
|
+
function f(r, e = []) {
|
|
75
77
|
let l = "", n = "";
|
|
76
78
|
for (let t = 0, i = e.length;t < i; t++)
|
|
77
79
|
l += `\x1B[${e[t][0]}m`, n = `\x1B[${e[t][1]}m${n}`;
|
|
@@ -81,7 +83,7 @@ function f2(r, e = []) {
|
|
|
81
83
|
if (Array.isArray(a)) {
|
|
82
84
|
let s = g.get(i);
|
|
83
85
|
if (s === undefined)
|
|
84
|
-
s =
|
|
86
|
+
s = f(r, [...e, a]), g.set(i, s);
|
|
85
87
|
return s;
|
|
86
88
|
}
|
|
87
89
|
if (typeof a === "function")
|
|
@@ -90,20 +92,20 @@ function f2(r, e = []) {
|
|
|
90
92
|
if (u === undefined) {
|
|
91
93
|
if (g.size >= 64)
|
|
92
94
|
g.clear();
|
|
93
|
-
u =
|
|
95
|
+
u = f(r, [...e, a(...s)]), g.set(y, u);
|
|
94
96
|
}
|
|
95
97
|
return u;
|
|
96
98
|
};
|
|
97
99
|
throw Error(`Unknown style: ${String(i)}`);
|
|
98
100
|
} });
|
|
99
101
|
}
|
|
100
|
-
function
|
|
101
|
-
return
|
|
102
|
+
function Fe(r = {}) {
|
|
103
|
+
return f(r);
|
|
102
104
|
}
|
|
103
|
-
var
|
|
105
|
+
var o = Fe();
|
|
104
106
|
|
|
105
107
|
// ../kit/src/theme.ts
|
|
106
|
-
import { existsSync as
|
|
108
|
+
import { existsSync as existsSync3, readFileSync as readFileSync2, statSync } from "fs";
|
|
107
109
|
import { homedir as homedir2 } from "os";
|
|
108
110
|
import { join as join3, resolve as resolve2 } from "path";
|
|
109
111
|
import process5 from "process";
|
|
@@ -124,6 +126,7 @@ function findUp(names, from = process3.cwd(), stop = () => false) {
|
|
|
124
126
|
}
|
|
125
127
|
|
|
126
128
|
// ../kit/src/user-config.ts
|
|
129
|
+
import { existsSync as existsSync2, readFileSync } from "fs";
|
|
127
130
|
import { homedir } from "os";
|
|
128
131
|
import { join as join2 } from "path";
|
|
129
132
|
import process4 from "process";
|
|
@@ -134,6 +137,24 @@ function configBase(where = {}) {
|
|
|
134
137
|
function userConfigPath(bin, where = {}) {
|
|
135
138
|
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
136
139
|
}
|
|
140
|
+
function userConfigReadPath(bin, legacy = [], where = {}) {
|
|
141
|
+
const current = userConfigPath(bin, where);
|
|
142
|
+
if (existsSync2(current))
|
|
143
|
+
return current;
|
|
144
|
+
const base = configBase(where);
|
|
145
|
+
return legacy.map((parts) => join2(base, ...parts)).find((path) => existsSync2(path)) ?? current;
|
|
146
|
+
}
|
|
147
|
+
function readUserConfig(bin, where = {}, legacy = []) {
|
|
148
|
+
const path = userConfigReadPath(bin, legacy, where);
|
|
149
|
+
if (!existsSync2(path))
|
|
150
|
+
return {};
|
|
151
|
+
try {
|
|
152
|
+
const parsed = Bun.JSONC.parse(readFileSync(path, "utf8"));
|
|
153
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : {};
|
|
154
|
+
} catch {
|
|
155
|
+
return {};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
137
158
|
|
|
138
159
|
// ../kit/src/theme.ts
|
|
139
160
|
var ENDS = new Map;
|
|
@@ -200,7 +221,7 @@ function readThemeFile(path, warn) {
|
|
|
200
221
|
warn(`${path} is not a regular file under ${MAX_FILE_BYTES / 1024} KiB, theme ignored`);
|
|
201
222
|
return;
|
|
202
223
|
}
|
|
203
|
-
const parsed = JSON.parse(
|
|
224
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
204
225
|
if (isRecord(parsed))
|
|
205
226
|
return parsed;
|
|
206
227
|
warn(`${path} is not a JSON object, theme ignored`);
|
|
@@ -266,7 +287,7 @@ function applyFile(theme, file, where, warn) {
|
|
|
266
287
|
}
|
|
267
288
|
}
|
|
268
289
|
function endsWalk(home) {
|
|
269
|
-
return (dir) => dir === resolve2(home) ||
|
|
290
|
+
return (dir) => dir === resolve2(home) || existsSync3(join3(dir, ".git"));
|
|
270
291
|
}
|
|
271
292
|
var warned = new Set;
|
|
272
293
|
function warnOnce(message) {
|
|
@@ -322,7 +343,7 @@ function takesControlCodes(stream, env = process6.env) {
|
|
|
322
343
|
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
|
|
323
344
|
}
|
|
324
345
|
function paint(stream, env = process6.env) {
|
|
325
|
-
return
|
|
346
|
+
return Fe({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
326
347
|
}
|
|
327
348
|
function marked(mark, text, stream, detail, env = process6.env) {
|
|
328
349
|
const colors = paint(stream, env);
|
|
@@ -336,6 +357,9 @@ function marked(mark, text, stream, detail, env = process6.env) {
|
|
|
336
357
|
function ok(text, detail) {
|
|
337
358
|
console.log(marked("ok", text, process6.stdout, detail));
|
|
338
359
|
}
|
|
360
|
+
function warn(text, detail) {
|
|
361
|
+
console.error(marked("warn", text, process6.stderr, detail));
|
|
362
|
+
}
|
|
339
363
|
function role(name, stream, env = process6.env) {
|
|
340
364
|
const colors = paint(stream, env);
|
|
341
365
|
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name], env) : colors;
|
|
@@ -416,18 +440,14 @@ var FULL_STOP = /\.$/;
|
|
|
416
440
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
417
441
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
418
442
|
}
|
|
419
|
-
function helpText(usage, options, colors = paint(process8.stdout), { examples = [],
|
|
420
|
-
return [usageLine(usage, colors),
|
|
443
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], exits = [] } = {}) {
|
|
444
|
+
return [usageLine(usage, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
421
445
|
`);
|
|
422
446
|
}
|
|
423
447
|
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
424
448
|
if (exits.length === 0)
|
|
425
449
|
return [];
|
|
426
|
-
|
|
427
|
-
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
428
|
-
}
|
|
429
|
-
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
430
|
-
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
450
|
+
return ["", `${colors.bold("Exit codes:")} ${exits.map(([code, meaning]) => `${colors.cyan(String(code))} ${meaning}`).join(", ")}`];
|
|
431
451
|
}
|
|
432
452
|
function wantsHelpJson(env = process8.env) {
|
|
433
453
|
return env.KIT_HELP === "json";
|
|
@@ -715,21 +735,45 @@ function nearestOption(message, options, allowNegative) {
|
|
|
715
735
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
716
736
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
717
737
|
}
|
|
738
|
+
// ../kit/src/argv.ts
|
|
739
|
+
import { readFileSync as readFileSync3, realpathSync } from "fs";
|
|
740
|
+
import { resolve as resolve3 } from "path";
|
|
741
|
+
import process11 from "process";
|
|
742
|
+
function commandLine() {
|
|
743
|
+
try {
|
|
744
|
+
return readFileSync3("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
745
|
+
} catch {
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
function realPath(word) {
|
|
750
|
+
try {
|
|
751
|
+
return realpathSync(resolve3(word));
|
|
752
|
+
} catch {
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
function scriptArgs(argv = process11.argv.slice(2), cmdline = commandLine(), script = process11.argv[1]) {
|
|
757
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
758
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve3(word) === script || realPath(word) === target));
|
|
759
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
760
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
761
|
+
}
|
|
718
762
|
// ../kit/src/progress.ts
|
|
719
763
|
var ESC = String.fromCharCode(27);
|
|
720
764
|
var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
|
|
721
765
|
var RESET_STYLE = `${ESC}[0m`;
|
|
722
766
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
723
767
|
// ../kit/src/cli.ts
|
|
724
|
-
import { readFileSync as
|
|
768
|
+
import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
|
|
725
769
|
import { dirname as dirname3, join as join5 } from "path";
|
|
726
|
-
import
|
|
770
|
+
import process13 from "process";
|
|
727
771
|
|
|
728
772
|
// ../kit/src/usage.ts
|
|
729
773
|
import { appendFileSync, mkdirSync } from "fs";
|
|
730
774
|
import { homedir as homedir3 } from "os";
|
|
731
775
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
732
|
-
import
|
|
776
|
+
import process12 from "process";
|
|
733
777
|
|
|
734
778
|
// ../kit/src/logs.ts
|
|
735
779
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -743,14 +787,14 @@ var usageError;
|
|
|
743
787
|
function noteUsageError(message) {
|
|
744
788
|
usageError = message;
|
|
745
789
|
}
|
|
746
|
-
function stateHome(env =
|
|
790
|
+
function stateHome(env = process12.env) {
|
|
747
791
|
const dir = env.XDG_STATE_HOME;
|
|
748
792
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
749
793
|
}
|
|
750
|
-
function usagePath(env =
|
|
794
|
+
function usagePath(env = process12.env) {
|
|
751
795
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
752
796
|
}
|
|
753
|
-
function usageRecorded(env =
|
|
797
|
+
function usageRecorded(env = process12.env) {
|
|
754
798
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
755
799
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
756
800
|
}
|
|
@@ -764,7 +808,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
764
808
|
usageError = undefined;
|
|
765
809
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
766
810
|
}
|
|
767
|
-
function recordUsage(entry, env =
|
|
811
|
+
function recordUsage(entry, env = process12.env) {
|
|
768
812
|
if (!usageRecorded(env))
|
|
769
813
|
return;
|
|
770
814
|
try {
|
|
@@ -841,7 +885,6 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
841
885
|
header(colors, summary),
|
|
842
886
|
"",
|
|
843
887
|
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
844
|
-
...replacesRows(replaces, colors),
|
|
845
888
|
"",
|
|
846
889
|
...await commandRows(commands, colors),
|
|
847
890
|
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
@@ -855,8 +898,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
855
898
|
`);
|
|
856
899
|
}
|
|
857
900
|
function printError(prefix, message, hint, cause) {
|
|
858
|
-
const colors = paint(
|
|
859
|
-
const styled = takesControlCodes(
|
|
901
|
+
const colors = paint(process13.stderr);
|
|
902
|
+
const styled = takesControlCodes(process13.stderr);
|
|
860
903
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
861
904
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
862
905
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -899,11 +942,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
899
942
|
}
|
|
900
943
|
}
|
|
901
944
|
if (name === undefined && level.path.length > 1) {
|
|
902
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
945
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stderr));
|
|
903
946
|
return 2;
|
|
904
947
|
}
|
|
905
948
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
906
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
949
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stdout));
|
|
907
950
|
return 0;
|
|
908
951
|
}
|
|
909
952
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -959,24 +1002,24 @@ function candidateLine(candidate) {
|
|
|
959
1002
|
}
|
|
960
1003
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
961
1004
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
962
|
-
|
|
1005
|
+
const typed = [];
|
|
963
1006
|
let at = 0;
|
|
964
1007
|
while (at < before.length) {
|
|
965
1008
|
const word = before[at];
|
|
966
1009
|
if (word === "--") {
|
|
967
|
-
|
|
1010
|
+
typed.push(...before.slice(at + 1));
|
|
968
1011
|
break;
|
|
969
1012
|
}
|
|
970
1013
|
if (!word.startsWith("-"))
|
|
971
|
-
|
|
1014
|
+
typed.push(word);
|
|
972
1015
|
else if (takesValue(word))
|
|
973
1016
|
at++;
|
|
974
1017
|
at++;
|
|
975
1018
|
}
|
|
976
1019
|
const last = positionals.at(-1);
|
|
977
|
-
if (
|
|
978
|
-
return positionals[
|
|
979
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
1020
|
+
if (typed.length < positionals.length)
|
|
1021
|
+
return { name: positionals[typed.length], typed };
|
|
1022
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
980
1023
|
}
|
|
981
1024
|
async function leafCandidates(run, words) {
|
|
982
1025
|
const completing = await completingOf(run);
|
|
@@ -984,12 +1027,12 @@ async function leafCandidates(run, words) {
|
|
|
984
1027
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
985
1028
|
const flag = words.at(-2);
|
|
986
1029
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1030
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
987
1031
|
if (option?.type === "string") {
|
|
988
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1032
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
989
1033
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
990
1034
|
}
|
|
991
|
-
const
|
|
992
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1035
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
993
1036
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
994
1037
|
}
|
|
995
1038
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
@@ -1020,9 +1063,13 @@ async function printCandidates(found) {
|
|
|
1020
1063
|
return 0;
|
|
1021
1064
|
}
|
|
1022
1065
|
function packageVersion(entry = Bun.main) {
|
|
1023
|
-
|
|
1066
|
+
let resolved = entry;
|
|
1067
|
+
try {
|
|
1068
|
+
resolved = realpathSync2(entry);
|
|
1069
|
+
} catch {}
|
|
1070
|
+
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
1024
1071
|
try {
|
|
1025
|
-
const { version } = JSON.parse(
|
|
1072
|
+
const { version } = JSON.parse(readFileSync4(join5(dir, "package.json"), "utf8"));
|
|
1026
1073
|
if (typeof version === "string")
|
|
1027
1074
|
return version;
|
|
1028
1075
|
} catch {}
|
|
@@ -1030,7 +1077,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
1030
1077
|
return "0.0.0";
|
|
1031
1078
|
}
|
|
1032
1079
|
}
|
|
1033
|
-
async function runCli(cli, argv =
|
|
1080
|
+
async function runCli(cli, argv = scriptArgs()) {
|
|
1034
1081
|
const version = cli.version ?? packageVersion();
|
|
1035
1082
|
if (argv[0] === COMPLETE)
|
|
1036
1083
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
@@ -1057,7 +1104,7 @@ function commandPath(scope, argv) {
|
|
|
1057
1104
|
}
|
|
1058
1105
|
return path.length === 0 ? undefined : path.join(" ");
|
|
1059
1106
|
}
|
|
1060
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1107
|
+
async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
|
|
1061
1108
|
if (argv[0] === COMPLETE)
|
|
1062
1109
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1063
1110
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1080,7 +1127,7 @@ async function runMain({ name, version, run, redact, usage }, argv = process12.a
|
|
|
1080
1127
|
// ../kit/src/compose.ts
|
|
1081
1128
|
async function captureOutput(argv, { cwd, env } = {}) {
|
|
1082
1129
|
const run = Bun.spawn(argv, { ...cwd === undefined ? {} : { cwd }, env: { ...process.env, ...env }, stdin: "ignore", stdout: "pipe", stderr: "pipe" });
|
|
1083
|
-
const [stdout, stderr, code] = await Promise.all([run.stdout.text(), run.stderr.text(), run.exited]);
|
|
1130
|
+
const [stdout, stderr, code] = await Promise.all([new Response(run.stdout).text(), new Response(run.stderr).text(), run.exited]);
|
|
1084
1131
|
return { code, stdout, stderr };
|
|
1085
1132
|
}
|
|
1086
1133
|
async function compose(args, { flags = [], ...options } = {}) {
|
|
@@ -1088,6 +1135,11 @@ async function compose(args, { flags = [], ...options } = {}) {
|
|
|
1088
1135
|
}
|
|
1089
1136
|
// ../kit/src/folders.ts
|
|
1090
1137
|
var SKIPPED_FOLDERS = new Set(["node_modules", "vendor", "dist", "build", "target", "coverage", "venv", "__pycache__"]);
|
|
1138
|
+
function ignoredFolders(extra = [], where = {}) {
|
|
1139
|
+
const configured = readUserConfig("config", where).ignore;
|
|
1140
|
+
const names = Array.isArray(configured) ? configured.filter((name) => typeof name === "string" && name !== "") : [];
|
|
1141
|
+
return new Set([...SKIPPED_FOLDERS, ...extra, ...names]);
|
|
1142
|
+
}
|
|
1091
1143
|
// ../kit/src/secrets.ts
|
|
1092
1144
|
var SECRET_PART = /token|secret|passw|credential|private|dsn|apikey|accesskey|key$/i;
|
|
1093
1145
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
@@ -1100,7 +1152,7 @@ var {Glob } = globalThis.Bun;
|
|
|
1100
1152
|
var SESSION_FILES = new Glob("*.json");
|
|
1101
1153
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1102
1154
|
// src/commands/redact.ts
|
|
1103
|
-
import
|
|
1155
|
+
import process15 from "process";
|
|
1104
1156
|
|
|
1105
1157
|
// src/files.ts
|
|
1106
1158
|
import { chmod, chown, readdir, realpath, rename, rm, stat, writeFile } from "fs/promises";
|
|
@@ -1397,7 +1449,7 @@ function maskValue(key, value, known) {
|
|
|
1397
1449
|
var ENV_FILE = /^(?:\.env(?:\..+)?|.+\.env|\.envrc)$/;
|
|
1398
1450
|
var TEMPLATE = /\.(?:example|sample|template|dist|defaults)$/;
|
|
1399
1451
|
var KNOWN_DEPTH = 3;
|
|
1400
|
-
var SKIPPED =
|
|
1452
|
+
var SKIPPED = ignoredFolders([".git", ".kay"]);
|
|
1401
1453
|
var LOCK_WAIT_MS = 5000;
|
|
1402
1454
|
var LOCK_STALE_MS = 30000;
|
|
1403
1455
|
var REAP_STALE_MS = 5000;
|
|
@@ -1668,15 +1720,14 @@ function check(text, known) {
|
|
|
1668
1720
|
var usage2 = `redact [--check]
|
|
1669
1721
|
|
|
1670
1722
|
Copies stdin to stdout line by line with secrets masked: known token formats (GitHub, GitLab, Slack, AWS, npm, Stripe, Google and SendGrid keys, sk-, JWTs, Authorization headers, password= and secret= pairs), private key blocks, the password and the tokens of any URL and the values of secret-looking keys in this folder's .env* files.
|
|
1671
|
-
--check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check
|
|
1672
|
-
|
|
1673
|
-
Exits 0 when it copied the input or found no secret, 1 when --check finds one, 2 on a usage error.`;
|
|
1723
|
+
--check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check\`.`;
|
|
1724
|
+
var EXITS = [[0, "it copied the input or found no secret"], [1, "--check finds one"], [2, "a usage error"]];
|
|
1674
1725
|
var options = {
|
|
1675
1726
|
check: { type: "boolean", description: "list where secrets are instead of masking them, exit 1 when found" },
|
|
1676
1727
|
"no-env": { type: "boolean", description: "do not search for the values of the .env* files" }
|
|
1677
1728
|
};
|
|
1678
1729
|
async function redact(args) {
|
|
1679
|
-
const parsed = parse(args, usage2, options, { replaces: "printing a command's secrets into a log or a transcript", examples: [["docker compose config | redact", "the resolved compose file, secrets masked"], ["git diff --cached | redact --check", "refuse a commit carrying a secret"]] });
|
|
1730
|
+
const parsed = parse(args, usage2, options, { exits: EXITS, replaces: "printing a command's secrets into a log or a transcript", examples: [["docker compose config | redact", "the resolved compose file, secrets masked"], ["git diff --cached | redact --check", "refuse a commit carrying a secret"]] });
|
|
1680
1731
|
const known = parsed.values["no-env"] === true ? undefined : await loadKnown();
|
|
1681
1732
|
if (parsed.values.check === true) {
|
|
1682
1733
|
const findings = check(await readInput("-"), known);
|
|
@@ -1687,10 +1738,10 @@ async function redact(args) {
|
|
|
1687
1738
|
const redactor = new Redactor(known);
|
|
1688
1739
|
const decoder = new TextDecoder;
|
|
1689
1740
|
for await (const chunk of Bun.stdin.stream())
|
|
1690
|
-
|
|
1691
|
-
|
|
1741
|
+
process15.stdout.write(redactor.push(decoder.decode(chunk, { stream: true })));
|
|
1742
|
+
process15.stdout.write(redactor.push(decoder.decode()) + redactor.end());
|
|
1692
1743
|
return 0;
|
|
1693
1744
|
}
|
|
1694
1745
|
|
|
1695
1746
|
// src/redact.ts
|
|
1696
|
-
|
|
1747
|
+
process16.exitCode = await runMain({ name: "redact", run: redact });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-env",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.13.
|
|
4
|
+
"version": "0.13.2",
|
|
5
5
|
"description": "ev lists and edits .env files with secrets masked, redact masks secrets in any piped output.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -37,6 +37,6 @@
|
|
|
37
37
|
"redact": "$npm_execpath src/redact.ts"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@kaynooo/cli-kit": "0.
|
|
40
|
+
"@kaynooo/cli-kit": "0.38.2"
|
|
41
41
|
}
|
|
42
42
|
}
|
package/src/commands/audit.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Exposure } from '../audit'
|
|
2
3
|
import type { ComposeFix } from '../fix'
|
|
3
4
|
import { stat } from 'node:fs/promises'
|
|
4
5
|
import { relative, resolve } from 'node:path'
|
|
5
6
|
import process from 'node:process'
|
|
6
|
-
import { fail, jsonOption, parse, printJson, role } from '@kaynooo/cli-kit'
|
|
7
|
+
import { fail, jsonOption, parse, printJson, role, warn } from '@kaynooo/cli-kit'
|
|
7
8
|
import { shellQuote } from '@kaynooo/cli-kit/shell'
|
|
8
9
|
import { audit as findExposed } from '../audit'
|
|
9
10
|
import { fixCompose, shutOthersOut } from '../fix'
|
|
@@ -12,8 +13,9 @@ const usage = `ev audit [dir...] [--fix]
|
|
|
12
13
|
|
|
13
14
|
Lists the .env files any account on the machine can read or git tracks, and the compose files any account can read that write a secret out (a secret-named value or a URL password) instead of reading it from a .env, never printing a value.
|
|
14
15
|
Looks under each folder (the current one by default) down to 5 levels, skipping node_modules, vendor and .git, and leaves .example, .sample, .template and .dist files out. A folder it cannot read is named on stderr.
|
|
15
|
-
With --fix, moves each secret a listed compose file writes out into the .env beside it as \${KEY}, puts it back when \`docker compose config\` resolves differently after, and shuts other accounts out of both files and of every listed .env; a key the .env already sets to another value stays put
|
|
16
|
-
|
|
16
|
+
With --fix, moves each secret a listed compose file writes out into the .env beside it as \${KEY}, puts it back when \`docker compose config\` resolves differently after, and shuts other accounts out of both files and of every listed .env; a key the .env already sets to another value stays put.`
|
|
17
|
+
|
|
18
|
+
const EXITS: ExitRow[] = [[0, 'it finds none (with --fix, when none is left)'], [1, 'it finds one'], [2, 'a usage error']]
|
|
17
19
|
|
|
18
20
|
const options = {
|
|
19
21
|
json: jsonOption('the findings and the folders it could not read'),
|
|
@@ -76,7 +78,7 @@ async function fixAll(exposed: Exposure[], json: boolean): Promise<{ fixed: Comp
|
|
|
76
78
|
}
|
|
77
79
|
|
|
78
80
|
export async function audit(args: string[]): Promise<number> {
|
|
79
|
-
const parsed = parse(args, usage, options)
|
|
81
|
+
const parsed = parse(args, usage, options, { exits: EXITS })
|
|
80
82
|
|
|
81
83
|
const roots = (parsed.positionals.length > 0 ? parsed.positionals : ['.']).map(dir => resolve(dir))
|
|
82
84
|
|
|
@@ -90,11 +92,11 @@ export async function audit(args: string[]): Promise<number> {
|
|
|
90
92
|
|
|
91
93
|
// Container data folders owned by another account often run to dozens, so past a few they are only counted.
|
|
92
94
|
if (!json && unread.length > MAX_UNREAD_LISTED) {
|
|
93
|
-
|
|
95
|
+
warn(`could not read ${unread.length} folders, so they were not audited (--json lists them)`)
|
|
94
96
|
}
|
|
95
97
|
else if (!json) {
|
|
96
98
|
for (const dir of unread)
|
|
97
|
-
|
|
99
|
+
warn(`could not read ${dir}, so it was not audited`)
|
|
98
100
|
}
|
|
99
101
|
|
|
100
102
|
if (parsed.values.fix === true && (json || exposed.length > 0)) {
|
package/src/commands/compare.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import process from 'node:process'
|
|
2
3
|
import { fail, jsonOption, parse, printJson, role } from '@kaynooo/cli-kit'
|
|
3
4
|
import { entries } from '../dotenv'
|
|
@@ -7,17 +8,17 @@ const diffUsage = `ev diff <a> <b>
|
|
|
7
8
|
|
|
8
9
|
Names the keys set in one file and not the other, never their values.
|
|
9
10
|
|
|
10
|
-
--json prints {a, b, onlyA, onlyB}
|
|
11
|
+
--json prints {a, b, onlyA, onlyB}.`
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
const DIFF_EXITS: ExitRow[] = [[0, 'both set the same keys'], [1, 'they differ'], [2, 'a usage error']]
|
|
13
14
|
|
|
14
15
|
const missingUsage = `ev missing [file]
|
|
15
16
|
|
|
16
17
|
Names the keys the reference file sets that the file (.env by default) does not.
|
|
17
18
|
|
|
18
|
-
--json prints {a, b, onlyA, onlyB} with the reference as a, so onlyA is what is missing
|
|
19
|
+
--json prints {a, b, onlyA, onlyB} with the reference as a, so onlyA is what is missing.`
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
const MISSING_EXITS: ExitRow[] = [[0, 'none is missing'], [1, 'some are'], [2, 'a usage error']]
|
|
21
22
|
|
|
22
23
|
const jsonFlag = { json: jsonOption() } as const
|
|
23
24
|
|
|
@@ -26,7 +27,7 @@ async function keys(path: string): Promise<Set<string>> {
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
export async function diff(args: string[]): Promise<number> {
|
|
29
|
-
const parsed = parse(args, diffUsage, jsonFlag)
|
|
30
|
+
const parsed = parse(args, diffUsage, jsonFlag, { exits: DIFF_EXITS })
|
|
30
31
|
|
|
31
32
|
const [a, b] = parsed.positionals
|
|
32
33
|
|
|
@@ -57,7 +58,7 @@ export async function diff(args: string[]): Promise<number> {
|
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
export async function missing(args: string[]): Promise<number> {
|
|
60
|
-
const parsed = parse(args, missingUsage, { against: { type: 'string', description: 'the reference file (.env.example by default)' }, ...jsonFlag })
|
|
61
|
+
const parsed = parse(args, missingUsage, { against: { type: 'string', description: 'the reference file (.env.example by default)' }, ...jsonFlag }, { exits: MISSING_EXITS })
|
|
61
62
|
|
|
62
63
|
const [file = '.env'] = parsed.positionals
|
|
63
64
|
const against = parsed.values.against ?? '.env.example'
|
package/src/commands/edit.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import { badUsage, fail, parse, readInput } from '@kaynooo/cli-kit'
|
|
2
3
|
import { entries, lastLine, removeKey, setLine } from '../dotenv'
|
|
3
4
|
import { loadKnown, readEnv, withLocks, writeEnv } from '../files'
|
|
@@ -7,29 +8,31 @@ const DEFAULT_FILE = '.env'
|
|
|
7
8
|
const TRAILING_NEWLINE = /\r?\n$/
|
|
8
9
|
const KEY_NAME = /^[A-Z_][\w.-]*$/i
|
|
9
10
|
|
|
10
|
-
const
|
|
11
|
+
const QUIET = { quiet: { type: 'boolean', short: 'q', description: 'print nothing on success' } } as const
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
const setUsage = `ev set KEY [file] [--quiet]
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
Sets KEY in the file (.env by default, created when missing), replacing its line in place or appending one. The value is read from stdin, or from -F <file>, so it never appears in a command line; --empty sets it to an empty value.`
|
|
15
16
|
|
|
16
|
-
const
|
|
17
|
+
const SET_EXITS: ExitRow[] = [[0, 'it wrote the key'], [1, 'it could not'], [2, 'a usage error']]
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
const unsetUsage = `ev unset KEY [file] [--quiet]
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
Removes every line setting KEY from the file (.env by default), leaving comments and other lines as they are.`
|
|
21
22
|
|
|
22
|
-
const
|
|
23
|
+
const UNSET_EXITS: ExitRow[] = [[0, 'it removed the key'], [1, 'the file does not set it'], [2, 'a usage error']]
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
const mvUsage = `ev mv KEY --from <file> --to <file> [--quiet]
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
Moves KEY's line from one file to another, as written. Refuses to replace a key the target already sets unless --force.`
|
|
27
28
|
|
|
28
|
-
const
|
|
29
|
+
const MV_EXITS: ExitRow[] = [[0, 'it moved the key'], [1, 'the source lacks it or the target already sets it'], [2, 'a usage error']]
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
const cpUsage = `ev cp KEY --from <file> --to <file> [--as NEWKEY] [--quiet]
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
Copies KEY's value from one file to another without printing it, replacing the key's line in the target in place or appending one, and prints the masked result as ev set does. --as stores the copy under another key.`
|
|
34
|
+
|
|
35
|
+
const CP_EXITS: ExitRow[] = [[0, 'it copied the key'], [1, 'the source lacks it'], [2, 'a usage error']]
|
|
33
36
|
|
|
34
37
|
async function readValue(from: string | undefined): Promise<string> {
|
|
35
38
|
const input = await readInput(from ?? '-', { noStdin: 'pipe the value on stdin or give -F <file>, it is never taken from the command line' })
|
|
@@ -54,7 +57,8 @@ export async function set(args: string[]): Promise<number> {
|
|
|
54
57
|
const parsed = parse(args, setUsage, {
|
|
55
58
|
file: { type: 'string', short: 'F', description: 'read the value from this file instead of stdin' },
|
|
56
59
|
empty: { type: 'boolean', description: 'set an empty value, reading nothing' },
|
|
57
|
-
|
|
60
|
+
...QUIET,
|
|
61
|
+
}, { exits: SET_EXITS })
|
|
58
62
|
|
|
59
63
|
const [key, target = DEFAULT_FILE] = parsed.positionals
|
|
60
64
|
|
|
@@ -77,13 +81,14 @@ export async function set(args: string[]): Promise<number> {
|
|
|
77
81
|
return current
|
|
78
82
|
})
|
|
79
83
|
|
|
80
|
-
|
|
84
|
+
if (parsed.values.quiet !== true)
|
|
85
|
+
console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [file])).replaceAll('\n', '\\n')}`)
|
|
81
86
|
|
|
82
87
|
return 0
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
export async function unset(args: string[]): Promise<number> {
|
|
86
|
-
const parsed = parse(args, unsetUsage, {})
|
|
91
|
+
const parsed = parse(args, unsetUsage, QUIET, { exits: UNSET_EXITS })
|
|
87
92
|
|
|
88
93
|
const [key, target = DEFAULT_FILE] = parsed.positionals
|
|
89
94
|
|
|
@@ -99,7 +104,8 @@ export async function unset(args: string[]): Promise<number> {
|
|
|
99
104
|
await writeEnv(target, file)
|
|
100
105
|
})
|
|
101
106
|
|
|
102
|
-
|
|
107
|
+
if (parsed.values.quiet !== true)
|
|
108
|
+
console.log(`${target}: removed ${key}`)
|
|
103
109
|
|
|
104
110
|
return 0
|
|
105
111
|
}
|
|
@@ -139,7 +145,8 @@ export async function mv(args: string[]): Promise<number> {
|
|
|
139
145
|
from: { type: 'string', description: 'the file to take KEY from' },
|
|
140
146
|
to: { type: 'string', description: 'the file to put KEY in, created when missing' },
|
|
141
147
|
force: { type: 'boolean', description: 'replace KEY when the target already sets it' },
|
|
142
|
-
|
|
148
|
+
...QUIET,
|
|
149
|
+
}, { exits: MV_EXITS })
|
|
143
150
|
|
|
144
151
|
const [key] = parsed.positionals
|
|
145
152
|
const { from, to, force } = parsed.values
|
|
@@ -154,7 +161,8 @@ export async function mv(args: string[]): Promise<number> {
|
|
|
154
161
|
await writeEnv(from, source)
|
|
155
162
|
})
|
|
156
163
|
|
|
157
|
-
|
|
164
|
+
if (parsed.values.quiet !== true)
|
|
165
|
+
console.log(`${key}: ${from} -> ${to}`)
|
|
158
166
|
|
|
159
167
|
return 0
|
|
160
168
|
}
|
|
@@ -164,7 +172,8 @@ export async function cp(args: string[]): Promise<number> {
|
|
|
164
172
|
from: { type: 'string', description: 'the file to take KEY from' },
|
|
165
173
|
to: { type: 'string', description: 'the file to put the copy in, created when missing' },
|
|
166
174
|
as: { type: 'string', description: 'store the copy under this key instead' },
|
|
167
|
-
|
|
175
|
+
...QUIET,
|
|
176
|
+
}, { exits: CP_EXITS })
|
|
168
177
|
|
|
169
178
|
const [key] = parsed.positionals
|
|
170
179
|
const { from, to, as } = parsed.values
|
|
@@ -173,9 +182,9 @@ export async function cp(args: string[]): Promise<number> {
|
|
|
173
182
|
fail(`needs KEY, --from and --to\n\n${cpUsage}`)
|
|
174
183
|
|
|
175
184
|
const { source, target, value, name } = await withLocks([from, to], async () => transfer(key, from, to, as, true))
|
|
176
|
-
const known = await loadKnown(process.cwd(), [source, target])
|
|
177
185
|
|
|
178
|
-
|
|
186
|
+
if (parsed.values.quiet !== true)
|
|
187
|
+
console.log(`${to}: ${name}=${maskValue(name, value, await loadKnown(process.cwd(), [source, target])).replaceAll('\n', '\\n')}`)
|
|
179
188
|
|
|
180
189
|
return 0
|
|
181
190
|
}
|