Skip to content

Commit

Permalink
prompt for template
Browse files Browse the repository at this point in the history
prompts package seems to mess up the stdin, vite selection doesn't really work after interactively picking the vite option, need to investigate
  • Loading branch information
vaaski committed Jul 11, 2024
1 parent 7692c24 commit 5b201cb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { blue, bold, gray, magenta, yellow } from "kolorist"

type Template = {
export type TemplateBuiltin = {
name: string
folder: string
color: (str: string | number) => string
editorEntry: string
entry: string[]
alias?: string
}
type TemplateVite = Omit<Template, "folder" | "entry" | "editorEntry"> & { vite: true }
export type TemplateVite = Omit<TemplateBuiltin, "folder" | "entry" | "editorEntry"> & {
vite: true
}

export type Template = TemplateBuiltin | TemplateVite

export const templates: (Template | TemplateVite)[] = [
export const templates: Template[] = [
{
name: "typescript",
alias: "ts",
Expand Down
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -31,7 +32,7 @@ const argv = minimist<{
string: ["_"],
})

if (argv.help || !argv.template) {
if (argv.help) {
console.log(helpMessage)
process.exit(0)
}
Expand All @@ -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`)))
Expand Down Expand Up @@ -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}`))
}
16 changes: 16 additions & 0 deletions src/template-prompt.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 5b201cb

Please sign in to comment.