|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import consola from "consola"; |
| 3 | +import fs from "fs-extra"; |
| 4 | +import inquirer from "inquirer"; |
| 5 | +import path from "path"; |
| 6 | + |
| 7 | +async function detectPackageManager(): Promise<string | undefined> { |
| 8 | + const userAgent = process.env.npm_config_user_agent; |
| 9 | + if (userAgent) { |
| 10 | + if (userAgent.startsWith("bun")) { |
| 11 | + return "bun"; |
| 12 | + } |
| 13 | + if (userAgent.startsWith("npm")) { |
| 14 | + return "npm"; |
| 15 | + } |
| 16 | + if (userAgent.startsWith("yarn")) { |
| 17 | + return "yarn"; |
| 18 | + } |
| 19 | + if (userAgent.startsWith("pnpm")) { |
| 20 | + return "pnpm"; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + execSync("bun --version", { stdio: "ignore" }); |
| 26 | + return "bun"; |
| 27 | + } catch (e) {} |
| 28 | + try { |
| 29 | + execSync("npm --version", { stdio: "ignore" }); |
| 30 | + return "npm"; |
| 31 | + } catch (e) {} |
| 32 | + try { |
| 33 | + execSync("yarn --version", { stdio: "ignore" }); |
| 34 | + return "yarn"; |
| 35 | + } catch (e) {} |
| 36 | + try { |
| 37 | + execSync("pnpm --version", { stdio: "ignore" }); |
| 38 | + return "pnpm"; |
| 39 | + } catch (e) {} |
| 40 | + |
| 41 | + return undefined; |
| 42 | +} |
| 43 | + |
| 44 | +async function copyTemplate(templatePath: string, projectPath: string) { |
| 45 | + const files = [ |
| 46 | + "clients", |
| 47 | + "commands", |
| 48 | + "events", |
| 49 | + "utils", |
| 50 | + ".env.example", |
| 51 | + ".gitignore", |
| 52 | + "index.ts", |
| 53 | + "package.json", |
| 54 | + "tsconfig.json", |
| 55 | + ]; |
| 56 | + |
| 57 | + for (const file of files) { |
| 58 | + const sourcePath = path.join(templatePath, file); |
| 59 | + const destPath = path.join(projectPath, file); |
| 60 | + |
| 61 | + try { |
| 62 | + if (fs.lstatSync(sourcePath).isDirectory()) { |
| 63 | + await fs.copy(sourcePath, destPath); |
| 64 | + } else { |
| 65 | + await fs.copyFile(sourcePath, destPath); |
| 66 | + } |
| 67 | + } catch (e) { |
| 68 | + consola.error(`Failed to copy ${sourcePath} to ${destPath}`, e); |
| 69 | + throw new Error(`Failed to copy template files, ${e}`); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +async function init() { |
| 75 | + const currentWorkingDirectory = process.cwd(); |
| 76 | + const packageRoot = path.join(__dirname, "..", "..", ".."); //navigate to package root from here |
| 77 | + const templatePath = path.join(packageRoot, "templates", "ts"); |
| 78 | + |
| 79 | + const useCurrentDir = await inquirer.prompt([ |
| 80 | + { |
| 81 | + type: "confirm", |
| 82 | + name: "useCurrent", |
| 83 | + message: "Initialize a project in the current directory?", |
| 84 | + default: true, |
| 85 | + }, |
| 86 | + ]); |
| 87 | + |
| 88 | + let projectDir = currentWorkingDirectory; |
| 89 | + |
| 90 | + if (!useCurrentDir.useCurrent) { |
| 91 | + const projectDirResponse = await inquirer.prompt([ |
| 92 | + { |
| 93 | + type: "input", |
| 94 | + name: "projectName", |
| 95 | + message: "What is your project directory?", |
| 96 | + default: "my-project", |
| 97 | + }, |
| 98 | + ]); |
| 99 | + projectDir = path.join( |
| 100 | + currentWorkingDirectory, |
| 101 | + projectDirResponse.projectName, |
| 102 | + ); |
| 103 | + } |
| 104 | + const packageManager = await detectPackageManager(); |
| 105 | + |
| 106 | + const packageManagerResponse = await inquirer.prompt([ |
| 107 | + { |
| 108 | + type: "list", |
| 109 | + name: "packageManager", |
| 110 | + message: "Select a package manager to install dependencies:", |
| 111 | + choices: [ |
| 112 | + { name: "npm", value: "npm" }, |
| 113 | + { name: "yarn", value: "yarn" }, |
| 114 | + { name: "pnpm", value: "pnpm" }, |
| 115 | + { name: "bun", value: "bun" }, |
| 116 | + { name: "Don't install dependencies", value: "none" }, |
| 117 | + ], |
| 118 | + default: packageManager ?? "none", |
| 119 | + }, |
| 120 | + ]); |
| 121 | + |
| 122 | + consola.info(`Initializing project in ${projectDir}...`); |
| 123 | + |
| 124 | + try { |
| 125 | + await fs.ensureDir(projectDir); |
| 126 | + await copyTemplate(templatePath, projectDir); |
| 127 | + consola.success("Copied template files successfully."); |
| 128 | + |
| 129 | + if (packageManagerResponse.packageManager !== "none") { |
| 130 | + consola.info( |
| 131 | + `Installing dependencies with ${packageManagerResponse.packageManager}...`, |
| 132 | + ); |
| 133 | + execSync(`${packageManagerResponse.packageManager} install`, { |
| 134 | + cwd: projectDir, |
| 135 | + stdio: "inherit", |
| 136 | + }); |
| 137 | + consola.success("Installed dependencies."); |
| 138 | + } |
| 139 | + |
| 140 | + consola.success("Project initialized successfully!"); |
| 141 | + consola.info( |
| 142 | + `Go to ${path.relative(currentWorkingDirectory, projectDir)} and run "${packageManagerResponse.packageManager} run build" and "${packageManagerResponse.packageManager} run start" to get started.`, |
| 143 | + ); |
| 144 | + } catch (error: any) { |
| 145 | + consola.error(`Failed to initialize project: ${error.message}`); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +export { init }; |
0 commit comments