@kaynooo/cli-edit 0.9.1 → 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 +67 -39
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -2,9 +2,9 @@
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";
@@ -675,21 +675,45 @@ function nearestOption(message, options, allowNegative) {
675
675
  const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
676
676
  return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
677
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
+ }
678
702
  // ../kit/src/progress.ts
679
703
  var ESC = String.fromCharCode(27);
680
704
  var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
681
705
  var RESET_STYLE = `${ESC}[0m`;
682
706
  var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
683
707
  // ../kit/src/cli.ts
684
- import { readFileSync as readFileSync2, realpathSync } from "fs";
708
+ import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
685
709
  import { dirname as dirname3, join as join5 } from "path";
686
- import process12 from "process";
710
+ import process13 from "process";
687
711
 
688
712
  // ../kit/src/usage.ts
689
713
  import { appendFileSync, mkdirSync } from "fs";
690
714
  import { homedir as homedir3 } from "os";
691
715
  import { dirname as dirname2, isAbsolute, join as join4 } from "path";
692
- import process11 from "process";
716
+ import process12 from "process";
693
717
 
694
718
  // ../kit/src/logs.ts
695
719
  var DAY_MS = 24 * 60 * 60 * 1000;
@@ -703,14 +727,14 @@ var usageError;
703
727
  function noteUsageError(message) {
704
728
  usageError = message;
705
729
  }
