Allow dash-prefixed cwd values in --cwd form
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
79f54596e7
commit
94b265759b
2 files changed
+45
-3
No files matched your search
@@ -292,6 +292,17 @@ describe("parseCliOptions", () => {
|
||||
expect(options.launchCwd).toBe(path.resolve("/workspace", "apps/renderer"));
|
||||
});
|
||||
|
||||
it("accepts dash-prefixed cwd values with separate --cwd argument", () => {
|
||||
const options = parseCliOptions(["--cwd", "-project"], {}, "/workspace");
|
||||
expect(options.launchCwd).toBe(path.resolve("/workspace", "-project"));
|
||||
});
|
||||
|
||||
it("treats known flags after --cwd as missing values", () => {
|
||||
expect(() => parseCliOptions(["--cwd", "--help"], {}, "/workspace")).toThrow(
|
||||
"Missing value for --cwd",
|
||||
);
|
||||
});
|
||||
|
||||
it("throws for unknown arguments", () => {
|
||||
expect(() => parseCliOptions(["--wat"], {}, "/workspace")).toThrow(
|
||||
"Unknown argument: --wat",
|
||||
|
||||
+34
-3
@@ -75,9 +75,36 @@ interface StartupErrorShape {
|
||||
message?: unknown;
|
||||
}
|
||||
|
||||
function readArgValue(args: string[], index: number, key: string): string {
|
||||
const KNOWN_CLI_FLAGS = new Set([
|
||||
"--help",
|
||||
"-h",
|
||||
"--version",
|
||||
"-v",
|
||||
"--no-open",
|
||||
"--backend-port",
|
||||
"--web-port",
|
||||
"--cwd",
|
||||
"--",
|
||||
]);
|
||||
|
||||
function readArgValue(
|
||||
args: string[],
|
||||
index: number,
|
||||
key: string,
|
||||
options?: {
|
||||
allowDashPrefixed?: boolean;
|
||||
},
|
||||
): string {
|
||||
const value = args[index + 1];
|
||||
if (!value || value.startsWith("-")) {
|
||||
if (!value) {
|
||||
throw new Error(`Missing value for ${key}.`);
|
||||
}
|
||||
|
||||
const dashPrefixed = value.startsWith("-");
|
||||
if (!options?.allowDashPrefixed && dashPrefixed) {
|
||||
throw new Error(`Missing value for ${key}.`);
|
||||
}
|
||||
if (options?.allowDashPrefixed && dashPrefixed && KNOWN_CLI_FLAGS.has(value)) {
|
||||
throw new Error(`Missing value for ${key}.`);
|
||||
}
|
||||
|
||||
@@ -198,7 +225,11 @@ export function parseCliOptions(
|
||||
}
|
||||
|
||||
if (arg === "--cwd") {
|
||||
launchCwd = parseExplicitPath(readArgValue(argv, index, "--cwd"), "--cwd", parserCwd);
|
||||
launchCwd = parseExplicitPath(
|
||||
readArgValue(argv, index, "--cwd", { allowDashPrefixed: true }),
|
||||
"--cwd",
|
||||
parserCwd,
|
||||
);
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user