UNPKG

@excli/express

Version:

A cli tool for creating Express.js applications, supporting both JavaScript and TypeScript.

134 lines โ€ข 5.55 kB
#!/usr/bin/env node import { cwd } from "node:process"; import { fileURLToPath } from "node:url"; import { join, basename, dirname } from "node:path"; import { existsSync, mkdirSync, cpSync, writeFileSync } from "node:fs"; import { spinner, isCancel, multiselect } from "@clack/prompts"; import { intro, text, select, outro, log } from "@clack/prompts"; import { hasPkManager } from "./scripts.js"; import { installPackages, sleep } from "./utils.js"; import { git, docker, prettier, env } from "./options.js"; import { terminate, directories, packageJsonInit } from "./utils.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); console.clear(); intro("๐Ÿ”ฅ Express.js App Generator | Build your dreams, faster! โšก"); (async () => { const directory = (await text({ message: "What should we name your server directory? ๐ŸŽฏ", placeholder: "server (Hit Enter for current directory)", })); if (isCancel(directory)) terminate("Process cancelled โŒ"); const rootDir = cwd(); const targetDir = !directory?.trim() ? rootDir : join(rootDir, directory); const dirName = basename(targetDir) || "container_app"; if (existsSync(targetDir) && directory?.trim()) { return terminate(`${directory} already exists. Please choose a different name ๐Ÿคท`); } const language = (await select({ message: "Pick your coding Language:", options: [ { label: "TypeScript", value: "ts", hint: "Recommended ๐Ÿš€" }, { label: "JavaScript", value: "js", hint: "Classic ๐Ÿ’ผ" }, ], })); if (isCancel(language)) terminate("Process cancelled โŒ"); const devTools = (await multiselect({ message: "๐Ÿ”ง Setting up core development tools...", options: [ { label: "โœจ Prettier", value: "prettier" }, { label: "๐Ÿณ Docker (deployment + database)", value: "docker" }, { label: "๐Ÿ“ Git", value: "git" }, ], })); if (isCancel(devTools)) terminate("Process cancelled โŒ"); let db; if (devTools.includes("docker")) { db = (await select({ message: "Alright, pick your database:", options: [ { label: "๐Ÿฌ MySQL", value: "mysql", hint: "Widely used ๐ŸŒ" }, { label: "๐Ÿ˜ PostgreSQL", value: "postgres", hint: "SQL powerhouse โšก", }, { label: "๐Ÿƒ MongoDB", value: "mongodb", hint: "NoSQL flexible ๐Ÿ”„" }, ], })); if (isCancel(db)) terminate("Process cancelled โŒ"); } const pkgManager = (await select({ message: "Which package manager do you want to use?", options: [ { label: "๐Ÿ“‹ npm", value: "npm", hint: "Standard choice ๐Ÿ”ง" }, { label: "๐Ÿงถ yarn", value: "yarn", hint: "Smooth workflow ๐Ÿ’ซ" }, { label: "โšก pnpm", value: "pnpm", hint: "Lightning fast ๐Ÿš€" }, ], })); if (isCancel(pkgManager)) terminate("Process cancelled โŒ"); if (!hasPkManager(pkgManager)) { terminate(`โŒ ${pkgManager} is not installed on your system! Please install it first.`); } const s1 = spinner(); s1.start("๐Ÿ“ Setting up directory structure..."); if (!existsSync(targetDir)) mkdirSync(targetDir, { recursive: true }); const sourceDir = join(targetDir, "src"); const publicDir = join(targetDir, "public"); const template = join(__dirname, "../templates", language); if (!existsSync(template)) terminate(`โŒ Template not found at: ${template}`); mkdirSync(publicDir, { recursive: true }); cpSync(template, targetDir, { recursive: true }); for (let { file, variables } of env()) { const fullPath = join(targetDir, file); writeFileSync(fullPath, variables); } for (let dir of directories) { if (language !== "ts" && dir === "types") continue; const directoryPath = join(sourceDir, dir); mkdirSync(directoryPath, { recursive: true }); } await sleep(1000); s1.stop("โœ… Directory structure created."); const s2 = spinner(); s2.start("Adding Development Tools"); if (devTools.includes("prettier")) { for (let { content, filename } of prettier()) { const fullPath = join(targetDir, filename); writeFileSync(fullPath, content); } } if (devTools.includes("git")) { const gitPath = join(targetDir, ".gitignore"); const { gitignoreContent } = git(); writeFileSync(gitPath, gitignoreContent); } if (devTools.includes("docker") && db) { for (let { content, filename } of docker(db, dirName)) { const fullPath = join(targetDir, filename); writeFileSync(fullPath, content); } } await sleep(1000); s2.stop("โœ… Development Tools Added"); await packageJsonInit(pkgManager, targetDir, language); const s6 = spinner({ indicator: "timer" }); s6.start("๐Ÿ“ฅ Installing dependencies..."); await installPackages(pkgManager, targetDir, language, devTools); await sleep(1000); s6.stop("โœ… Dependencies installed successfully! in:"); log.success(`Scaffolding project in ${targetDir}...`); outro(`๐Ÿš€ You're all set! Thanks for using Express App Generator ๐Ÿ™Œ GitHub โ†’ https://github.com/pxycknomdictator `); })(); //# sourceMappingURL=main.js.map