UNPKG

tamim-cli

Version:

A CLI tool for generating module boilerplate code including routes, controllers, services, and more

140 lines (139 loc) โ€ข 6.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = newApp; const inquirer_1 = __importDefault(require("inquirer")); const child_process_1 = require("child_process"); const fs_1 = require("fs"); const path_1 = require("path"); async function newApp() { const questions = [ { type: "input", name: "name", message: "What is the name of your new app?", validate: (input) => { if (input.trim() === "") { return "Please enter a valid name."; } return true; }, }, { type: "list", name: "packageManager", message: "Which package manager do you want to use?", choices: ["npm", "yarn", "pnpm", "bun"], }, ]; const answers = await inquirer_1.default.prompt(questions); // Handle current directory case const name = answers.name.trim(); const isCurrentDir = name === "."; const destination = isCurrentDir ? process.cwd() : (0, path_1.join)(process.cwd(), name); // Check if destination exists and is not empty (for current directory) if ((0, fs_1.existsSync)(destination)) { if (isCurrentDir) { const files = require("fs").readdirSync(destination); // Filter out hidden files and common files that might exist const relevantFiles = files.filter((file) => !file.startsWith(".") && !["node_modules", "package.json"].includes(file)); if (relevantFiles.length > 0) { console.error("\nโŒ Error: The current directory is not empty"); console.error(" Please use an empty directory or specify a new directory name\n"); return; } } else { console.error(`\nโŒ Error: The directory '${name}' already exists`); console.error(" Please choose another name\n"); return; } } const gitUrl = "https://github.com/Tamim-369/tamim-template.git"; try { console.log("\n๐Ÿš€ Initializing new project..."); // Clone directly using the provided name console.log(`\n๐Ÿ“ฆ Downloading template...`); (0, child_process_1.spawnSync)("git", ["clone", gitUrl, name], { stdio: "pipe", // Change to pipe to suppress git output cwd: process.cwd(), }); console.log(`โœ… Template downloaded successfully${isCurrentDir ? " to current directory" : ` to ./${name}`}`); // Remove .git folder with proper error handling const gitFolder = (0, path_1.join)(destination, ".git"); if ((0, fs_1.existsSync)(gitFolder)) { try { console.log("\n๐Ÿ”ง Preparing project structure..."); // First attempt: Use rmSync with recursive option (0, fs_1.rmSync)(gitFolder, { recursive: true, force: true }); } catch (error) { try { // Second attempt: Use git command to clean up (works better on Windows) (0, child_process_1.spawnSync)("git", ["clean", "-fxd"], { stdio: "pipe", cwd: destination, }); // Remove .git directory using git command (0, child_process_1.spawnSync)("git", ["checkout-index", "-a", "-f", "--prefix=./"], { stdio: "pipe", cwd: destination, }); (0, fs_1.rmSync)(gitFolder, { recursive: true, force: true }); } catch (innerError) { console.warn("\nโš ๏ธ Warning: Git folder cleanup incomplete"); console.warn(" You may need to remove the .git folder manually\n"); } } } const packageManager = answers.packageManager; // Remove lock files based on chosen package manager const lockFiles = { npm: ["yarn.lock", "pnpm-lock.yaml", "bun.lockb"], yarn: ["package-lock.json", "pnpm-lock.yaml", "bun.lockb"], pnpm: ["package-lock.json", "yarn.lock", "bun.lockb"], bun: ["package-lock.json", "yarn.lock", "pnpm-lock.yaml"], }; // Remove unnecessary lock files console.log(`\n๐Ÿ“ Configuring ${packageManager}...`); lockFiles[packageManager].forEach((file) => { (0, fs_1.rmSync)((0, path_1.join)(destination, file), { force: true }); }); // Install dependencies with chosen package manager const installCommands = { npm: "npm install", yarn: "yarn install", pnpm: "pnpm install", bun: "bun install", }; console.log("\n๐Ÿ“ฅ Installing dependencies..."); (0, child_process_1.spawnSync)(installCommands[packageManager], { stdio: "pipe", cwd: destination, shell: true, }); console.log("\nโœจ Success! Your new project is ready."); console.log(`\n๐Ÿ“ Project location: ${isCurrentDir ? "current directory" : `./${name}`}`); console.log("\n๐ŸŽ‰ You can now start developing your application:"); if (!isCurrentDir) { console.log(` cd ${name}`); } console.log(` ${packageManager}${packageManager === "npm" ? " run" : ""} dev\n`); } catch (error) { console.error("\nโŒ Error: Failed to initialize project"); console.error(` ${isCurrentDir ? "Could not setup current directory" : `Could not create ${name}`}`); console.error(` ${error}\n`); // Clean up the destination directory if it was created if (!isCurrentDir && (0, fs_1.existsSync)(destination)) { console.log("๐Ÿงน Cleaning up..."); (0, fs_1.rmSync)(destination, { recursive: true, force: true }); } } }