1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| import { resolve } from "node:path"; import { execSync } from "node:child_process"; import { mkdirSync, readdirSync } from "node:fs";
const arg1 = process.argv[2]; const arg2 = process.argv[3]; const arg3 = process.argv[4];
main(arg1, arg2, arg3);
function main(arg1, arg2, arg3) { console.log("执行中..."); const gitCloneUrl = arg1 || ""; const gitBranch = arg2 || "master"; const buildEnv = arg3 || "build"; const pwd = resolve(); const workDir = "work_space"; const vueBuildDirName = "dist"; const saveTarDir = "dist_files"; const remoteFiles = "favicon.ico index.html index.html.gz assets";
if (!gitCloneUrl) return console.error("git clone 地址不能为空!");
try { console.log(execSync(`rm -rf ${workDir}`, { cwd: pwd }).toString()); console.log("正在克隆代码..."); console.log(execSync(`git clone ${gitCloneUrl} ${workDir} -b ${gitBranch} --depth=1`, { cwd: pwd }).toString()); console.log("克隆完成"); console.log("正在安装项目依赖..."); console.log(execSync(`npm install`, { cwd: resolve(pwd, workDir) }).toString()); console.log("安装项目依赖完成"); console.log("正在打包..."); console.log(execSync(`npm run ${buildEnv}`, { cwd: resolve(pwd, workDir) }).toString()); console.log("打包完成"); console.log("正在整理资源..."); const tarName = handleDistTarName(); console.log(execSync(`tar cvf ${tarName}.tar -C ./${vueBuildDirName} .`, { cwd: resolve(pwd, workDir) }).toString()); const fileList = readdirSync(pwd); if (fileList.includes(saveTarDir)) { console.log("存放所有构建物文件目录已存在"); } else { mkdirSync(resolve(pwd, saveTarDir)); console.log("存放所有构建物文件目录已创建"); } console.log(execSync(`mv ./${workDir}/${tarName}.tar ./`, { cwd: pwd }).toString()); console.log("正在整理资源..."); console.log(execSync(`rm -rf ${remoteFiles}`, { cwd: pwd }).toString()); console.log(execSync(`tar xvf ${tarName}.tar`, { cwd: pwd }).toString()); console.log(execSync(`mv ${tarName}.tar ./${saveTarDir}`, { cwd: pwd }).toString()); console.log("正在整理资源..."); console.log(execSync(`rm -rf ${workDir}`, { cwd: pwd }).toString()); console.log("构建更新完成"); } catch (error) { console.error("执行错误:", error); } }
function handleDistTarName() { const y = new Date().getFullYear().toString(); const m = (new Date().getMonth() + 1).toString().padStart(2, "0"); const d = new Date().getDate().toString().padStart(2, "0"); const h = new Date().getHours().toString().padStart(2, "0"); const mi = new Date().getMinutes().toString().padStart(2, "0"); const s = new Date().getSeconds().toString().padStart(2, "0"); return `${y}-${m}-${d}_${h}-${mi}-${s}`; }
|