Files
create-glstack/index.ts
T
2026-10-07 13:00:58 +02:00

100 lines
3.0 KiB
TypeScript

#!/usr/bin/env bun
import * as p from "@clack/prompts";
import { initRenderer } from '@gregorlohaus/tdir'
import { z } from 'zod'
import path from "node:path";
p.intro(`create-glstack ${process.env.GLSTACK_DEV && 'isDev'}`);
const project = await p.group(
{
name: () =>
p.text({
message: "What would you like to name your project?",
placeholder: "glstack-test",
defaultValue: "glstack-test",
validate: (value) => {
return undefined
},
}),
frontend: async () =>
await p.select({
message: 'Pick a frontend framework.',
options: [
{ value: "svelte-kit", label: "SvelteKit" },
{ value: "solid-start", label: "SolidStart" },
{ value: "none", label: "None" }
]
})
,
mobile: async () =>
await p.select({
message: 'Pick a mobile framework.',
options: [
{ value: "expo", label: "ReactNative + Expo" },
{ value: "none", label: "None" }
]
})
,
bundleid: async ({ results }) => {
if (results.mobile !== "expo") return ""
// Valid as both an iOS bundle identifier and an Android package name.
const fallback = `com.${String(results.name).toLowerCase().replace(/[^a-z0-9]/g, "").replace(/^[0-9]+/, "") || "app"}.mobile`
return await p.text({
message: "What bundle identifier should the mobile app use?",
placeholder: fallback,
defaultValue: fallback,
validate: (value) => {
if (value && !/^[a-zA-Z][a-zA-Z0-9]*(\.[a-zA-Z][a-zA-Z0-9]*)+$/.test(value)) {
return "Use reverse-DNS segments of letters and digits, e.g. com.example.app"
}
return undefined
},
})
},
goprefix: () =>
p.text({
message: "What would you like to use as a go package prefix?",
placeholder: "github.com/glstack-test",
defaultValue: "github.com/glstack-test",
validate: (value) => {
return undefined
},
}),
installDeps: async () => {
return await p.confirm({message: "Install dependencies?", })
}
},
{
onCancel: () => {
p.cancel("Operation cancelled.");
process.exit(0);
},
}
);
const templateDir = (process.env.GLSTACK_DEV == 'true' ? './template' : path.join(import.meta.dir, '..', 'template'))
const createRenderer = initRenderer(templateDir)
const render = createRenderer(z.object({
project: z.object({
name: z.string(),
goprefix: z.string(),
frontend: z.string(),
mobile: z.string(),
bundleid: z.string()
})
}))
const destDir = path.join("./", project.name);
// `results` in the bundleid prompt makes group() infer it as unknown.
render(destDir, { project: { ...project, bundleid: project.bundleid as string } }, { reverseMap: true })
const s = p.spinner();
if (project.installDeps) {
s.start("Installing dependencies");
await Bun.$`bun install`.cwd(path.join(destDir)).quiet();
s.stop("Dependencies installed.");
}
p.outro("You're all set!");