costclaw 0.15.0 → 0.15.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.
Files changed (2) hide show
  1. package/dist/index.js +31 -40
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -5980,6 +5980,18 @@ async function pathExists(p) {
5980
5980
  return false;
5981
5981
  }
5982
5982
  }
5983
+ function stamp(now) {
5984
+ const p = (n, w = 2) => String(n).padStart(w, "0");
5985
+ return `${now.getFullYear()}${p(now.getMonth() + 1)}${p(now.getDate())}-${p(now.getHours())}${p(now.getMinutes())}${p(now.getSeconds())}`;
5986
+ }
5987
+ async function backupFile(target, now = /* @__PURE__ */ new Date()) {
5988
+ if (!await pathExists(target)) return null;
5989
+ const base = `${target}.bak-${stamp(now)}`;
5990
+ let path = base;
5991
+ for (let n = 2; await pathExists(path); n++) path = `${base}-${n}`;
5992
+ await copyFile(target, path);
5993
+ return path;
5994
+ }
5983
5995
  async function readIncludeSections(artifactsDir) {
5984
5996
  const path = join7(artifactsDir, INCLUDE_SECTIONS_ARTIFACT);
5985
5997
  if (!await pathExists(path)) return null;
@@ -6076,7 +6088,7 @@ async function runInclude(opts) {
6076
6088
  const settingsSrc = join7(artifactsDir, "settings.suggested.json");
6077
6089
  const suggestedJson = await readFile6(settingsSrc, "utf8");
6078
6090
  const target = opts.applySettingsPath ?? join7(projectDir, ".claude", "settings.json");
6079
- const settingsLines = await applyDerivedSettings(target, suggestedJson, opts.force ?? false);
6091
+ const settingsLines = await applyDerivedSettings(target, suggestedJson);
6080
6092
  lines.push(...settingsLines);
6081
6093
  wrote.push(target);
6082
6094
  }
@@ -6114,7 +6126,7 @@ async function runRemove(opts) {
6114
6126
  return { lines, wrote: [], removed };
6115
6127
  }
6116
6128
  async function runReplace(opts) {
6117
- const { projectDir, artifactsDir, force } = opts;
6129
+ const { projectDir, artifactsDir } = opts;
6118
6130
  if (!await pathExists(join7(projectDir, ".git"))) {
6119
6131
  throw new Error(
6120
6132
  "apply --replace needs a git repository so the change can be undone with git checkout -- CLAUDE.md. Use the default include mode instead."
@@ -6126,25 +6138,19 @@ async function runReplace(opts) {
6126
6138
  }
6127
6139
  const optimized = await readFile6(optimizedSrc, "utf8");
6128
6140
  const claudeMdPath = join7(projectDir, "CLAUDE.md");
6129
- const backup = `${claudeMdPath}.bak`;
6130
- if (await pathExists(backup) && !force) {
6131
- throw new Error(
6132
- `A backup already exists at ${backup}. Refusing to overwrite it. Pass --force to replace the existing backup.`
6133
- );
6134
- }
6135
6141
  const original = await pathExists(claudeMdPath) ? await readFile6(claudeMdPath, "utf8") : "";
6136
- await writeFile4(backup, original, "utf8");
6142
+ const backup = await backupFile(claudeMdPath);
6137
6143
  await writeFile4(claudeMdPath, optimized, "utf8");
6138
6144
  const lines = [
6139
- `Backed up the original to ${backup}`,
6145
+ ...backup ? [`Backed up the original to ${backup}`] : [],
6140
6146
  `Wrote the optimized CLAUDE.md to ${claudeMdPath}`,
6141
6147
  "",
6142
6148
  "Diff (old vs new):",
6143
6149
  lineDiff(original, optimized),
6144
6150
  "",
6145
- "Undo: git checkout -- CLAUDE.md (or restore CLAUDE.md.bak)"
6151
+ backup ? `Undo: git checkout -- CLAUDE.md (or restore ${backup})` : "Undo: git checkout -- CLAUDE.md"
6146
6152
  ];
6147
- return { lines, wrote: [claudeMdPath, backup], removed: [] };
6153
+ return { lines, wrote: backup ? [claudeMdPath, backup] : [claudeMdPath], removed: [] };
6148
6154
  }
6149
6155
  async function runApply(opts) {
6150
6156
  if (opts.global) return runGlobal(opts);
@@ -6183,15 +6189,7 @@ async function runGlobal(opts) {
6183
6189
  );
6184
6190
  }
6185
6191
  const original = await pathExists(claudeMdPath) ? await readFile6(claudeMdPath, "utf8") : null;
6186
- const backup = `${claudeMdPath}.bak`;
6187
- if (original !== null) {
6188
- if (await pathExists(backup) && !opts.force) {
6189
- throw new Error(
6190
- `A backup already exists at ${backup}. Refusing to overwrite it. Pass --force to replace the existing backup.`
6191
- );
6192
- }
6193
- await writeFile4(backup, original, "utf8");
6194
- }
6192
+ const backup = await backupFile(claudeMdPath);
6195
6193
  const wrote = [];
6196
6194
  const lines = [];
6197
6195
  await mkdir4(dirname3(claudeMdPath), { recursive: true });
@@ -6232,7 +6230,7 @@ async function runGlobal(opts) {
6232
6230
  "Every line you wrote is untouched. To also drop the sections the review marked remove, rerun with --replace."
6233
6231
  );
6234
6232
  }
6235
- if (original !== null) lines.push(`Backed up the original to ${backup}`);
6233
+ if (backup) lines.push(`Backed up the original to ${backup}`);
6236
6234
  lines.push("Undo: costclaw apply --global --remove");
6237
6235
  return { lines, wrote, removed: [] };
6238
6236
  }
@@ -6274,13 +6272,7 @@ function unionStrings(existing, additions) {
6274
6272
  function isPlainObject(value) {
6275
6273
  return typeof value === "object" && value !== null && !Array.isArray(value);
6276
6274
  }
6277
- async function applyDerivedSettings(target, suggestedJson, force) {
6278
- const backup = `${target}.bak`;
6279
- if (await pathExists(backup) && !force) {
6280
- throw new Error(
6281
- `A backup already exists at ${backup}. Refusing to overwrite it. Pass --force to replace the existing backup.`
6282
- );
6283
- }
6275
+ async function applyDerivedSettings(target, suggestedJson) {
6284
6276
  let existingText = "";
6285
6277
  let existing = {};
6286
6278
  if (await pathExists(target)) {
@@ -6312,11 +6304,11 @@ async function applyDerivedSettings(target, suggestedJson, force) {
6312
6304
  const merged = { ...existing, permissions: mergedPerms };
6313
6305
  const mergedText = JSON.stringify(merged, null, 2) + "\n";
6314
6306
  await mkdir4(dirname3(target), { recursive: true });
6315
- await writeFile4(backup, existingText, "utf8");
6307
+ const backup = await backupFile(target);
6316
6308
  await writeFile4(target, mergedText, "utf8");
6317
6309
  return [
6318
6310
  `Merged the derived permission allowlist into ${target}`,
6319
- `Backed up the previous file to ${backup}`,
6311
+ ...backup ? [`Backed up the previous file to ${backup}`] : [],
6320
6312
  "Only permissions.allow and permissions.deny changed (deduped union, existing entries kept and ordered first). Every other key was left as it was.",
6321
6313
  "",
6322
6314
  "Diff (previous vs merged):",
@@ -8564,7 +8556,7 @@ function parseApplyFlags(args) {
8564
8556
  };
8565
8557
  }
8566
8558
  function cliVersion() {
8567
- return true ? "0.15.0" : "0.0.0-dev";
8559
+ return true ? "0.15.1" : "0.0.0-dev";
8568
8560
  }
8569
8561
  function isHelpRequest(command, args) {
8570
8562
  if (command === void 0) return false;
@@ -8580,8 +8572,8 @@ function helpText() {
8580
8572
  " costclaw diff [<baseline.json> <current.json>] [--json]",
8581
8573
  " costclaw badge [--path <dir>] [--claude-md <file>] [--out <file>]",
8582
8574
  " costclaw optimize [--path <dir>] [--claude-md <file>] [--project <dir>] [--out <dir>] [--limit <n>] [--no-dashboard] [--no-serve] [--force]",
8583
- " costclaw apply [<dir>] [--remove] [--replace] [--apply-settings [file]] [--from <dir>] [--force]",
8584
- " costclaw apply --global [--replace] [--remove] [--from <dir>] [--force]",
8575
+ " costclaw apply [<dir>] [--remove] [--replace] [--apply-settings [file]] [--from <dir>]",
8576
+ " costclaw apply --global [--replace] [--remove] [--from <dir>]",
8585
8577
  " costclaw activate <license-key> [--name <machine-name>]",
8586
8578
  " costclaw deactivate",
8587
8579
  " costclaw --version",
@@ -8640,8 +8632,9 @@ function helpText() {
8640
8632
  "permissions.allow and permissions.deny, backs up the original, and prints a",
8641
8633
  "diff. --from <dir> points apply at a specific artifacts folder instead of",
8642
8634
  "looking one up by project; without it, apply finds artifacts from the last",
8643
- "optimize run, or generates them cold if none exist yet. --force allows",
8644
- "overwriting an existing .bak backup for --replace and --apply-settings.",
8635
+ "optimize run, or generates them cold if none exist yet. Anything apply",
8636
+ "overwrites is backed up first to <file>.bak-YYYYMMDD-HHMMSS, so every run",
8637
+ "keeps its own copy and no earlier backup is ever replaced.",
8645
8638
  "",
8646
8639
  "apply --global does the same for ~/.claude/CLAUDE.md, the one file loaded on",
8647
8640
  "every turn of every project. It writes the sections your audit earned to",
@@ -8762,8 +8755,7 @@ Run: costclaw optimize
8762
8755
  projectDir: flags.projectDir,
8763
8756
  mode: flags.mode,
8764
8757
  artifactsDir: globalDir,
8765
- global: true,
8766
- force: flags.force
8758
+ global: true
8767
8759
  });
8768
8760
  process.stdout.write(outcome2.lines.join("\n") + "\n");
8769
8761
  return;
@@ -8790,8 +8782,7 @@ Run: costclaw optimize
8790
8782
  mode: flags.mode,
8791
8783
  artifactsDir,
8792
8784
  applySettings: flags.applySettings,
8793
- applySettingsPath: flags.applySettingsPath,
8794
- force: flags.force
8785
+ applySettingsPath: flags.applySettingsPath
8795
8786
  });
8796
8787
  process.stdout.write(outcome.lines.join("\n") + "\n");
8797
8788
  if (flags.mode === "include") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "costclaw",
3
- "version": "0.15.0",
3
+ "version": "0.15.1",
4
4
  "description": "Local Claude Code usage estimates, cache analysis, and rule-based setup checks. Your prompts stay on your machine.",
5
5
  "type": "module",
6
6
  "bin": {