Files
hyperframes/skills/media-use/scripts/lib/npx-sync.mjs
T
James RussoandClaude Opus 5.5 a8fe3a5b68 fix(skills): plugin launcher and media helpers run the pinned CLI, not a project copy (#7552)
* fix(skills): plugin launcher and media helpers run the pinned CLI, not a project copy

The plugin launcher now starts npx from the home folder, so the project's
node_modules, package.json bin and .npmrc cannot choose which CLI npm runs or
how it starts it. It names the project in HYPERFRAMES_PLUGIN_CWD and the CLI's
bin moves into that folder before anything else runs, then drops the variable
so nested runs keep the folder their caller gives them.

In --script mode the launcher hands itself over as HYPERFRAMES_CLI_ENTRY. The
media helpers (Kokoro voice, transcription, the resolve shim) run the CLI they
were handed, and stop with an actionable message when none was, instead of
resolving `npx hyperframes` from the project.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(skills): keep the project name and require a home folder for npx

The CLI bin now sets PWD to the project folder after moving into it, because
npm's shell resets PWD to the folder npx started in and preview names the
project from PWD. The plugin launcher no longer falls back to the temp folder
when there is no home folder; it stops with an error instead, and only when it
is about to start npx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(cli): type the bin test environment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:28:10 +00:00

79 lines
3.2 KiB
JavaScript

import { existsSync } from "node:fs";
import { dirname, isAbsolute, join } from "node:path";
function resolveNpxCliPath(env, pathExists) {
const npmExecPath = env.npm_execpath;
const nodeExecPath = env.npm_node_execpath || process.execPath;
if (npmExecPath) {
const fileName = npmExecPath.replace(/\\/g, "/").split("/").pop()?.toLowerCase();
const candidate =
fileName === "npx-cli.js" ? npmExecPath : join(dirname(npmExecPath), "npx-cli.js");
if (pathExists(candidate)) return candidate;
}
const besideNode = join(dirname(nodeExecPath), "node_modules", "npm", "bin", "npx-cli.js");
return pathExists(besideNode) ? besideNode : null;
}
// Sync-spawn analog of the audio engine's spawnP, for execFileSync call sites
// that must hard-fail (rather than fall through to another provider) when npx
// cannot be resolved. On Windows a bare "npx" is npx.cmd, which
// execFileSync/spawnSync cannot exec (spawnSync npx ENOENT) —
// resolveSpawnCommand reroutes it through node + npx-cli.js, no shell:true.
//
// `platform`/`env`/`pathExists` params (defaulting to the real values) exist
// so tests can exercise the win32 branch without mocking node:child_process
// (its ESM exports are non-configurable) — same idiom as spawnP and
// localTtsGenerate.
// The HyperFrames CLI or plugin launcher that starts this code hands over its own entry file, so
// `npx hyperframes` runs that same CLI with the same Node. Without one it fails rather than
// letting npx resolve `hyperframes` from the project.
function launchingCliInvocation(argv, opts, env) {
if (argv[0] !== "hyperframes") return null;
const entry = env.HYPERFRAMES_CLI_ENTRY;
if (!entry || !isAbsolute(entry)) {
throw new Error(
"No HyperFrames CLI was handed to this helper, and it does not look one up in the project. " +
"Run the helper through the plugin launcher (`plugin-cli.mjs --script <helper> ...`), or set " +
"HYPERFRAMES_CLI_ENTRY to the absolute path of your installed CLI's bin/hyperframes.mjs.",
);
}
return {
cmd: process.execPath,
args: [entry, ...argv.slice(1).map((arg) => String(arg))],
opts: { stdio: "ignore", windowsHide: true, ...opts },
};
}
export function resolveNpxInvocation(
argv,
opts,
platform = process.platform,
env = process.env,
pathExists = existsSync,
) {
const resolved =
launchingCliInvocation(argv, opts, env) ??
(platform !== "win32"
? { cmd: "npx", args: argv, opts: { stdio: "ignore", ...opts } }
: (() => {
const nodeExecPath = env.npm_node_execpath || process.execPath;
const npxCliPath = resolveNpxCliPath(env, pathExists);
return npxCliPath
? {
cmd: nodeExecPath,
args: [npxCliPath, ...argv.map((arg) => String(arg))],
opts: { stdio: "ignore", windowsHide: true, ...opts },
}
: null;
})());
if (!resolved) {
// npx-on-win32 with no resolvable npx-cli.js — same terminal condition
// spawnP warns about, surfaced as a throw for callers with no fallback.
throw new Error(
"cannot run npx on Windows: npm's npx-cli.js was not found " +
"(install npm with Node, or run via npx/npm run so npm_execpath is set)",
);
}
return resolved;
}