import { Spinner } from "@topcli/spinner"; import inquirer from "inquirer"; import cp from "child_process"; import chalk from "chalk"; import path from "path"; import md5 from "md5"; import fs from "fs"; import { DIR } from "../../index.js"; const generateProject = (params: { startCommand?: string; buildCommand?: string; nodeVersion?: string; targetPath: string; repository: string; command?: string; branch: string; name: string; env: string; rsaKeys: { privateKey: string; publicKey: string; } }, isCustomRSA?: boolean) => { return new Promise(async (resolve, reject) => { const spinner = new Spinner().start(`${params.name} - Project cloning...`); let directory; if (process.platform === "win32") { directory = `${params.targetPath}\\${params.name}`; } else { directory = `${params.targetPath}/${params.name}`; } fs.writeFileSync(path.join(DIR, "privateKey.pem"), params.rsaKeys.privateKey, { mode: "600" }); fs.chmodSync(path.join(DIR, "privateKey.pem"), "0600"); const respPass = await inquirer.prompt([ { name: "keyPassword", message: `${params.name} - Enter your key password:`, type: "password" } ]); const passmd5 = md5(respPass.keyPassword); const pass = md5(passmd5); console.log( "COPY and PASTE this password:", chalk.cyan(pass) ); const agent = "eval `ssh-agent`"; let command = ""; const isRepoExists = fs.existsSync(path.join(directory, ".git")); if (isRepoExists) { command = `cd ${directory} && ${agent} && ssh-add ${path.join(DIR, "privateKey.pem")} && GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no -i ${path.join(DIR, "privateKey.pem")}" git pull origin ${params.branch}`; } else { command = `${agent} && ssh-add ${path.join(DIR, "privateKey.pem")} && GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no -i ${path.join(DIR, "privateKey.pem")}" git clone -b ${params.branch} ${params.repository} ${directory}`; } cp.execSync(`${command}`, { maxBuffer: 1024 * 1024 * 50, stdio: "inherit" }); spinner.succeed(`${params.name} - Project successfully cloned.`); if (isCustomRSA) { console.log( "Your RSA Private Key:", chalk.blue( params.rsaKeys.privateKey ), "Your RSA Public Key:", chalk.blue( params.rsaKeys.publicKey ) ); } if(params.nodeVersion) { cp.execSync(`export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh" && nvm install ${params.nodeVersion} && nvm use ${params.nodeVersion} && npm i -g yarn pm2 && cd ${directory} && yarn install --check-files --ignore-engines && yarn ${params.buildCommand ? params.buildCommand : "build"}`, { maxBuffer: 1024 * 1024 * 50, stdio: "inherit" }); } else { cp.execSync(`cd ${directory} && yarn install --ignore-engines && yarn ${params.buildCommand ? params.buildCommand : "build"}`, { maxBuffer: 1024 * 1024 * 20, stdio: "inherit" }); } fs.writeFileSync(`${path.join(directory, ".env")}`, params.env); let pm2RestartCommand = `npx pm2 restart ${params.name} --update-env`; let pm2StartCommand = `cd ${directory} && npx pm2 start yarn --name ${params.name} --time --log-date-format "YYYY-MM-DDTHH:mm:ss.SSSZ" -l ${params.name}-combined.log -- ${params.command ? params.command : "start:prod"}`; if(params.nodeVersion) { pm2RestartCommand = `export NVM_DIR="$HOME/.nvm"\n[ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh" && nvm use ${params.nodeVersion} && ${pm2RestartCommand}`; pm2StartCommand = `export NVM_DIR="$HOME/.nvm"\n[ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh" && nvm use ${params.nodeVersion} && ${pm2StartCommand}`; } try { cp.execSync(pm2RestartCommand, { stdio: "ignore" }); console.log(`${params.name} app successfully restarted.`); } catch { cp.execSync(pm2StartCommand, { stdio: "inherit" }); } spinner.succeed(`${params.name} - Project successfully builded.`); resolve(true); }); }; export default generateProject;