From 5b201cbe7da34bc1b7564b4717cce4c1e8488078 Mon Sep 17 00:00:00 2001 From: Oskar Sommer Date: Thu, 11 Jul 2024 14:13:07 +0200 Subject: [PATCH] prompt for template prompts package seems to mess up the stdin, vite selection doesn't really work after interactively picking the vite option, need to investigate --- src/constants.ts | 10 +++++++--- src/index.ts | 17 +++++++++++------ src/template-prompt.ts | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 src/template-prompt.ts diff --git a/src/constants.ts b/src/constants.ts index 467b7ea..6e7d8b7 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,6 +1,6 @@ import { blue, bold, gray, magenta, yellow } from "kolorist" -type Template = { +export type TemplateBuiltin = { name: string folder: string color: (str: string | number) => string @@ -8,9 +8,13 @@ type Template = { entry: string[] alias?: string } -type TemplateVite = Omit & { vite: true } +export type TemplateVite = Omit & { + vite: true +} + +export type Template = TemplateBuiltin | TemplateVite -export const templates: (Template | TemplateVite)[] = [ +export const templates: Template[] = [ { name: "typescript", alias: "ts", diff --git a/src/index.ts b/src/index.ts index aad5102..c9d2036 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,8 @@ import { fileURLToPath } from "node:url" import temporaryPath from "temporary-path" import prompts from "prompts" -import { helpMessage, templates } from "./constants" +import { type Template, helpMessage, templates } from "./constants" +import { templatePrompt } from "./template-prompt" import { separator, spawnerInstance } from "./util" const __dirname = dirname(fileURLToPath(import.meta.url)) @@ -31,7 +32,7 @@ const argv = minimist<{ string: ["_"], }) -if (argv.help || !argv.template) { +if (argv.help) { console.log(helpMessage) process.exit(0) } @@ -41,9 +42,12 @@ if (argv.delete && argv.keep) { process.exit(1) } -const template = templates.find( - (t) => t.name === argv.template || t.alias === argv.template, -) +let template: Template | undefined +if (argv.template) { + template = templates.find((t) => t.name === argv.template || t.alias === argv.template) +} else { + template = await templatePrompt() +} if (!template) { console.log(red(bold(`Template ${black(argv.template ?? "")} not found`))) @@ -143,7 +147,8 @@ if (!argv.delete && !argv.keep) { } if (keep) { - console.log(`The folder is kept at ${tempFolder}`) + console.log(gray(`The folder is kept at ${tempFolder}`)) } else { await rm(tempFolder, { recursive: true, force: true }) + console.log(gray(`Deleted ${tempFolder}`)) } diff --git a/src/template-prompt.ts b/src/template-prompt.ts new file mode 100644 index 0000000..b03a592 --- /dev/null +++ b/src/template-prompt.ts @@ -0,0 +1,16 @@ +import prompts from "prompts" +import { type Template, templates } from "./constants" + +export const templatePrompt = async () => { + const prompt = await prompts({ + type: "select", + name: "template", + message: "Select a template", + choices: templates.map((t) => ({ + title: t.color(t.name), + value: t, + })), + }) + + return prompt.template as Template +}