From 77e4313363d9c5664dc58e7a7001872d68df92a8 Mon Sep 17 00:00:00 2001 From: Tim Leung Date: Wed, 7 Feb 2024 11:10:58 +0000 Subject: [PATCH] Refactor create-app prompts with tests (#32) --- .../changelog/@unreleased/pr-32.v2.yml | 5 + packages/create-app/src/cli.ts | 226 +----------------- .../src/prompts/promptApplicationUrl.test.ts | 76 ++++++ .../src/prompts/promptApplicationUrl.ts | 63 +++++ .../src/prompts/promptClientId.test.ts | 49 ++++ .../create-app/src/prompts/promptClientId.ts | 37 +++ .../src/prompts/promptFoundryUrl.test.ts | 59 +++++ .../src/prompts/promptFoundryUrl.ts | 37 +++ .../src/prompts/promptOsdkPackage.test.ts | 54 +++++ .../src/prompts/promptOsdkPackage.ts | 37 +++ .../src/prompts/promptOsdkRegistryUrl.test.ts | 63 +++++ .../src/prompts/promptOsdkRegistryUrl.ts | 45 ++++ .../src/prompts/promptOverwrite.test.ts | 52 ++++ .../create-app/src/prompts/promptOverwrite.ts | 66 +++++ .../src/prompts/promptProject.test.ts | 53 ++++ .../create-app/src/prompts/promptProject.ts | 36 +++ .../src/prompts/promptTemplate.test.ts | 47 ++++ .../create-app/src/prompts/promptTemplate.ts | 52 ++++ 18 files changed, 841 insertions(+), 216 deletions(-) create mode 100644 packages/create-app/changelog/@unreleased/pr-32.v2.yml create mode 100644 packages/create-app/src/prompts/promptApplicationUrl.test.ts create mode 100644 packages/create-app/src/prompts/promptApplicationUrl.ts create mode 100644 packages/create-app/src/prompts/promptClientId.test.ts create mode 100644 packages/create-app/src/prompts/promptClientId.ts create mode 100644 packages/create-app/src/prompts/promptFoundryUrl.test.ts create mode 100644 packages/create-app/src/prompts/promptFoundryUrl.ts create mode 100644 packages/create-app/src/prompts/promptOsdkPackage.test.ts create mode 100644 packages/create-app/src/prompts/promptOsdkPackage.ts create mode 100644 packages/create-app/src/prompts/promptOsdkRegistryUrl.test.ts create mode 100644 packages/create-app/src/prompts/promptOsdkRegistryUrl.ts create mode 100644 packages/create-app/src/prompts/promptOverwrite.test.ts create mode 100644 packages/create-app/src/prompts/promptOverwrite.ts create mode 100644 packages/create-app/src/prompts/promptProject.test.ts create mode 100644 packages/create-app/src/prompts/promptProject.ts create mode 100644 packages/create-app/src/prompts/promptTemplate.test.ts create mode 100644 packages/create-app/src/prompts/promptTemplate.ts diff --git a/packages/create-app/changelog/@unreleased/pr-32.v2.yml b/packages/create-app/changelog/@unreleased/pr-32.v2.yml new file mode 100644 index 000000000..49727a65d --- /dev/null +++ b/packages/create-app/changelog/@unreleased/pr-32.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Refactor create-app prompts and add unit tests + links: + - https://github.com/palantir/osdk-ts/pull/32 diff --git a/packages/create-app/src/cli.ts b/packages/create-app/src/cli.ts index 9614f6a54..1db28479f 100644 --- a/packages/create-app/src/cli.ts +++ b/packages/create-app/src/cli.ts @@ -28,9 +28,16 @@ import { generateEnvProduction, } from "./generate/generateEnv.js"; import { generateNpmRc } from "./generate/generateNpmRc.js"; -import { green, italic } from "./highlight.js"; +import { green } from "./highlight.js"; +import { promptApplicationUrl } from "./prompts/promptApplicationUrl.js"; +import { promptClientId } from "./prompts/promptClientId.js"; +import { promptFoundryUrl } from "./prompts/promptFoundryUrl.js"; +import { promptOsdkPackage } from "./prompts/promptOsdkPackage.js"; +import { promptOsdkRegistryUrl } from "./prompts/promptOsdkRegistryUrl.js"; +import { promptOverwrite } from "./prompts/promptOverwrite.js"; +import { promptProject } from "./prompts/promptProject.js"; +import { promptTemplate } from "./prompts/promptTemplate.js"; import type { Template, TemplateContext } from "./templates.js"; -import { TEMPLATES } from "./templates.js"; interface CliArgs { project?: string; @@ -95,7 +102,7 @@ export async function cli(args: string[] = process.argv) { const parsed: CliArgs = base.parseSync(); const project: string = await promptProject(parsed); - const overwrite: boolean = await promptOverwrite(parsed, project); + const overwrite: boolean = await promptOverwrite({ ...parsed, project }); const template: Template = await promptTemplate(parsed); const foundryUrl: string = await promptFoundryUrl(parsed); const applicationUrl: string | undefined = await promptApplicationUrl(parsed); @@ -194,216 +201,3 @@ export async function cli(args: string[] = process.argv) { }, }); } - -async function promptProject(parsed: CliArgs): Promise { - let project = parsed.project; - while (project == null || !/^[a-zA-Z0-9-_]+$/.test(project)) { - if (project != null) { - consola.fail( - "Project name can only contain alphanumeric characters, hyphens and underscores", - ); - } - project = await consola.prompt("Project name:", { - type: "text", - placeholder: "my-osdk-app", - default: "my-osdk-app", - }); - } - - return project; -} - -async function promptOverwrite( - parsed: CliArgs, - project: string, -): Promise { - if (!fs.existsSync(path.join(process.cwd(), project))) { - return true; - } - - if (parsed.overwrite != null) { - return parsed.overwrite; - } - - const result = (await consola.prompt( - `The directory ${ - green( - project, - ) - } already exists do you want to overwrite or ignore it?`, - { - type: "select", - options: [ - { label: "Remove existing files and continue", value: "overwrite" }, - { label: "Ignore files and continue", value: "ignore" }, - { label: "Cancel", value: "cancel" }, - ], - }, - // Types for "select" are wrong the value is returned rather than the option object - // https://github.com/unjs/consola/pull/238 - )) as unknown as "overwrite" | "ignore" | "cancel"; - - switch (result) { - case "overwrite": - return true; - case "ignore": - return false; - case "cancel": - consola.fail("Operation cancelled"); - process.exit(0); - } -} - -async function promptTemplate(parsed: CliArgs): Promise