@excli/express
Version:
A cli tool for creating Express.js applications, supporting both JavaScript and TypeScript.
134 lines โข 5.55 kB
JavaScript
#!/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