Skip to content

Commit

Permalink
include vite templates in help message
Browse files Browse the repository at this point in the history
  • Loading branch information
vaaski committed Jul 19, 2024
1 parent 4bd2faa commit 5bc7303
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Currently, there are three templates available:
- `JavaScript`/`js`
- `Vite`/`v`

> [!NOTE]
> [!TIP]
> You can also specify any vite template directly, e.g. `vue-ts`
To use a template, run `localpen` with the `-t` flag:
Expand All @@ -64,6 +64,6 @@ This prompt can be skipped by passing the `--keep` (`-k`) or the `--delete` (`-d
### Todo

- [ ] ensure all the vite templates work
- [ ] list the vite templates in the help message
- [x] list the vite templates in the help message

[bun]: https://bun.sh
25 changes: 22 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { blue, bold, gray, magenta, yellow } from "kolorist"
import { getViteTemplateVariantsColors } from "./create-vite"

export type TemplateBuiltin = {
name: string
Expand Down Expand Up @@ -36,14 +37,28 @@ export const templates: Template[] = [
entry: ["bun", "run", "--watch", "index.js"],
},
{
name: "create-vite",
name: "Vite",
alias: "v",
color: magenta,
vite: true,
},
]

export const helpMessage = `
export const helpMessage = async () => {
const viteTemplateStrings = []
const viteVariants = await getViteTemplateVariantsColors()

for (const [name, data] of viteVariants) {
let variantString = data.color(name)

for (const [variant, color] of data.variants) {
if (variant === "TypeScript") variantString += ` (-${color("ts")})`
}

viteTemplateStrings.push(variantString)
}

return `
Usage: localpen [options]
${gray("Options:")}
Expand All @@ -57,4 +72,8 @@ ${gray("Deletion options, pick either or get prompted:")}
Available templates:
${templates
.map((t) => ` - ${t.color(t.name)}${t.alias ? `/${t.color(t.alias)}` : ""}`)
.join("\n")}`
.join("\n")}
Vite templates:
${viteTemplateStrings.map((t) => ` - ${t}`).join("\n")}`
}
4 changes: 2 additions & 2 deletions src/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import prompts from "prompts"
const __dirname = dirname(fileURLToPath(import.meta.url))
export const VITE_TEMPLATES_FOLDER = join(__dirname, "../templates/vite")

const COLOR_STARTS_WITH = Object.entries({
export const VITE_COLOR_STARTS_WITH = Object.entries({
vanilla: colors.yellow,
vue: colors.green,
react: colors.cyan,
Expand All @@ -35,7 +35,7 @@ export const getViteTemplateVariantsColors = async (): Promise<
const map: ViteTemplateMap = {}

for (const viteTemplate of contents) {
const colorMatch = COLOR_STARTS_WITH.find(([color]) =>
const colorMatch = VITE_COLOR_STARTS_WITH.find(([color]) =>
viteTemplate.startsWith(color),
)
const color = colorMatch ? colorMatch[1] : colors.gray
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const argv = minimist<{
})

if (argv.help) {
console.log(helpMessage)
console.log(await helpMessage())
process.exit(0)
}

Expand Down Expand Up @@ -86,7 +86,7 @@ if (argv.template) {

if (!template) {
console.log(red(bold(`Template ${black(argv.template ?? "")} not found`)))
console.log(`${helpMessage}`)
console.log(await helpMessage())
process.exit(1)
}

Expand Down

0 comments on commit 5bc7303

Please sign in to comment.