Skip to content

Commit 3246718

Browse files
feat(cli): hyperframes <dir> opens the project in the app (#6426)
* feat(cli): hyperframes <dir> opens the project in the app * refactor(cli): one command-word lookup and one rewritten command line for the folder shortcut * refactor(cli): shorten the folder shortcut comments to fit the comment ratchet
1 parent d8398c9 commit 3246718

6 files changed

Lines changed: 127 additions & 10 deletions

File tree

‎docs/packages/cli.mdx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,9 @@ ownership.
948948
`open` opens a project in the HyperFrames desktop app and adds it to Home.
949949

950950
```bash
951-
npx hyperframes open # the project in the current directory
952-
npx hyperframes open ./my-video
951+
npx hyperframes . # the project in the current directory
952+
npx hyperframes ./my-video # any folder that isn't a command name
953+
npx hyperframes open # the same, naming the command
953954
npx hyperframes open --json # for agents
954955
```
955956

‎packages/cli/src/cli.lifecycle.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,32 @@ describe("CLI lifecycle", () => {
145145
});
146146
});
147147

148+
it("runs `open` for a bare project folder, and keeps `render` a command", async () => {
149+
const dir = mkdtempSync(join(tmpdir(), "hf-cli-shortcut-"));
150+
const run = vi.fn();
151+
try {
152+
for (const [name, argv] of [
153+
["open", [dir]],
154+
["open", ["--verbose", dir]],
155+
["render", ["render"]],
156+
] as const) {
157+
vi.doMock(`./commands/${name}.js`, () => ({
158+
default: { meta: { name }, args: { dir: { type: "positional", required: false } }, run },
159+
}));
160+
mockTelemetry();
161+
process.argv = ["node", "cli.ts", ...argv];
162+
await import("./cli.js");
163+
await vi.dynamicImportSettled();
164+
vi.doUnmock(`./commands/${name}.js`);
165+
vi.resetModules();
166+
}
167+
expect(run.mock.calls.map(([ctx]) => ctx.rawArgs)).toEqual([[dir], [dir], []]);
168+
expect(process.argv.slice(2)).toEqual(["render"]);
169+
} finally {
170+
rmSync(dir, { recursive: true, force: true });
171+
}
172+
});
173+
148174
it("hands queued events to flushSync even after finalizeCli has run", async () => {
149175
const flushSync = vi.fn();
150176
const trackCommandResult = vi.fn();

‎packages/cli/src/cli.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { existsSync } from "node:fs";
4848
// `hyperframes --version` near-instant (~10ms vs ~80ms).
4949
import { VERSION } from "./version.js";
5050

51-
const argv = process.argv.slice(2);
51+
let argv = process.argv.slice(2);
5252
const commandArg = argv[0];
5353
const rootVersionRequested =
5454
commandArg === "--version" ||
@@ -63,11 +63,7 @@ if (rootVersionRequested) {
6363
// Capture treats CWD as project data too: its early loader must use the same
6464
// allowlist/opt-out as output ancestors, before browser selection can observe it.
6565
const { applyDotEnv, CAPTURE_ENV_KEYS } = await import("./utils/dotEnv.js");
66-
// The root has no value-taking options: Citty skips flags up to the first
67-
// positional command, and stops subcommand selection at the -- separator.
68-
const separator = argv.indexOf("--");
69-
const commandArgs = separator < 0 ? argv : argv.slice(0, separator);
70-
const captureEnv = commandArgs.find((arg) => !arg.startsWith("-")) === "capture";
66+
const captureEnv = argv[commandWordIndex(argv)] === "capture";
7167
if (!captureEnv || process.env.HYPERFRAMES_NO_CAPTURE_DOTENV !== "1") {
7268
try {
7369
const { readFileSync } = await import("node:fs");
@@ -104,6 +100,7 @@ import {
104100
type CommandResult,
105101
} from "./utils/commandResult.js";
106102
import { registerRunningCli } from "./utils/runningCli.js";
103+
import { commandWordIndex, openShortcutArgv } from "./utils/projectShortcut.js";
107104

108105
registerRunningCli();
109106

@@ -183,6 +180,13 @@ const commandLoaders = {
183180
figma: () => import("./commands/figma.js").then((m) => m.default),
184181
};
185182

183+
// `hyperframes <dir>` is `hyperframes open <dir>`; every later read of the command sees `open`.
184+
process.argv = [
185+
...process.argv.slice(0, 2),
186+
...openShortcutArgv(argv, new Set(Object.keys(commandLoaders))),
187+
];
188+
argv = process.argv.slice(2);
189+
186190
const subCommands = Object.fromEntries(
187191
Object.entries(commandLoaders).map(([name, load]) => [name, trackCommandFailures(load)]),
188192
);

‎packages/cli/src/commands/open.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import { downloadHint, openInDesktop, type DesktopOpenResult } from "../utils/de
66
import { resolveProject, resolveProjectOrThrow, type ProjectDir } from "../utils/project.js";
77

88
export const examples: Example[] = [
9-
["Open this project in the HyperFrames desktop app", "hyperframes open"],
10-
["Open another project", "hyperframes open ./my-video"],
9+
["Open this project in the HyperFrames desktop app", "hyperframes ."],
10+
["Open another project", "hyperframes ./my-video"],
11+
["Same, naming the command (needed for a folder named like a command)", "hyperframes open ."],
1112
["For agents", "hyperframes open --json"],
1213
];
1314

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import { afterAll, describe, expect, it } from "vitest";
5+
import { commandWordIndex, openShortcutArgv } from "./projectShortcut.js";
6+
7+
const commands = new Set(["open", "render", "lint"]);
8+
const cwd = mkdtempSync(join(tmpdir(), "hf-shortcut-"));
9+
mkdirSync(join(cwd, "my-video"));
10+
mkdirSync(join(cwd, "render"));
11+
writeFileSync(join(cwd, "notes.txt"), "");
12+
afterAll(() => rmSync(cwd, { recursive: true, force: true }));
13+
14+
describe("openShortcutArgv", () => {
15+
it.each([".", "./my-video", "my-video", cwd])("opens the folder %s", (dir) => {
16+
expect(openShortcutArgv([dir], commands, cwd)).toEqual(["open", dir]);
17+
});
18+
19+
it("keeps the flags that follow the folder", () => {
20+
expect(openShortcutArgv(["my-video", "--json"], commands, cwd)).toEqual([
21+
"open",
22+
"my-video",
23+
"--json",
24+
]);
25+
});
26+
27+
it("finds the folder after root flags, like the command word", () => {
28+
expect(openShortcutArgv(["--verbose", "my-video", "--json"], commands, cwd)).toEqual([
29+
"--verbose",
30+
"open",
31+
"my-video",
32+
"--json",
33+
]);
34+
});
35+
36+
it("ignores a folder after the -- separator", () => {
37+
expect(openShortcutArgv(["--", "my-video"], commands, cwd)).toEqual(["--", "my-video"]);
38+
});
39+
40+
it("lets a known command win over a folder with its name", () => {
41+
expect(openShortcutArgv(["render"], commands, cwd)).toEqual(["render"]);
42+
});
43+
44+
it.each([[[]], [["--help"]], [["notes.txt"]], [["nope"]]])("leaves %j alone", (argv) => {
45+
expect(openShortcutArgv(argv, commands, cwd)).toEqual(argv);
46+
});
47+
});
48+
49+
describe("commandWordIndex", () => {
50+
it.each([
51+
[["render", "x"], 0],
52+
[["--verbose", "render"], 1],
53+
[["--verbose"], -1],
54+
[["--", "render"], -1],
55+
[["--verbose", "--", "render"], -1],
56+
[[], -1],
57+
] as const)("%j -> %i", (argv, index) => {
58+
expect(commandWordIndex(argv)).toBe(index);
59+
});
60+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { statSync } from "node:fs";
2+
import { resolve } from "node:path";
3+
4+
/** Index of the root command word (first non-flag before `--`), or -1; the root has no value-taking options. */
5+
export function commandWordIndex(argv: readonly string[]): number {
6+
const end = argv.indexOf("--");
7+
const index = argv.findIndex((arg) => !arg.startsWith("-"));
8+
return end >= 0 && index >= end ? -1 : index;
9+
}
10+
11+
/** `hyperframes <dir>` is `hyperframes open <dir>`; a known command wins over a folder of the same name. */
12+
export function openShortcutArgv(
13+
argv: readonly string[],
14+
commands: ReadonlySet<string>,
15+
cwd: string = process.cwd(),
16+
): readonly string[] {
17+
const at = commandWordIndex(argv);
18+
if (at < 0 || commands.has(argv[at]!)) return argv;
19+
try {
20+
if (!statSync(resolve(cwd, argv[at]!)).isDirectory()) return argv;
21+
} catch {
22+
return argv;
23+
}
24+
return [...argv.slice(0, at), "open", ...argv.slice(at)];
25+
}

0 commit comments

Comments
 (0)