@deftai/directive 0.129.5 → 0.129.6

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/bin/deft-hook CHANGED
@@ -1,14 +1,251 @@
1
- #!/bin/sh
2
- # PATH shim for deft-hook (AC3.5). Sets DEFT_LAUNCHER_MODE and runs the single
3
- # dist/bin.js (D4.3). Resolve symlinks so npm's .bin/deft-hook link finds dist/.
4
- SOURCE=$0
5
- while [ -L "$SOURCE" ]; do
6
- TARGET=$(readlink "$SOURCE")
7
- case "$TARGET" in
8
- /*) SOURCE=$TARGET ;;
9
- *) SOURCE=$(dirname "$SOURCE")/$TARGET ;;
10
- esac
11
- done
12
- DIR=$(CDPATH= cd -- "$(dirname "$SOURCE")" && pwd)
13
- export DEFT_LAUNCHER_MODE=hook
14
- exec node "$DIR/../dist/bin.js" "$@"
1
+ #!/usr/bin/env node
2
+ /**
3
+ * PATH entry for deft-hook (AC3.5 / #5998). Node shebang so npm's Windows
4
+ * cmd-shim starts Node, not /bin/sh. Sets DEFT_LAUNCHER_MODE and runs dist/bin.js.
5
+ *
6
+ * Also owns #4654 / #5997 Windows .ps1 shim cleanup via:
7
+ * node ./bin/deft-hook --remove-windows-ps1-shims
8
+ * (launcher postinstall). Logic mirrors packages/runtime/src/windows-bin-ps1.ts.
9
+ */
10
+ import { spawnSync } from "node:child_process";
11
+ import {
12
+ existsSync,
13
+ readFileSync,
14
+ realpathSync,
15
+ unlinkSync,
16
+ } from "node:fs";
17
+ import { basename, dirname, join, win32 } from "node:path";
18
+ import { fileURLToPath } from "node:url";
19
+
20
+ const REMOVE_FLAG = "--remove-windows-ps1-shims";
21
+
22
+ const PACKAGE_BIN_NAMES = ["directive", "deft", "deft-hook"];
23
+ const NODE_OWNED_SHIM_NAMES = ["npx", "npm"];
24
+
25
+ const here = dirname(fileURLToPath(import.meta.url));
26
+ const pkgDir = dirname(here);
27
+
28
+ if (process.argv.includes(REMOVE_FLAG)) {
29
+ const result = removeInstalledWindowsPs1Shims({
30
+ pkgDir,
31
+ env: process.env,
32
+ platform: process.platform,
33
+ });
34
+ if (result.failed.length > 0) {
35
+ process.stderr.write(
36
+ `remove-windows-ps1-shims: could not remove ${result.failed.join(", ")}\n`,
37
+ );
38
+ process.exit(1);
39
+ }
40
+ process.exit(0);
41
+ }
42
+
43
+ process.env.DEFT_LAUNCHER_MODE = "hook";
44
+ const binJs = join(pkgDir, "dist", "bin.js");
45
+ if (!existsSync(binJs)) {
46
+ process.stderr.write(`deft-hook: missing ${binJs}\n`);
47
+ process.exit(1);
48
+ }
49
+ const args = process.argv.slice(2).filter((a) => a !== REMOVE_FLAG);
50
+ const result = spawnSync(process.execPath, [binJs, ...args], {
51
+ cwd: process.cwd(),
52
+ env: process.env,
53
+ stdio: "inherit",
54
+ });
55
+ if (result.error) {
56
+ process.stderr.write(`deft-hook: failed to start: ${result.error.message}\n`);
57
+ process.exit(1);
58
+ }
59
+ if (result.status === null && result.signal) process.exit(130);
60
+ process.exit(result.status ?? 1);
61
+
62
+ function packageIsUnderNodeModules(dir) {
63
+ const parent = dirname(dir);
64
+ if (basename(parent) === "node_modules") return true;
65
+ return basename(dirname(parent)) === "node_modules";
66
+ }
67
+
68
+ function nodeModulesDir(dir) {
69
+ const scopeOrNm = dirname(dir);
70
+ if (basename(scopeOrNm) === "node_modules") return scopeOrNm;
71
+ return dirname(scopeOrNm);
72
+ }
73
+
74
+ function linkerBinDir(opts) {
75
+ const nm = nodeModulesDir(opts.pkgDir);
76
+ if (!opts.global) return join(nm, ".bin");
77
+ const prefix = dirname(nm);
78
+ if (opts.platform === "win32") return prefix;
79
+ return join(prefix, "bin");
80
+ }
81
+
82
+ function isGlobalInstall(env) {
83
+ return env.npm_config_global === "true";
84
+ }
85
+
86
+ function dirKey(dir, platform) {
87
+ const norm = dir.replace(/[\\/]+/g, "/");
88
+ if (platform === "win32") return norm.toLowerCase();
89
+ return norm;
90
+ }
91
+
92
+ function ancestorBinDirs(dir, stopDir, platform) {
93
+ const stop = dirKey(stopDir, platform);
94
+ const start = dirKey(dir, platform);
95
+ const stopPrefix = stop.endsWith("/") ? stop : `${stop}/`;
96
+ if (start !== stop && !start.startsWith(stopPrefix)) {
97
+ return [join(stopDir, "node_modules", ".bin")];
98
+ }
99
+ const out = [];
100
+ let cur = dir;
101
+ for (;;) {
102
+ out.push(join(cur, "node_modules", ".bin"));
103
+ if (dirKey(cur, platform) === stop) break;
104
+ const parent = dirname(cur);
105
+ if (parent === cur) break;
106
+ cur = parent;
107
+ }
108
+ return out;
109
+ }
110
+
111
+ function candidateBinDirs(opts) {
112
+ const global = isGlobalInstall(opts.env);
113
+ const dirs = [];
114
+ if (packageIsUnderNodeModules(opts.pkgDir)) {
115
+ dirs.push(linkerBinDir({ pkgDir: opts.pkgDir, global, platform: opts.platform }));
116
+ }
117
+ if (global) {
118
+ const prefix = opts.env.npm_config_prefix;
119
+ if (prefix !== undefined && prefix.length > 0) {
120
+ dirs.push(opts.platform === "win32" ? prefix : join(prefix, "bin"));
121
+ }
122
+ const pnpmHome = opts.env.PNPM_HOME;
123
+ if (pnpmHome !== undefined && pnpmHome.length > 0) dirs.push(pnpmHome);
124
+ } else {
125
+ const localPrefix = opts.env.npm_config_local_prefix;
126
+ if (localPrefix !== undefined && localPrefix.length > 0) {
127
+ dirs.push(...ancestorBinDirs(opts.pkgDir, localPrefix, opts.platform));
128
+ }
129
+ }
130
+ const seen = new Set();
131
+ const out = [];
132
+ for (const dir of dirs) {
133
+ const key = dirKey(dir, opts.platform);
134
+ if (seen.has(key)) continue;
135
+ seen.add(key);
136
+ out.push(dir);
137
+ }
138
+ return out;
139
+ }
140
+
141
+ function isSafeBinName(name) {
142
+ if (name.length === 0) return false;
143
+ if (name.includes("/") || name.includes("\\") || name.includes("..")) return false;
144
+ return true;
145
+ }
146
+
147
+ function removeGeneratedPs1Shims(opts) {
148
+ if (opts.platform !== "win32") return { removed: [], failed: [] };
149
+ const protect = new Set(
150
+ (opts.protectedNames ?? NODE_OWNED_SHIM_NAMES).map((name) => name.toLowerCase()),
151
+ );
152
+ const removed = [];
153
+ const failed = [];
154
+ for (const name of opts.binNames) {
155
+ if (!isSafeBinName(name) || protect.has(name.toLowerCase())) continue;
156
+ const ps1 = join(opts.binDir, `${name}.ps1`);
157
+ const cmd = join(opts.binDir, `${name}.cmd`);
158
+ if (!existsSync(cmd) || !existsSync(ps1)) continue;
159
+ try {
160
+ unlinkSync(ps1);
161
+ removed.push(ps1);
162
+ } catch {
163
+ failed.push(ps1);
164
+ }
165
+ }
166
+ return { removed, failed };
167
+ }
168
+
169
+ function normalizedWinPath(p) {
170
+ const norm = win32.normalize(p).replace(/[\\/]+/g, "\\");
171
+ const trimmed = norm.length > 3 && norm.endsWith("\\") ? norm.slice(0, -1) : norm;
172
+ return trimmed.toLowerCase();
173
+ }
174
+
175
+ function pathIsInside(root, target) {
176
+ const rootKey = normalizedWinPath(root);
177
+ const targetKey = normalizedWinPath(target);
178
+ return targetKey === rootKey || targetKey.startsWith(`${rootKey}\\`);
179
+ }
180
+
181
+ function hostPath(p) {
182
+ return p.replace(/\\/g, "/");
183
+ }
184
+
185
+ function realpathIsInside(root, target) {
186
+ try {
187
+ return pathIsInside(realpathSync(hostPath(root)), realpathSync(hostPath(target)));
188
+ } catch {
189
+ return false;
190
+ }
191
+ }
192
+
193
+ function cmdShimTargetsPackage(cmdText, binDir, packageDir) {
194
+ const re = /%(?:dp0%|~dp0)(?:\\+|\/+)([^"\r\n\s]+)/gi;
195
+ for (const match of cmdText.matchAll(re)) {
196
+ const rel = match[1];
197
+ if (rel === undefined || rel.length === 0) continue;
198
+ const resolved = win32.normalize(win32.join(binDir, rel.replace(/\//g, "\\")));
199
+ if (pathIsInside(packageDir, resolved) || realpathIsInside(packageDir, resolved)) {
200
+ return true;
201
+ }
202
+ }
203
+ return false;
204
+ }
205
+
206
+ function cmdLinksCurrentInstall(binDir, name, packageDir) {
207
+ if (!isSafeBinName(name)) return false;
208
+ let text;
209
+ try {
210
+ text = readFileSync(join(binDir, `${name}.cmd`), "utf8");
211
+ } catch {
212
+ return false;
213
+ }
214
+ return cmdShimTargetsPackage(text, binDir, packageDir);
215
+ }
216
+
217
+ function readPackageBinNames(packageJsonText) {
218
+ let parsed;
219
+ try {
220
+ parsed = JSON.parse(packageJsonText);
221
+ } catch {
222
+ return [];
223
+ }
224
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return [];
225
+ const bin = parsed.bin;
226
+ if (bin === null || typeof bin !== "object" || Array.isArray(bin)) return [];
227
+ return Object.keys(bin);
228
+ }
229
+
230
+ function removeInstalledWindowsPs1Shims(opts) {
231
+ if (opts.platform !== "win32") return { removed: [], failed: [] };
232
+ let names;
233
+ try {
234
+ names = readPackageBinNames(readFileSync(join(opts.pkgDir, "package.json"), "utf8"));
235
+ } catch {
236
+ names = PACKAGE_BIN_NAMES;
237
+ }
238
+ const removed = [];
239
+ const failed = [];
240
+ for (const binDir of candidateBinDirs(opts)) {
241
+ const linked = names.filter((name) => cmdLinksCurrentInstall(binDir, name, opts.pkgDir));
242
+ const result = removeGeneratedPs1Shims({
243
+ binDir,
244
+ binNames: linked,
245
+ platform: opts.platform,
246
+ });
247
+ removed.push(...result.removed);
248
+ failed.push(...result.failed);
249
+ }
250
+ return { removed, failed };
251
+ }
package/bin/deft-hook.cmd CHANGED
@@ -1,13 +1,11 @@
1
1
  @ECHO OFF
2
- REM Windows PATH shim for deft-hook (AC3.5). npm may not auto-pick this file when
3
- REM bin points at the POSIX script; keep beside bin/deft-hook for manual/global use
4
- REM and for package managers that install sibling .cmd. Sets mode and runs bin.js.
5
- SET DEFT_LAUNCHER_MODE=hook
6
- NODE_EXE=node
7
- WHERE node >NUL 2>&1 || SET NODE_EXE=node.exe
8
- "%~dp0..\dist\bin.js" 2>NUL
2
+ REM Windows PATH shim for deft-hook (AC3.5 / #5998). Sets mode and runs bin.js via Node.
3
+ SETLOCAL
4
+ SET "DEFT_LAUNCHER_MODE=hook"
5
+ SET "NODE_EXE=node"
6
+ WHERE node >NUL 2>&1 || SET "NODE_EXE=node.exe"
9
7
  IF EXIST "%~dp0..\dist\bin.js" (
10
- %NODE_EXE% "%~dp0..\dist\bin.js" %*
8
+ "%NODE_EXE%" "%~dp0..\dist\bin.js" %*
11
9
  EXIT /B %ERRORLEVEL%
12
10
  )
13
11
  ECHO deft-hook: missing %~dp0..\dist\bin.js
package/dist/bin.js CHANGED
@@ -2188,13 +2188,44 @@ function validateTargetVersionSpec(raw) {
2188
2188
  }
2189
2189
  return { ok: true, version: trimmed };
2190
2190
  }
2191
- function resolveLatestStableVersion(env = process.env, projectRoot) {
2192
- const result = spawnSync5("npm", ["view", RUNTIME_PACKAGE_NAME, "version", "--json"], {
2191
+ function defaultNpmView(args, opts) {
2192
+ return spawnSync5("npm", [...args], {
2193
2193
  encoding: "utf8",
2194
- env,
2195
- cwd: projectRoot,
2194
+ env: opts.env,
2195
+ cwd: opts.cwd,
2196
2196
  shell: process.platform === "win32"
2197
2197
  });
2198
+ }
2199
+ function unwrapNpmViewJson(raw) {
2200
+ const trimmed = raw.trim();
2201
+ if (trimmed.length === 0) {
2202
+ return { ok: false, message: "npm view --json returned empty output." };
2203
+ }
2204
+ let parsed;
2205
+ try {
2206
+ parsed = JSON.parse(trimmed);
2207
+ } catch {
2208
+ return { ok: false, message: "Could not parse npm view --json output." };
2209
+ }
2210
+ if (Array.isArray(parsed)) {
2211
+ if (parsed.length === 1) {
2212
+ return { ok: true, value: parsed[0] };
2213
+ }
2214
+ if (parsed.length === 0) {
2215
+ return { ok: false, message: "npm view --json returned an empty array." };
2216
+ }
2217
+ return {
2218
+ ok: false,
2219
+ message: `npm view --json returned ${parsed.length} values; expected a single result (refusing to pick arbitrarily).`
2220
+ };
2221
+ }
2222
+ return { ok: true, value: parsed };
2223
+ }
2224
+ function resolveLatestStableVersion(env = process.env, projectRoot, runNpmView = defaultNpmView) {
2225
+ const result = runNpmView(["view", RUNTIME_PACKAGE_NAME, "version", "--json"], {
2226
+ env,
2227
+ cwd: projectRoot
2228
+ });
2198
2229
  if (result.error) {
2199
2230
  return {
2200
2231
  ok: false,
@@ -2207,13 +2238,17 @@ function resolveLatestStableVersion(env = process.env, projectRoot) {
2207
2238
  message: `npm view ${RUNTIME_PACKAGE_NAME} version failed` + (result.stderr ? `: ${result.stderr.trim()}` : "")
2208
2239
  };
2209
2240
  }
2210
- let version = (result.stdout ?? "").trim();
2211
- if (version.startsWith('"') && version.endsWith('"')) {
2212
- try {
2213
- version = JSON.parse(version);
2214
- } catch {
2215
- }
2241
+ const unwrapped = unwrapNpmViewJson(result.stdout ?? "");
2242
+ if (!unwrapped.ok) {
2243
+ return { ok: false, message: unwrapped.message };
2216
2244
  }
2245
+ if (typeof unwrapped.value !== "string") {
2246
+ return {
2247
+ ok: false,
2248
+ message: `Registry latest is not an exact SemVer (${JSON.stringify(unwrapped.value)}).`
2249
+ };
2250
+ }
2251
+ const version = unwrapped.value.trim();
2217
2252
  if (!isExactSemver(version)) {
2218
2253
  return {
2219
2254
  ok: false,
@@ -2228,17 +2263,11 @@ function resolveLatestStableVersion(env = process.env, projectRoot) {
2228
2263
  }
2229
2264
  return { ok: true, version };
2230
2265
  }
2231
- function resolvePublishedContract(version, env = process.env, projectRoot) {
2232
- const result = spawnSync5(
2233
- "npm",
2234
- ["view", `${RUNTIME_PACKAGE_NAME}@${version}`, "directive", "--json"],
2235
- {
2236
- encoding: "utf8",
2237
- env,
2238
- cwd: projectRoot,
2239
- shell: process.platform === "win32"
2240
- }
2241
- );
2266
+ function resolvePublishedContract(version, env = process.env, projectRoot, runNpmView = defaultNpmView) {
2267
+ const result = runNpmView(["view", `${RUNTIME_PACKAGE_NAME}@${version}`, "directive", "--json"], {
2268
+ env,
2269
+ cwd: projectRoot
2270
+ });
2242
2271
  if (result.error) {
2243
2272
  return {
2244
2273
  ok: false,
@@ -2258,16 +2287,14 @@ function resolvePublishedContract(version, env = process.env, projectRoot) {
2258
2287
  message: `Published ${RUNTIME_PACKAGE_NAME}@${version} has no directive.contract metadata.`
2259
2288
  };
2260
2289
  }
2261
- let parsed;
2262
- try {
2263
- parsed = JSON.parse(raw);
2264
- } catch {
2290
+ const unwrapped = unwrapNpmViewJson(raw);
2291
+ if (!unwrapped.ok) {
2265
2292
  return {
2266
2293
  ok: false,
2267
- message: `Could not parse directive metadata for ${RUNTIME_PACKAGE_NAME}@${version}.`
2294
+ message: unwrapped.message === "Could not parse npm view --json output." ? `Could not parse directive metadata for ${RUNTIME_PACKAGE_NAME}@${version}.` : unwrapped.message
2268
2295
  };
2269
2296
  }
2270
- let directive = parsed;
2297
+ let directive = unwrapped.value;
2271
2298
  if (directive !== null && typeof directive === "object" && !Array.isArray(directive) && !("contract" in directive)) {
2272
2299
  const values = Object.values(directive);
2273
2300
  if (values.length === 1) directive = values[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deftai/directive",
3
- "version": "0.129.5",
3
+ "version": "0.129.6",
4
4
  "description": "Directive launcher — finds the project pin and hands off to @deftai/directive-runtime.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://deftai.github.io/directive/",
@@ -30,6 +30,7 @@
30
30
  },
31
31
  "dependencies": {},
32
32
  "scripts": {
33
- "build": "tsc -b && node ./scripts/bundle.mjs"
33
+ "build": "tsc -b && node ./scripts/bundle.mjs",
34
+ "postinstall": "node ./bin/deft-hook --remove-windows-ps1-shims"
34
35
  }
35
36
  }