706
- function stateHome(env = process11.env) {
730
+ function stateHome(env = process12.env) {
707
731
  const dir = env.XDG_STATE_HOME;
708
732
  return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
709
733
  }
710
- function usagePath(env = process11.env) {
734
+ function usagePath(env = process12.env) {
711
735
  return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
712
736
  }
713
- function usageRecorded(env = process11.env) {
737
+ function usageRecorded(env = process12.env) {
714
738
  const off = env.KAYNOOO_CLI_NO_USAGE;
715
739
  return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
716
740
  }
@@ -724,7 +748,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
724
748
  usageError = undefined;
725
749
  return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
726
750
  }
727
- function recordUsage(entry, env = process11.env) {
751
+ function recordUsage(entry, env = process12.env) {
728
752
  if (!usageRecorded(env))
729
753
  return;
730
754
  try {
@@ -742,8 +766,8 @@ var LEVEL_OPTIONS = {
742
766
  };
743
767
  var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
744
768
  function printError(prefix, message, hint, cause) {
745
- const colors = paint(process12.stderr);
746
- const styled = takesControlCodes(process12.stderr);
769
+ const colors = paint(process13.stderr);
770
+ const styled = takesControlCodes(process13.stderr);
747
771
  const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
748
772
  const under = (label, text, tint) => text === undefined ? "" : `
749
773
  ${styled ? tint(label) : `${label}:`} ${text}`;
@@ -787,24 +811,24 @@ function candidateLine(candidate) {
787
811
  }
788
812
  function positionalBeingTyped(before, { options, positionals }) {
789
813
  const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
790
- let index = 0;
814
+ const typed = [];
791
815
  let at = 0;
792
816
  while (at < before.length) {
793
817
  const word = before[at];
794
818
  if (word === "--") {
795
- index += before.length - at - 1;
819
+ typed.push(...before.slice(at + 1));
796
820
  break;
797
821
  }
798
822
  if (!word.startsWith("-"))
799
- index++;
823
+ typed.push(word);
800
824
  else if (takesValue(word))
801
825
  at++;
802
826
  at++;
803
827
  }
804
828
  const last = positionals.at(-1);
805
- if (index < positionals.length)
806
- return positionals[index];
807
- 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 };
808
832
  }
809
833
  async function leafCandidates(run, words) {
810
834
  const completing = await completingOf(run);
@@ -812,12 +836,12 @@ async function leafCandidates(run, words) {
812
836
  return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
813
837
  const flag = words.at(-2);
814
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);
815
840
  if (option?.type === "string") {
816
- const values = option.choices === undefined ? await option.candidates?.() : option.choices;
841
+ const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
817
842
  return values === undefined ? [FILES] : values.map(candidateLine);
818
843
  }
819
- const name = positionalBeingTyped(words.slice(0, -1), completing);
820
- const values = name === undefined ? undefined : await completing.candidates[name]?.();
844
+ const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
821
845
  return values === undefined ? [FILES] : values.map(candidateLine);
822
846
  }
823
847
  async function printCandidates(found) {
@@ -826,9 +850,13 @@ async function printCandidates(found) {
826
850
  return 0;
827
851
  }
828
852
  function packageVersion(entry = Bun.main) {
829
- 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)) {
830
858
  try {
831
- const { version } = JSON.parse(readFileSync2(join5(dir, "package.json"), "utf8"));
859
+ const { version } = JSON.parse(readFileSync3(join5(dir, "package.json"), "utf8"));
832
860
  if (typeof version === "string")
833
861
  return version;
834
862
  } catch {}
@@ -836,7 +864,7 @@ function packageVersion(entry = Bun.main) {
836
864
  return "0.0.0";
837
865
  }
838
866
  }
839
- async function runMain({ name, version, run, redact, usage }, argv = process12.argv.slice(2)) {
867
+ async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
840
868
  if (argv[0] === COMPLETE)
841
869
  return printCandidates(leafCandidates(run, argv.slice(1)));
842
870
  if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
@@ -1107,16 +1135,16 @@ function writable(path) {
1107
1135
  var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
1108
1136
  function readUtf8(path) {
1109
1137
  try {
1110
- return UTF8.decode(readFileSync3(path));
1138
+ return UTF8.decode(readFileSync4(path));
1111
1139
  } catch {
1112
1140
  return;
1113
1141
  }
1114
1142
  }
1115
1143
  function canonical(path) {
1116
1144
  try {
1117
- return realpathSync2(path);
1145
+ return realpathSync3(path);
1118
1146
  } catch {
1119
- return resolve3(path);
1147
+ return resolve4(path);
1120
1148
  }
1121
1149
  }
1122
1150
  function dedupe(paths) {
@@ -1129,8 +1157,8 @@ function dedupe(paths) {
1129
1157
  return [...byFile.values()];
1130
1158
  }
1131
1159
  function writeAtomic(path, text) {
1132
- const target = realpathSync2(path);
1133
- const temp = `${target}.sub-${process14.pid}`;
1160
+ const target = realpathSync3(path);
1161
+ const temp = `${target}.sub-${process15.pid}`;
1134
1162
  try {
1135
1163
  writeFileSync2(temp, text);
1136
1164
  chmodSync(temp, (statSync2(target, { throwIfNoEntry: false })?.mode ?? 420) & 4095);
@@ -1149,23 +1177,23 @@ async function blocksOnStdin(files) {
1149
1177
  const hint = `${strings}give --from and --to, or pipe SEARCH/REPLACE blocks on stdin from a heredoc
1150
1178
 
1151
1179
  ${USAGE}`;
1152
- if (process14.stdin.isTTY)
1180
+ if (process15.stdin.isTTY)
1153
1181
  fail(hint);
1154
1182
  return new Promise((resolve) => {
1155
1183
  const chunks = [];
1156
1184
  const timer = setTimeout(() => {
1157
1185
  console.error(`error: sub: ${hint}`);
1158
- process14.exit(2);
1186
+ process15.exit(2);
1159
1187
  }, STDIN_SILENCE_MS);
1160
- process14.stdin.on("data", (chunk) => {
1188
+ process15.stdin.on("data", (chunk) => {
1161
1189
  clearTimeout(timer);
1162
1190
  chunks.push(chunk);
1163
1191
  });
1164
- process14.stdin.on("end", () => {
1192
+ process15.stdin.on("end", () => {
1165
1193
  clearTimeout(timer);
1166
1194
  if (chunks.length === 0 && extra.length > 0) {
1167
1195
  console.error(`error: sub: ${hint}`);
1168
- process14.exit(2);
1196
+ process15.exit(2);
1169
1197
  }
1170
1198
  resolve(Buffer.concat(chunks).toString("utf8"));
1171
1199
  });
@@ -1248,7 +1276,7 @@ async function main(argv) {
1248
1276
  if (json)
1249
1277
  console.error(JSON.stringify({ error: "nothing written", failures: problems }));
1250
1278
  else
1251
- console.error(`sub: nothing written
1279
+ console.error(`${role("problem", process15.stderr)("sub: nothing written")}
1252
1280
  ${problems.map(({ path, problem }) => `${path}: ${problem}`).join(`
1253
1281
  `)}`);
1254
1282
  return 1;
@@ -1257,7 +1285,7 @@ ${problems.map(({ path, problem }) => `${path}: ${problem}`).join(`
1257
1285
  for (const change of changes)
1258
1286
  change.diff = await diffTexts(change.path, change.before, change.after);
1259
1287
  } catch (error) {
1260
- console.error(`sub: nothing written
1288
+ console.error(`${role("problem", process15.stderr)("sub: nothing written")}
1261
1289
  ${error instanceof Error ? error.message : String(error)}`);
1262
1290
  return 1;
1263
1291
  }
@@ -1272,9 +1300,9 @@ ${error instanceof Error ? error.message : String(error)}`);
1272
1300
  continue;
1273
1301
  }
1274
1302
  if (values.quiet !== true)
1275
- 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}`)}`);
1276
1304
  if (values.diff === true && diff.unified !== "")
1277
- process14.stdout.write(diff.unified);
1305
+ process15.stdout.write(diff.unified);
1278
1306
  }
1279
1307
  if (json)
1280
1308
  printJson({ dryRun: values["dry-run"] === true, files: done });
@@ -1282,4 +1310,4 @@ ${error instanceof Error ? error.message : String(error)}`);
1282
1310
  console.log("dry run: nothing written");
1283
1311
  return 0;
1284
1312
  }
1285
- 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.1",
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.36.0"
42
+ "@kaynooo/cli-kit": "0.38.2"
43
43
  }
44
44
  }