From 213f7c538305dc5773576eb92f45bc31f86a4ae6 Mon Sep 17 00:00:00 2001 From: wonjang <48057918+wonjangcloud9@users.noreply.github.com> Date: Sat, 14 Nov 2026 03:47:58 +0000 Subject: [PATCH] fix(cli): quote the project name in init's printed `cd` commands (#7608) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `hyperframes init --non-interactive` closes with `cd && npm run …` lines for the person or agent to paste. The name went in as typed, so a name with a space or a quote printed a command that fails as written: `cd my first video` (bash: "cd: my: No such file or directory", zsh: "too many arguments") and `cd it's mine` (an unterminated quote). A new `shellArg` quotes a word only when a shell would split or expand it: double quotes, or single quotes when the text holds `$`, a backtick, `\`, `"` or `!`; on Windows, double quotes, with `\` paths left bare. `hyperframes open` / `preview --stop` hints (desktopApp's `shownDir`) already quoted whitespace with JSON.stringify and now use it too, so a name like `it's` or `a&b` is covered there as well. Co-authored-by: Claude Opus 5.5 --- packages/cli/src/commands/init.ts | 9 ++-- packages/cli/src/utils/desktopApp.ts | 4 +- packages/cli/src/utils/shellArg.test.ts | 56 +++++++++++++++++++++++++ packages/cli/src/utils/shellArg.ts | 11 +++++ 4 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 packages/cli/src/utils/shellArg.test.ts create mode 100644 packages/cli/src/utils/shellArg.ts diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index 2cd0952447..ac088c0a5c 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -63,6 +63,7 @@ import { } from "@hyperframes/parsers"; import { decodableAudioStream } from "@hyperframes/parsers/media-streams"; import { cliSourced } from "../utils/sourceTag.js"; +import { shellArg } from "../utils/shellArg.js"; function resolveScaffoldTemplateId(exampleFlag: string | undefined, hasMediaFile: boolean): string { const example = exampleFlag === "agent" ? "blank" : exampleFlag; @@ -1044,7 +1045,7 @@ export default defineCommand({ console.log(); console.log(` ${c.accent("2.")} Open this project with your AI coding agent:`); console.log( - ` ${c.accent(`cd ${name}`)} then start ${c.accent("Claude Code")}, ${c.accent("Cursor")}, or your preferred agent`, + ` ${c.accent(`cd ${shellArg(name)}`)} then start ${c.accent("Claude Code")}, ${c.accent("Cursor")}, or your preferred agent`, ); console.log(); console.log(` ${c.accent("3.")} Try a starter prompt:`); @@ -1056,13 +1057,13 @@ export default defineCommand({ ); console.log(); console.log(` ${c.accent("4.")} Preview in the browser:`); - console.log(` ${c.accent(`cd ${name}`)} && ${c.accent("npm run dev")}`); + console.log(` ${c.accent(`cd ${shellArg(name)}`)} && ${c.accent("npm run dev")}`); console.log(); console.log(` ${c.accent("5.")} Check the composition:`); - console.log(` ${c.accent(`cd ${name}`)} && ${c.accent("npm run check")}`); + console.log(` ${c.accent(`cd ${shellArg(name)}`)} && ${c.accent("npm run check")}`); console.log(); console.log(` ${c.accent("6.")} Render to MP4 when ready:`); - console.log(` ${c.accent(`cd ${name}`)} && ${c.accent("npm run render")}`); + console.log(` ${c.accent(`cd ${shellArg(name)}`)} && ${c.accent("npm run render")}`); console.log(); console.log(` ${c.dim(`Full docs: ${cliSourced("https://hyperframes.heygen.com")}`)}`); return; diff --git a/packages/cli/src/utils/desktopApp.ts b/packages/cli/src/utils/desktopApp.ts index f0c9f7f845..b765518b5c 100644 --- a/packages/cli/src/utils/desktopApp.ts +++ b/packages/cli/src/utils/desktopApp.ts @@ -8,6 +8,7 @@ import { createProjectConfig } from "./projectConfig.js"; import { isCI } from "./attendedTerminal.js"; import { isAgentDriven } from "../telemetry/agent_runtime.js"; import { readRecord, removeRecord, writeRecord } from "./projectRecords.js"; +import { shellArg } from "./shellArg.js"; // The HyperFrames desktop app, then builds named in HYPERFRAMES_DESKTOP_BUNDLE_IDS / _APP_NAMES (comma-separated). const list = (name: string): string[] => @@ -247,8 +248,7 @@ function leaveIntent(dir: string, cta?: string, doc?: "storyboard"): () => void } function shownDir(dir: string, cwd: string): string { - const shown = relative(cwd, dir) || "."; - return /\s/.test(shown) ? JSON.stringify(shown) : shown; + return shellArg(relative(cwd, dir) || "."); } /** The `hyperframes open` line for a project, relative to where the person is. */ diff --git a/packages/cli/src/utils/shellArg.test.ts b/packages/cli/src/utils/shellArg.test.ts new file mode 100644 index 0000000000..ab407e85c0 --- /dev/null +++ b/packages/cli/src/utils/shellArg.test.ts @@ -0,0 +1,56 @@ +import { execFileSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, realpathSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterAll, describe, expect, it } from "vitest"; +import { shellArg } from "./shellArg.js"; + +const NAMES = [ + "my-video", + "서울여행", + "films/a", + "my first video", + "it's mine", + "it's", + "a&b", + "$HOME", + "o'neil $x", + "서울 여행 브이로그", +]; + +describe("shellArg", () => { + it("leaves a plain name alone and quotes one the shell would split or expand", () => { + expect(NAMES.map((name) => shellArg(name, "darwin"))).toEqual([ + "my-video", + "서울여행", + "films/a", + '"my first video"', + `"it's mine"`, + `"it's"`, + '"a&b"', + "'$HOME'", + `'o'\\''neil $x'`, + '"서울 여행 브이로그"', + ]); + }); + + it("uses double quotes on Windows, where a backslash path needs none", () => { + expect(shellArg("films\\a", "win32")).toBe("films\\a"); + expect(shellArg("my first video", "win32")).toBe('"my first video"'); + expect(shellArg("it's mine", "win32")).toBe(`"it's mine"`); + }); + + const root = mkdtempSync(join(tmpdir(), "hf-shell-arg-")); + afterAll(() => rmSync(root, { recursive: true, force: true })); + + it.skipIf(process.platform === "win32")("prints a cd that a POSIX shell runs as written", () => { + for (const name of NAMES) { + mkdirSync(join(root, name), { recursive: true }); + const pwd = execFileSync("/bin/sh", ["-c", `cd ${shellArg(name)} && pwd -P`], { + cwd: root, + encoding: "utf8", + }); + expect(pwd.trim()).toBe(realpathSync(join(root, name))); + } + }); +}); diff --git a/packages/cli/src/utils/shellArg.ts b/packages/cli/src/utils/shellArg.ts new file mode 100644 index 0000000000..8020403742 --- /dev/null +++ b/packages/cli/src/utils/shellArg.ts @@ -0,0 +1,11 @@ +const PLAIN = /^[\p{L}\p{M}\p{N}_./@:+,=-]+$/u; +const PLAIN_WINDOWS_PATH = /^[\p{L}\p{M}\p{N}_./@:+,=\\-]+$/u; +const SPECIAL_IN_DOUBLE_QUOTES = /["$`\\!]/; + +/** `text` as one word of a command printed for the person to paste into their shell. */ +export function shellArg(text: string, platform: NodeJS.Platform = process.platform): string { + if (platform === "win32") return PLAIN_WINDOWS_PATH.test(text) ? text : `"${text}"`; + if (PLAIN.test(text)) return text; + if (!SPECIAL_IN_DOUBLE_QUOTES.test(text)) return `"${text}"`; + return `'${text.replaceAll("'", "'\\''")}'`; +}