UNPKG

create-lestin

Version:
116 lines (112 loc) 3.47 kB
#!/usr/bin/env node // src/index.ts import fs from "fs/promises"; import Path from "path"; import { fileURLToPath } from "url"; import { execSync } from "child_process"; import { Command } from "commander"; // package.json var package_default = { name: "create-lestin", version: "0.0.2", description: "Creates a new project with Lestin", keywords: [ "create", "lestin" ], homepage: "https://github.com/movahhedi/create-lestin", bugs: { url: "https://github.com/movahhedi/create-lestin" }, repository: { type: "git", url: "https://github.com/movahhedi/create-lestin" }, license: "MIT", author: "Shahab Movahhedi", type: "module", exports: { ".": { import: "./dist/index.js", require: "./dist/index.cjs", default: "./dist/index.js" } }, bin: "./dist/index.js", main: "./dist/index.js", types: "./dist/index.d.ts", files: [ "templates", "dist" ], scripts: { build: "tsup --format esm,cjs --clean --dts", prepublishOnly: "yarn build", update: "npx npm-check-updates -i" }, dependencies: { commander: "^12.1.0" }, devDependencies: { "@types/node": "^20.14.11", "npm-check-updates": "^16.14.20", tsup: "^8.2.1", typescript: "^5.5.3" }, packageManager: "yarn@4.6.0" }; // src/index.ts var __filename = fileURLToPath(import.meta.url); var __dirname = Path.dirname(fileURLToPath(import.meta.url)); var program = new Command(); program.name("create-lestin").description("Create a new project with Lestin").version(package_default.version, "-v, -V, --version", "create-lestin's version").argument("<dir-name>", "Directory of the project").option("-f, --force", "Force create a project in a non-empty directory").option("-G, --no-git", "Don't initialize a git repository").option("-i, --install", "Install dependencies after creating the project").action(async (dirNameRaw) => { const options = program.opts(); const dirName = Path.resolve(dirNameRaw); try { const doesDirExist = await fs.access(dirName); const isDirFull = (await fs.readdir(dirName)).length > 0; if (isDirFull) { console.error( "The directory is not empty. To force create a project in a non-empty directory, use the `--force` flag." ); return; } } catch (error) { } console.log("Creating project in the directory:", dirName, "\n"); await fs.mkdir(dirName, { recursive: true }); await fs.cp(Path.resolve(__dirname, "../templates/hono-client-server"), dirName, { recursive: true }); const dirFiles = await fs.readdir(dirName, { recursive: true }); dirFiles.forEach(async (file) => { const filePath = Path.resolve(dirName, file); if (filePath.endsWith("CREATECOMMAND")) { await fs.rename(filePath, filePath.replace("CREATECOMMAND", "")); } }); if (!options.noGit) { console.log("Initializing git repository..."); execSync("git init", { cwd: dirName, stdio: "inherit" }); } if (options.install) { console.log("Installing dependencies..."); execSync("yarn install", { cwd: dirName, stdio: "inherit" }); } console.log("Project created successfully at the directory:", dirName, "\n"); console.log("To start the project, run the following commands:\n"); if (dirNameRaw !== ".") { console.log(` cd ${dirNameRaw}`); } console.log(" yarn install"); console.log("\nHappy coding!"); }); program.parse(process.argv);