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("'", "'\\''")}'`; +}