Files
hyperframes/packages/cli/src/utils/projectShortcut.ts
T
WaterrrForever eb0a52c298 fix(cli): measure answers the four mistakes agents make most (#7443)
* fix(cli): measure answers the four mistakes agents make most

The usage errors agents hit most on `measure`: no op named, a path it
cannot read, and a flag it does not take. Some agents never get past
them to a successful measure.

- `measure <video>` with no op (alone or with --json) runs `probe` and
  says so on stderr. With other flags the op is a guess, so the
  unknown-command hint asks for one; it now names a path that does not
  exist yet too.
- When ffprobe cannot read a file, the error carries ffprobe's reason and
  a matching fix: a cut-short download, a page saved as .mp4, a
  permission.
- An unquoted path with spaces says to quote it, both where the extra
  words are rejected and where the cut path is missing.
- Window flags take --from/--to, and folder flags take -o.

* refactor(cli): keep comment share at the base in the measure files

* test(cli): expect a spaced path as the shell split it, on Windows too

* fix(cli): a cut-short MP4 says download it again, read from its header and ffprobe's specific line

* fix(cli): an unquoted spaced path reaches probe or its quote hint wherever it falls
2026-11-09 14:42:37 +00:00

53 lines
1.7 KiB
TypeScript

import { statSync } from "node:fs";
import { resolve } from "node:path";
/** Index of the root command word (first non-flag before `--`), or -1; the root has no value-taking options. */
export function commandWordIndex(argv: readonly string[]): number {
const end = argv.indexOf("--");
const index = argv.findIndex((arg) => !arg.startsWith("-"));
return end >= 0 && index >= end ? -1 : index;
}
/** `hyperframes <dir>` is `hyperframes open <dir>`; a known command wins over a folder of the same name. */
export function openShortcutArgv(
argv: readonly string[],
commands: ReadonlySet<string>,
cwd: string = process.cwd(),
): readonly string[] {
const at = commandWordIndex(argv);
if (at < 0 || commands.has(argv[at]!)) return argv;
try {
if (!statSync(resolve(cwd, argv[at]!)).isDirectory()) return argv;
} catch {
return argv;
}
return [...argv.slice(0, at), "open", ...argv.slice(at)];
}
export function measureProbeShortcutArgv(
argv: readonly string[],
ops: ReadonlySet<string>,
cwd: string = process.cwd(),
): readonly string[] {
const at = commandWordIndex(argv);
if (at < 0 || argv[at] !== "measure") return argv;
const words = argv.slice(at + 1);
if (words.length === 0 || ops.has(words[0]!)) return argv;
for (let n = words.length; n > 0; n--) {
const file = words.slice(0, n).join(" ");
const rest = words.slice(n);
if (words.slice(0, n).some((w) => w.startsWith("-"))) continue;
if (!rest.every((arg) => arg === "--json")) continue;
if (isFile(resolve(cwd, file))) return [...argv.slice(0, at + 1), "probe", file, ...rest];
}
return argv;
}
function isFile(path: string): boolean {
try {
return statSync(path).isFile();
} catch {
return false;
}
}