@kaynooo/cli-edit 0.9.0 → 0.10.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.
Files changed (2) hide show
  1. package/dist/cli.js +76 -46
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -2,15 +2,16 @@
2
2
  // @bun
3
3
 
4
4
  // src/cli.ts
5
- import { accessSync, chmodSync, constants, existsSync as existsSync3, readFileSync as readFileSync3, realpathSync as realpathSync2, renameSync, rmSync as rmSync2, statSync as statSync2, writeFileSync as writeFileSync2 } from "fs";
6
- import { normalize, resolve as resolve3 } from "path";
7
- import process14 from "process";
5
+ import { accessSync, chmodSync, constants, existsSync as existsSync3, readFileSync as readFileSync4, realpathSync as realpathSync3, renameSync, rmSync as rmSync2, statSync as statSync2, writeFileSync as writeFileSync2 } from "fs";
6
+ import { normalize, resolve as resolve4 } from "path";
7
+ import process15 from "process";
8
8
  // ../kit/src/args.ts
9
9
  import process10 from "process";
10
10
  import { parseArgs } from "util";
11
11
 
12
12
  // ../kit/src/format.ts
13
13
  import process7 from "process";
14
+
14
15
  // ../../node_modules/.bun/@kaynooo+utils@3.0.0+4c7ddab9986847c5/node_modules/@kaynooo/utils/dist/chunk-rxztfxgv.js
15
16
  var ct = /[aeiou]y$/;
16
17
  var N = new Map;
@@ -34,6 +35,7 @@ function go(t, e, { zero: r = "plural", ...n } = {}) {
34
35
  }
35
36
  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));
36
37
  var z = new Map;
38
+
37
39
  // ../kit/src/mode.ts
38
40
  import process2 from "process";
39
41
  var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
@@ -79,7 +81,7 @@ function C(r, e = globalThis.process?.env ?? {}) {
79
81
  return false;
80
82
  return r?.isTTY === true;
81
83
  }
82
- function f2(r, e = []) {
84
+ function f(r, e = []) {
83
85
  let l = "", n = "";
84
86
  for (let t = 0, i = e.length;t < i; t++)
85
87
  l += `\x1B[${e[t][0]}m`, n = `\x1B[${e[t][1]}m${n}`;
@@ -89,7 +91,7 @@ function f2(r, e = []) {
89
91
  if (Array.isArray(a)) {
90
92
  let s = g.get(i);
91
93
  if (s === undefined)
92
- s = f2(r, [...e, a]), g.set(i, s);
94
+ s = f(r, [...e, a]), g.set(i, s);
93
95
  return s;
94
96
  }
95
97
  if (typeof a === "function")
@@ -98,17 +100,17 @@ function f2(r, e = []) {
98
100
  if (u === undefined) {
99
101
  if (g.size >= 64)
100
102
  g.clear();
101
- u = f2(r, [...e, a(...s)]), g.set(y, u);
103
+ u = f(r, [...e, a(...s)]), g.set(y, u);
102
104
  }
103
105
  return u;
104
106
  };
105
107
  throw Error(`Unknown style: ${String(i)}`);
106
108
  } });
107
109
  }
