【脚本】vue项目自动化构建-构建物储存回滚(node脚本)

构建更新,储存每次构建物

使用:

1
node vue-build.mjs git克隆地址 git分支(可选) 打包指令(可选)

vue-build.mjs:

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);
/**
* 程序入口
* @param {string} arg1 - git clone 地址
* @param {string} arg2 - git branch 名称 默认:master
* @param {string} arg3 - 打包指令 默认:build
*/
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);
}
}

/**
* 打包构建物的命名(日期时间)
* @returns {String}
*/
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}`;
}

构建物回滚

使用:

1
node vue-rollback.mjs 构建物路径

vue-rollback.mjs:

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
import { resolve } from "node:path";
import { execSync } from "node:child_process";

const arg1 = process.argv[2];

main(arg1);
/**
* 程序入口
* @param {string} tarFilePath - 构建物路径
*/
function main(tarFilePath) {
console.log("执行中...");
const pwd = resolve();
const removeFiles = "favicon.ico index.html index.html.gz assets";

if (!tarFilePath) return console.error("请传入构建物");

try {
console.log(execSync(`rm -rf ${removeFiles}`, { cwd: pwd }).toString());
console.log(execSync(`tar xvf ${tarFilePath}`, { cwd: pwd }).toString());
console.log(`使用了:${tarFilePath} 构建物`);
} catch (error) {
console.error("执行错误:", error);
}
}