claudekit-cli 4.3.1-dev.17 → 4.3.1-dev.19

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.
@@ -0,0 +1,151 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const fs = require("node:fs");
5
+ const os = require("node:os");
6
+ const path = require("node:path");
7
+ const { spawnSync } = require("node:child_process");
8
+
9
+ const MAX_SETTINGS_BYTES = 5 * 1024 * 1024;
10
+ const SAFE_KIT_NAMES = new Set(["engineer", "marketing"]);
11
+
12
+ function getHomeDir() {
13
+ return process.env.CK_TEST_HOME || os.homedir();
14
+ }
15
+
16
+ function readJsonFile(filePath) {
17
+ try {
18
+ if (!filePath || !fs.existsSync(filePath)) return null;
19
+ const stat = fs.statSync(filePath);
20
+ if (!stat.isFile() || stat.size > MAX_SETTINGS_BYTES) return null;
21
+ return JSON.parse(fs.readFileSync(filePath, "utf8").replace(/^\uFEFF/, ""));
22
+ } catch {
23
+ return null;
24
+ }
25
+ }
26
+
27
+ function normalizeCommand(command) {
28
+ if (!command) return "";
29
+ let normalized = String(command).replace(/"/g, "");
30
+ normalized = normalized.replace(/~\//g, "$HOME/");
31
+ normalized = normalized.replace(/\$\{HOME\}/g, "$HOME");
32
+ normalized = normalized.replace(/\$CLAUDE_PROJECT_DIR/g, "$HOME");
33
+ normalized = normalized.replace(/%USERPROFILE%/g, "$HOME");
34
+ normalized = normalized.replace(/%CLAUDE_PROJECT_DIR%/g, "$HOME");
35
+ normalized = normalized.replace(/(^|\s)(?:\.\/)?\.claude\//g, "$1$HOME/.claude/");
36
+ normalized = normalized.replace(/\\/g, "/");
37
+ return normalized.replace(/\s+/g, " ").trim();
38
+ }
39
+
40
+ function collectHookCommands(settings) {
41
+ const commands = new Set();
42
+ for (const entries of Object.values(settings?.hooks || {})) {
43
+ if (!Array.isArray(entries)) continue;
44
+ for (const entry of entries) {
45
+ if (typeof entry?.command === "string") commands.add(normalizeCommand(entry.command));
46
+ if (!Array.isArray(entry?.hooks)) continue;
47
+ for (const hook of entry.hooks) {
48
+ if (typeof hook?.command === "string") commands.add(normalizeCommand(hook.command));
49
+ }
50
+ }
51
+ }
52
+ return commands;
53
+ }
54
+
55
+ function extractCkHookName(command) {
56
+ if (!String(command || "").trim().startsWith("node ")) return null;
57
+ const normalized = String(command).replace(/\\/g, "/");
58
+ const match = normalized.match(/\/hooks\/([^/"'\s]+)\.(?:cjs|mjs|js)(?:["'\s]|$)/);
59
+ return match?.[1] || null;
60
+ }
61
+
62
+ function installedHookCommands(config) {
63
+ return Object.values(config?.kits || {}).flatMap((entry) =>
64
+ Array.isArray(entry?.installedSettings?.hooks) ? entry.installedSettings.hooks : [],
65
+ );
66
+ }
67
+
68
+ function countMissingCkHookRegistrations(claudeDir) {
69
+ const settings = readJsonFile(path.join(claudeDir, "settings.json"));
70
+ const config = readJsonFile(path.join(claudeDir, ".ck.json"));
71
+ if (!settings || !config) return 0;
72
+
73
+ const existingCommands = collectHookCommands(settings);
74
+ const disabledHooks = new Set(
75
+ Object.entries(config.hooks || {})
76
+ .filter(([, enabled]) => enabled === false)
77
+ .map(([name]) => name),
78
+ );
79
+
80
+ let missing = 0;
81
+ for (const command of installedHookCommands(config)) {
82
+ const hookName = extractCkHookName(command);
83
+ if (hookName && disabledHooks.has(hookName)) continue;
84
+ if (!existingCommands.has(normalizeCommand(command))) missing++;
85
+ }
86
+ return missing;
87
+ }
88
+
89
+ function normalizeKitName(name) {
90
+ const normalized = String(name || "").toLowerCase();
91
+ if (normalized.includes("engineer")) return "engineer";
92
+ if (normalized.includes("marketing")) return "marketing";
93
+ return SAFE_KIT_NAMES.has(normalized) ? normalized : null;
94
+ }
95
+
96
+ function getInstalledKit(claudeDir) {
97
+ const metadata = readJsonFile(path.join(claudeDir, "metadata.json"));
98
+ const metadataKit = normalizeKitName(Object.keys(metadata?.kits || {})[0] || metadata?.name);
99
+ if (metadataKit) {
100
+ const kitVersion = metadata?.kits?.[metadataKit]?.version || metadata?.version || "";
101
+ return { kit: metadataKit, isBeta: String(kitVersion).includes("beta") };
102
+ }
103
+
104
+ const config = readJsonFile(path.join(claudeDir, ".ck.json"));
105
+ const configKit = normalizeKitName(Object.keys(config?.kits || {})[0]);
106
+ return configKit ? { kit: configKit, isBeta: false } : null;
107
+ }
108
+
109
+ function collectCandidateClaudeDirs(settingsFiles) {
110
+ const dirs = new Set();
111
+ for (const settingsFile of settingsFiles) dirs.add(path.dirname(settingsFile));
112
+ return [...dirs];
113
+ }
114
+
115
+ function resolveCkEntrypoint() {
116
+ return process.env.CK_POSTINSTALL_CK_BIN || path.join(__dirname, "ck.js");
117
+ }
118
+
119
+ function restoreMissingHookRegistrations(settingsFiles) {
120
+ if (process.env.CK_POSTINSTALL_SKIP_RESTORE_CK_HOOKS === "1") return 0;
121
+
122
+ let restored = 0;
123
+ for (const claudeDir of collectCandidateClaudeDirs(settingsFiles)) {
124
+ const missing = countMissingCkHookRegistrations(claudeDir);
125
+ if (missing <= 0) continue;
126
+
127
+ const installedKit = getInstalledKit(claudeDir);
128
+ if (!installedKit?.kit) continue;
129
+
130
+ const homeDir = getHomeDir();
131
+ const globalClaudeDir = process.env.CLAUDE_CONFIG_DIR || path.join(homeDir, ".claude");
132
+ const isGlobal = path.resolve(claudeDir) === path.resolve(globalClaudeDir);
133
+ const args = [resolveCkEntrypoint(), "init"];
134
+ if (isGlobal) args.push("-g");
135
+ args.push("--kit", installedKit.kit, "--yes", "--restore-ck-hooks", "--install-skills");
136
+ if (installedKit.isBeta) args.push("--beta");
137
+
138
+ const result = spawnSync(process.execPath, args, {
139
+ env: { ...process.env, CK_POSTINSTALL_SKIP_RESTORE_CK_HOOKS: "1" },
140
+ stdio: process.env.CK_POSTINSTALL_DEBUG === "1" ? "inherit" : "ignore",
141
+ });
142
+ if (result.status === 0) restored += missing;
143
+ }
144
+
145
+ return restored;
146
+ }
147
+
148
+ module.exports = {
149
+ countMissingCkHookRegistrations,
150
+ restoreMissingHookRegistrations,
151
+ };
@@ -4,6 +4,10 @@
4
4
  const fs = require("node:fs");
5
5
  const os = require("node:os");
6
6
  const path = require("node:path");
7
+ const {
8
+ countMissingCkHookRegistrations,
9
+ restoreMissingHookRegistrations,
10
+ } = require("./postinstall-hook-restore.cjs");
7
11
 
8
12
  const MAX_SETTINGS_BYTES = 5 * 1024 * 1024;
9
13
 
@@ -124,20 +128,28 @@ function collectCandidateSettingsFiles() {
124
128
 
125
129
  function main() {
126
130
  let pruned = 0;
127
- for (const filePath of collectCandidateSettingsFiles()) {
131
+ const settingsFiles = collectCandidateSettingsFiles();
132
+ for (const filePath of settingsFiles) {
128
133
  pruned += pruneSettingsFile(filePath);
129
134
  }
130
135
 
136
+ const restored = restoreMissingHookRegistrations(settingsFiles);
137
+
131
138
  if (pruned > 0 && process.env.CK_POSTINSTALL_DEBUG === "1") {
132
139
  console.warn(`[claudekit-cli] Pruned ${pruned} legacy hook prompt(s)`);
133
140
  }
141
+ if (restored > 0 && process.env.CK_POSTINSTALL_DEBUG === "1") {
142
+ console.warn(`[claudekit-cli] Restored ${restored} missing hook registration(s)`);
143
+ }
134
144
  }
135
145
 
136
146
  main();
137
147
 
138
148
  module.exports = {
139
149
  collectCandidateSettingsFiles,
150
+ countMissingCkHookRegistrations,
140
151
  isLegacyDescriptiveNamePrompt,
141
152
  pruneHooks,
142
153
  pruneSettingsFile,
154
+ restoreMissingHookRegistrations,
143
155
  };
package/cli-manifest.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
- "version": "4.3.1-dev.17",
3
- "generatedAt": "2026-05-27T03:32:56.469Z",
2
+ "version": "4.3.1-dev.19",
3
+ "generatedAt": "2026-05-27T13:19:15.391Z",
4
4
  "commands": {
5
5
  "agents": {
6
6
  "name": "agents",