108
- function Fe2(r = {}) {
109
- return f2(r);
110
+ function Fe(r = {}) {
111
+ return f(r);
110
112
  }
111
- var o2 = Fe2();
113
+ var o = Fe();
112
114
 
113
115
  // ../kit/src/theme.ts
114
116
  import { existsSync as existsSync2, readFileSync, statSync } from "fs";
@@ -329,7 +331,7 @@ function takesControlCodes(stream, env = process6.env) {
329
331
  return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
330
332
  }
331
333
  function paint(stream, env = process6.env) {
332
- return Fe2({ stream: { isTTY: takesControlCodes(stream, env) }, env });
334
+ return Fe({ stream: { isTTY: takesControlCodes(stream, env) }, env });
333
335
  }
334
336
  function role(name, stream, env = process6.env) {
335
337
  const colors = paint(stream, env);
@@ -673,21 +675,45 @@ function nearestOption(message, options, allowNegative) {
673
675
  const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
674
676
  return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
675
677
  }
678
+ // ../kit/src/argv.ts
679
+ import { readFileSync as readFileSync2, realpathSync } from "fs";
680
+ import { resolve as resolve3 } from "path";
681
+ import process11 from "process";
682
+ function commandLine() {
683
+ try {
684
+ return readFileSync2("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
685
+ } catch {
686
+ return;
687
+ }
688
+ }
689
+ function realPath(word) {
690
+ try {
691
+ return realpathSync(resolve3(word));
692
+ } catch {
693
+ return;
694
+ }
695
+ }
696
+ function scriptArgs(argv = process11.argv.slice(2), cmdline = commandLine(), script = process11.argv[1]) {
697
+ const target = script === undefined ? undefined : realPath(script) ?? script;
698
+ const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve3(word) === script || realPath(word) === target));
699
+ const after = at === -1 ? [] : cmdline.slice(at + 1);
700
+ return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
701
+ }
676
702
  // ../kit/src/progress.ts
677
703
  var ESC = String.fromCharCode(27);
678
704
  var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
679
705
  var RESET_STYLE = `${ESC}[0m`;
680
706
  var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
681
707
  // ../kit/src/cli.ts
682
- import { readFileSync as readFileSync2, realpathSync } from "fs";
708
+ import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
683
709
  import { dirname as dirname3, join as join5 } from "path";
684
- import process12 from "process";
710
+ import process13 from "process";
685
711
 
686
712
  // ../kit/src/usage.ts
687
713
  import { appendFileSync, mkdirSync } from "fs";
688
714
  import { homedir as homedir3 } from "os";
689
715
  import { dirname as dirname2, isAbsolute, join as join4 } from "path";
690
- import process11 from "process";
716
+ import process12 from "process";
691
717
 
692
718
  // ../kit/src/logs.ts
693
719
  var DAY_MS = 24 * 60 * 60 * 1000;
@@ -701,14 +727,14 @@ var usageError;
701
727
  function noteUsageError(message) {
702
728
  usageError = message;
703
729
  }
704
- function stateHome(env = process11.env) {
730
+ function stateHome(env = process12.env) {
705
731
  const dir = env.XDG_STATE_HOME;
706
732
  return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
707
733
  }
708
- function usagePath(env = process11.env) {
734
+ function usagePath(env = process12.env) {
709
735
  return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
710
736
  }
711
- function usageRecorded(env = process11.env) {
737
+ function usageRecorded(env = process12.env) {
712
738
  const off = env.KAYNOOO_CLI_NO_USAGE;
713
739
  return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
714
740
  }
@@ -722,7 +748,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
722
748
  usageError = undefined;
723
749
  return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
724
750
  }
725
- function recordUsage(entry, env = process11.env) {
751
+ function recordUsage(entry, env = process12.env) {
726
752
  if (!usageRecorded(env))
727
753
  return;
728
754
  try {
@@ -740,8 +766,8 @@ var LEVEL_OPTIONS = {
740
766
  };
741
767
  var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
742
768
  function printError(prefix, message, hint, cause) {
743
- const colors = paint(process12.stderr);
744
- const styled = takesControlCodes(process12.stderr);
769
+ const colors = paint(process13.stderr);
770
+ const styled = takesControlCodes(process13.stderr);
745
771
  const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
746
772
  const under = (label, text, tint) => text === undefined ? "" : `
747
773
  ${styled ? tint(label) : `${label}:`} ${text}`;
@@ -785,24 +811,24 @@ function candidateLine(candidate) {
785
811
  }
786
812
  function positionalBeingTyped(before, { options, positionals }) {
787
813
  const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
788
- let index = 0;
814
+ const typed = [];
789
815
  let at = 0;
790
816
  while (at < before.length) {
791
817
  const word = before[at];
792
818
  if (word === "--") {
793
- index += before.length - at - 1;
819
+ typed.push(...before.slice(at + 1));
794
820
  break;
795
821
  }
796
822
  if (!word.startsWith("-"))
797
- index++;
823
+ typed.push(word);
798
824
  else if (takesValue(word))
799
825
  at++;
800
826
  at++;
801
827
  }
802
828
  const last = positionals.at(-1);
803
- if (index < positionals.length)
804
- return positionals[index];
805
- return last?.replace("]", "").endsWith("...") === true ? last : undefined;
829
+ if (typed.length < positionals.length)
830
+ return { name: positionals[typed.length], typed };
831
+ return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
806
832
  }
807
833
  async function leafCandidates(run, words) {
808
834
  const completing = await completingOf(run);
@@ -810,12 +836,12 @@ async function leafCandidates(run, words) {
810
836
  return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
811
837
  const flag = words.at(-2);
812
838
  const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
839
+ const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
813
840
  if (option?.type === "string") {
814
- const values = option.choices === undefined ? await option.candidates?.() : option.choices;
841
+ const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
815
842
  return values === undefined ? [FILES] : values.map(candidateLine);
816
843
  }
817
- const name = positionalBeingTyped(words.slice(0, -1), completing);
818
- const values = name === undefined ? undefined : await completing.candidates[name]?.();
844
+ const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
819
845
  return values === undefined ? [FILES] : values.map(candidateLine);
820
846
  }
821
847
  async function printCandidates(found) {
@@ -824,9 +850,13 @@ async function printCandidates(found) {
824
850
  return 0;
825
851
  }
826
852
  function packageVersion(entry = Bun.main) {
827
- for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
853
+ let resolved = entry;
854
+ try {
855
+ resolved = realpathSync2(entry);
856
+ } catch {}
857
+ for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
828
858
  try {
829
- const { version } = JSON.parse(readFileSync2(join5(dir, "package.json"), "utf8"));
859
+ const { version } = JSON.parse(readFileSync3(join5(dir, "package.json"), "utf8"));
830
860
  if (typeof version === "string")
831
861
  return version;
832
862
  } catch {}
@@ -834,7 +864,7 @@ function packageVersion(entry = Bun.main) {
834
864
  return "0.0.0";
835
865
  }
836
866
  }
837
- async function runMain({ name, version, run, redact, usage }, argv = process12.argv.slice(2)) {
867
+ async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
838
868
  if (argv[0] === COMPLETE)
839
869
  return printCandidates(leafCandidates(run, argv.slice(1)));
840
870
  if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
@@ -1105,16 +1135,16 @@ function writable(path) {
1105
1135
  var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
1106
1136
  function readUtf8(path) {
1107
1137
  try {
1108
- return UTF8.decode(readFileSync3(path));
1138
+ return UTF8.decode(readFileSync4(path));
1109
1139
  } catch {
1110
1140
  return;
1111
1141
  }
1112
1142
  }
1113
1143
  function canonical(path) {
1114
1144
  try {
1115
- return realpathSync2(path);
1145
+ return realpathSync3(path);
1116
1146
  } catch {
1117
- return resolve3(path);
1147
+ return resolve4(path);
1118
1148
  }
1119
1149
  }
1120
1150
  function dedupe(paths) {
@@ -1127,8 +1157,8 @@ function dedupe(paths) {
1127
1157
  return [...byFile.values()];
1128
1158
  }
1129
1159
  function writeAtomic(path, text) {
1130
- const target = realpathSync2(path);
1131
- const temp = `${target}.sub-${process14.pid}`;
1160
+ const target = realpathSync3(path);
1161
+ const temp = `${target}.sub-${process15.pid}`;
1132
1162
  try {
1133
1163
  writeFileSync2(temp, text);
1134
1164
  chmodSync(temp, (statSync2(target, { throwIfNoEntry: false })?.mode ?? 420) & 4095);
@@ -1147,23 +1177,23 @@ async function blocksOnStdin(files) {
1147
1177
  const hint = `${strings}give --from and --to, or pipe SEARCH/REPLACE blocks on stdin from a heredoc
1148
1178
 
1149
1179
  ${USAGE}`;
1150
- if (process14.stdin.isTTY)
1180
+ if (process15.stdin.isTTY)
1151
1181
  fail(hint);
1152
1182
  return new Promise((resolve) => {
1153
1183
  const chunks = [];
1154
1184
  const timer = setTimeout(() => {
1155
1185
  console.error(`error: sub: ${hint}`);
1156
- process14.exit(2);
1186
+ process15.exit(2);
1157
1187
  }, STDIN_SILENCE_MS);
1158
- process14.stdin.on("data", (chunk) => {
1188
+ process15.stdin.on("data", (chunk) => {
1159
1189
  clearTimeout(timer);
1160
1190
  chunks.push(chunk);
1161
1191
  });
1162
- process14.stdin.on("end", () => {
1192
+ process15.stdin.on("end", () => {
1163
1193
  clearTimeout(timer);
1164
1194
  if (chunks.length === 0 && extra.length > 0) {
1165
1195
  console.error(`error: sub: ${hint}`);
1166
- process14.exit(2);
1196
+ process15.exit(2);
1167
1197
  }
1168
1198
  resolve(Buffer.concat(chunks).toString("utf8"));
1169
1199
  });
@@ -1246,7 +1276,7 @@ async function main(argv) {
1246
1276
  if (json)
1247
1277
  console.error(JSON.stringify({ error: "nothing written", failures: problems }));
1248
1278
  else
1249
- console.error(`sub: nothing written
1279
+ console.error(`${role("problem", process15.stderr)("sub: nothing written")}
1250
1280
  ${problems.map(({ path, problem }) => `${path}: ${problem}`).join(`
1251
1281
  `)}`);
1252
1282
  return 1;
@@ -1255,7 +1285,7 @@ ${problems.map(({ path, problem }) => `${path}: ${problem}`).join(`
1255
1285
  for (const change of changes)
1256
1286
  change.diff = await diffTexts(change.path, change.before, change.after);
1257
1287
  } catch (error) {
1258
- console.error(`sub: nothing written
1288
+ console.error(`${role("problem", process15.stderr)("sub: nothing written")}
1259
1289
  ${error instanceof Error ? error.message : String(error)}`);
1260
1290
  return 1;
1261
1291
  }
@@ -1270,9 +1300,9 @@ ${error instanceof Error ? error.message : String(error)}`);
1270
1300
  continue;
1271
1301
  }
1272
1302
  if (values.quiet !== true)
1273
- console.log(`${role("path", process14.stdout)(path)}: ${go(lines.length, "replacement")} on ${K("line", lines.length, { zero: "plural" })} ${listLines(lines)}, ${role(diff.added === 0 ? "label" : "ok", process14.stdout)(`+${diff.added}`)} ${role(diff.removed === 0 ? "label" : "problem", process14.stdout)(`-${diff.removed}`)}`);
1303
+ console.log(`${role("path", process15.stdout)(path)}: ${go(lines.length, "replacement")} on ${K("line", lines.length, { zero: "plural" })} ${listLines(lines)}, ${role(diff.added === 0 ? "label" : "ok", process15.stdout)(`+${diff.added}`)} ${role(diff.removed === 0 ? "label" : "problem", process15.stdout)(`-${diff.removed}`)}`);
1274
1304
  if (values.diff === true && diff.unified !== "")
1275
- process14.stdout.write(diff.unified);
1305
+ process15.stdout.write(diff.unified);
1276
1306
  }
1277
1307
  if (json)
1278
1308
  printJson({ dryRun: values["dry-run"] === true, files: done });
@@ -1280,4 +1310,4 @@ ${error instanceof Error ? error.message : String(error)}`);
1280
1310
  console.log("dry run: nothing written");
1281
1311
  return 0;
1282
1312
  }
1283
- process14.exitCode = await runMain({ name: "sub", run: main });
1313
+ process15.exitCode = await runMain({ name: "sub", run: main });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-edit",
3
3
  "type": "module",
4
- "version": "0.9.0",
4
+ "version": "0.10.0",
5
5
  "description": "sub: text replaced in files for Claude Code, written only when every replacement matches as often as expected.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -39,6 +39,6 @@
39
39
  "@kaynooo/utils": "3.0.0"
40
40
  },
41
41
  "devDependencies": {
42
- "@kaynooo/cli-kit": "0.35.0"
42
+ "@kaynooo/cli-kit": "0.38.2"
43
43
  }
44
44
  }