|
| 1 | +#!/usr/bin/env -S deno run --ext=ts -A |
| 2 | +// deno-lint-ignore-file no-import-prefix |
| 3 | +import * as fs from "jsr:@std/fs@^1"; |
| 4 | +import * as path from "jsr:@std/path@^1"; |
| 5 | +import { homedir } from "node:os"; |
| 6 | +import { |
| 7 | + intro, |
| 8 | + isCancel, |
| 9 | + log, |
| 10 | + note, |
| 11 | + outro, |
| 12 | + select, |
| 13 | + tasks, |
| 14 | + text, |
| 15 | + type Task, |
| 16 | +} from "npm:@clack/prompts@^0.11.0"; |
| 17 | +import { detectDefaultShell } from "npm:default-shell@^2.2.0"; |
| 18 | +import color from "npm:picocolors@^1.1.1"; |
| 19 | +import { denoJson, helloWorldScript, projectTemplate } from "./_utils/generate-project.ts"; |
| 20 | +import { initEditorEnv, initServerEnv } from "./_utils/init-env.ts"; |
| 21 | + |
| 22 | +const DREAMLAB_ROOT = path.join(path.fromFileUrl(import.meta.url), "../.."); |
| 23 | + |
| 24 | +type Template = { |
| 25 | + readonly label: string; |
| 26 | + readonly directory: string; |
| 27 | + readonly repo: string | undefined; |
| 28 | + readonly hint?: string; |
| 29 | +}; |
| 30 | + |
| 31 | +const TEMPLATES: [Template, ...(readonly Template[])] = [ |
| 32 | + { |
| 33 | + label: "OpenMonsters", |
| 34 | + directory: "openmonsters", |
| 35 | + repo: "https://github.com/WorldQL/openmonsters.git", |
| 36 | + hint: "recommended", |
| 37 | + }, |
| 38 | + { |
| 39 | + label: "Blank", |
| 40 | + directory: "dreamlab-project", |
| 41 | + repo: undefined, |
| 42 | + }, |
| 43 | +]; |
| 44 | + |
| 45 | +async function task(title: string, task: Task["task"]) { |
| 46 | + await tasks([{ title, task }]); |
| 47 | +} |
| 48 | + |
| 49 | +if (import.meta.main) { |
| 50 | + let updatedShell: "bash" | "zsh" | undefined; |
| 51 | + // detect shell type and inject `wql` alias |
| 52 | + try { |
| 53 | + const shell = detectDefaultShell(); |
| 54 | + |
| 55 | + // TODO: other shell types? |
| 56 | + if (shell === "/bin/bash" || shell === "/bin/zsh") { |
| 57 | + const rcFile = (() => { |
| 58 | + const zDotDir = Deno.env.get("ZDOTDIR"); |
| 59 | + if (shell === "/bin/zsh" && zDotDir) { |
| 60 | + return path.join(zDotDir, ".zshrc"); |
| 61 | + } |
| 62 | + |
| 63 | + const rc = shell === "/bin/bash" ? ".bashrc" : ".zshrc"; |
| 64 | + return path.join(homedir(), rc); |
| 65 | + })(); |
| 66 | + |
| 67 | + const content = await Deno.readTextFile(rcFile); |
| 68 | + using rc = await Deno.open(rcFile, { read: true, append: true }); |
| 69 | + |
| 70 | + if (!content.includes(`alias wql='`)) { |
| 71 | + const writer = rc.writable.getWriter(); |
| 72 | + await writer.ready; |
| 73 | + |
| 74 | + const encoder = new TextEncoder(); |
| 75 | + if (!content.endsWith("\n")) await writer.write(encoder.encode("\n")); |
| 76 | + await writer.write(encoder.encode(`export DREAMLAB_DIR="${DREAMLAB_ROOT}"\n`)); |
| 77 | + await writer.write( |
| 78 | + encoder.encode(`alias wql='deno run -A "$DREAMLAB_DIR/scripts/wql.ts"'\n`), |
| 79 | + ); |
| 80 | + |
| 81 | + await writer.ready; |
| 82 | + await writer.close(); |
| 83 | + |
| 84 | + updatedShell = shell === "/bin/bash" ? "bash" : "zsh"; |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (_error) { |
| 88 | + // uncomment when debugging |
| 89 | + // console.error(_error); |
| 90 | + } |
| 91 | + |
| 92 | + intro(color.bgCyan(" WorldQL Setup ")); |
| 93 | + |
| 94 | + await task("Initializing Dreamlab environment", async () => { |
| 95 | + await Promise.all([initEditorEnv(DREAMLAB_ROOT), initServerEnv(DREAMLAB_ROOT)]); |
| 96 | + return "Initialized Dreamlab environment"; |
| 97 | + }); |
| 98 | + |
| 99 | + const template = await select({ |
| 100 | + message: "Pick a project template:", |
| 101 | + options: TEMPLATES.map( |
| 102 | + template => ({ label: template.label, value: template, hint: template.hint }) as const, |
| 103 | + ), |
| 104 | + }); |
| 105 | + if (isCancel(template)) Deno.exit(1); |
| 106 | + |
| 107 | + let directory: string | symbol | undefined = await text({ |
| 108 | + message: "Project directory name:", |
| 109 | + placeholder: template.directory, |
| 110 | + validate: value => { |
| 111 | + const dir = path.join(Deno.cwd(), value === "" ? template.directory : value); |
| 112 | + const exists = fs.existsSync(dir); |
| 113 | + if (exists) return "Already exists"; |
| 114 | + |
| 115 | + return undefined; |
| 116 | + }, |
| 117 | + }); |
| 118 | + if (isCancel(directory)) Deno.exit(1); |
| 119 | + |
| 120 | + directory ??= template.directory; |
| 121 | + const fullPath = path.join(Deno.cwd(), directory); |
| 122 | + |
| 123 | + // TODO: show a confirmation step? |
| 124 | + // const shoudClone = await confirm({ |
| 125 | + // message: "", |
| 126 | + // }); |
| 127 | + // if (isCancel(shoudClone)) Deno.exit(1); |
| 128 | + // if (!shouldClone) { |
| 129 | + // cancel('Operation cancelled') |
| 130 | + // Deno.exit(0) |
| 131 | + // } |
| 132 | + |
| 133 | + if (template.repo === undefined) { |
| 134 | + await task("Creating blank project", async () => { |
| 135 | + await fs.emptyDir(fullPath); |
| 136 | + await Deno.writeTextFile( |
| 137 | + path.join(fullPath, "project.json"), |
| 138 | + JSON.stringify(projectTemplate(), null, 2) + "\n", |
| 139 | + ); |
| 140 | + await Deno.writeTextFile( |
| 141 | + path.join(fullPath, "deno.json"), |
| 142 | + JSON.stringify(denoJson(DREAMLAB_ROOT), null, 2) + "\n", |
| 143 | + ); |
| 144 | + |
| 145 | + await fs.ensureDir(path.join(fullPath, "src")); |
| 146 | + await Deno.writeTextFile(path.join(fullPath, "src", "hello-world.ts"), helloWorldScript); |
| 147 | + |
| 148 | + return "Created blank project"; |
| 149 | + }); |
| 150 | + } else { |
| 151 | + const repo = template.repo; |
| 152 | + await task(`Cloning Template: "${template.label}"`, async () => { |
| 153 | + const cmd = new Deno.Command("git", { |
| 154 | + args: ["clone", "--depth=1", repo, fullPath], |
| 155 | + }); |
| 156 | + |
| 157 | + const result = await cmd.output(); |
| 158 | + if (!result.success) { |
| 159 | + log.error("Failed to clone template!"); |
| 160 | + Deno.exit(1); |
| 161 | + } |
| 162 | + |
| 163 | + // clear git history |
| 164 | + await Deno.remove(path.join(fullPath, ".git"), { recursive: true }); |
| 165 | + |
| 166 | + // write correct deno.json |
| 167 | + await Deno.writeTextFile( |
| 168 | + path.join(fullPath, "deno.json"), |
| 169 | + JSON.stringify(denoJson(DREAMLAB_ROOT), null, 2) + "\n", |
| 170 | + ); |
| 171 | + |
| 172 | + return `Cloned Template: "${template.label}"`; |
| 173 | + }); |
| 174 | + |
| 175 | + // TODO: prompt to initialize a fresh git repo? |
| 176 | + } |
| 177 | + |
| 178 | + const notes: (string | false)[] = [ |
| 179 | + updatedShell === "bash" && "$ source ~/.bashrc", |
| 180 | + updatedShell === "zsh" && "", // TODO: shell update for zsh |
| 181 | + |
| 182 | + `$ cd ${directory} && wql up`, |
| 183 | + ]; |
| 184 | + note(notes.filter(line => line !== false).join("\n"), "Next steps:"); |
| 185 | + |
| 186 | + outro(`You're good to go!`); |
| 187 | + Deno.exit(0); |
| 188 | +} |
0 commit comments