-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reafactor code and add unit tests for parseArgs
- Loading branch information
1 parent
4da5e7f
commit 40aca43
Showing
11 changed files
with
246 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/create-cloudflare/src/helpers/__tests__/args.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { describe, expect, test, vi } from "vitest"; | ||
import { parseArgs } from "../args"; | ||
|
||
vi.mock("yargs/helpers", () => ({ hideBin: (x: unknown) => x })); | ||
|
||
describe("Cli", () => { | ||
describe("parseArgs", () => { | ||
test("parsing the first argument as the projectName", async () => { | ||
const result = await parseArgs(["my-project"]); | ||
expect(result.projectName).toBe("my-project"); | ||
}); | ||
|
||
test("not parsing first argument as the projectName if it is after --", async () => { | ||
const result = await parseArgs(["--", "my-project"]); | ||
expect(result.projectName).toBeFalsy(); | ||
}); | ||
|
||
test("parsing optional C3 arguments correctly", async () => { | ||
const result = await parseArgs(["--framework", "angular", "--ts=true"]); | ||
expect(result.projectName).toBeFalsy(); | ||
expect(result.framework).toEqual("angular"); | ||
expect(result.ts).toEqual(true); | ||
expect(result.additionalArgs).toEqual([]); | ||
}); | ||
|
||
test("parsing positional + optional C3 arguments correctly", async () => { | ||
const result = await parseArgs([ | ||
"my-project", | ||
"--framework", | ||
"angular", | ||
"--deploy", | ||
"true", | ||
"--git=false", | ||
]); | ||
expect(result.projectName).toEqual("my-project"); | ||
expect(result.framework).toEqual("angular"); | ||
expect(result.deploy).toEqual(true); | ||
expect(result.git).toEqual(false); | ||
expect(result.additionalArgs).toEqual([]); | ||
}); | ||
|
||
test("parsing optional C3 arguments + additional arguments correctly", async () => { | ||
const result = await parseArgs([ | ||
"--framework", | ||
"react", | ||
"--ts=true", | ||
"--", | ||
"positional-arg", | ||
"--react-option", | ||
"5", | ||
]); | ||
expect(result.projectName).toBeFalsy(); | ||
expect(result.framework).toEqual("react"); | ||
expect(result.ts).toEqual(true); | ||
expect(result.additionalArgs).toEqual([ | ||
"positional-arg", | ||
"--react-option", | ||
"5", | ||
]); | ||
}); | ||
|
||
test("parsing positional + optional C3 arguments + additional arguments correctly", async () => { | ||
const result = await parseArgs([ | ||
"my-react-project", | ||
"--framework", | ||
"react", | ||
"--ts=true", | ||
"--", | ||
"positional-arg", | ||
"--react-option", | ||
"5", | ||
]); | ||
expect(result.projectName).toBe("my-react-project"); | ||
expect(result.framework).toEqual("react"); | ||
expect(result.ts).toEqual(true); | ||
expect(result.additionalArgs).toEqual([ | ||
"positional-arg", | ||
"--react-option", | ||
"5", | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import yargs from "yargs"; | ||
import { hideBin } from "yargs/helpers"; | ||
import { version } from "../../package.json"; | ||
import { C3_DEFAULTS, WRANGLER_DEFAULTS, logRaw } from "./cli"; | ||
import { getRenderers, inputPrompt } from "./interactive"; | ||
import type { PromptConfig } from "./interactive"; | ||
import type { C3Args } from "types"; | ||
|
||
export const parseArgs = async (argv: string[]): Promise<Partial<C3Args>> => { | ||
const doubleDashesIdx = argv.indexOf("--"); | ||
const c3Args = argv.slice( | ||
0, | ||
doubleDashesIdx < 0 ? undefined : doubleDashesIdx | ||
); | ||
const additionalArgs = | ||
doubleDashesIdx < 0 ? [] : argv.slice(doubleDashesIdx + 1); | ||
|
||
const args = await yargs(hideBin(c3Args)) | ||
.scriptName("create-cloudflare") | ||
.usage("$0 [args]") | ||
.positional("name", { type: "string" }) | ||
.option("type", { type: "string" }) | ||
.option("framework", { type: "string" }) | ||
.option("deploy", { type: "boolean" }) | ||
.option("ts", { type: "boolean" }) | ||
.option("git", { type: "boolean" }) | ||
.option("open", { | ||
type: "boolean", | ||
default: true, | ||
description: | ||
"opens your browser after your deployment, set --no-open to disable", | ||
}) | ||
.option("existing-script", { | ||
type: "string", | ||
hidden: true, // templateMap["pre-existing"].hidden, | ||
}) | ||
.option("accept-defaults", { | ||
alias: "y", | ||
type: "boolean", | ||
}) | ||
.option("wrangler-defaults", { type: "boolean", hidden: true }) | ||
.version(version) | ||
.help().argv; | ||
|
||
return { | ||
...(args.wranglerDefaults && WRANGLER_DEFAULTS), | ||
...(args.acceptDefaults && C3_DEFAULTS), | ||
projectName: args._[0] as string | undefined, | ||
additionalArgs, | ||
...args, | ||
}; | ||
}; | ||
|
||
export const processArgument = async <T>( | ||
args: Partial<C3Args>, | ||
name: keyof C3Args, | ||
promptConfig: PromptConfig | ||
) => { | ||
let value = args[name]; | ||
const renderSubmitted = getRenderers(promptConfig).submit; | ||
|
||
// If the value has already been set via args, use that | ||
if (value !== undefined) { | ||
promptConfig.validate?.(value); | ||
|
||
const lines = renderSubmitted({ value }); | ||
logRaw(lines.join("\n")); | ||
|
||
return value as T; | ||
} | ||
|
||
value = await inputPrompt(promptConfig); | ||
|
||
return value as T; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.