工具库中记录打包环境信息相关代码
1const os = require("os");
2const util = require("util");
3const exec = util.promisify(require("child_process").exec);
4const fs = require("fs");
5const path = require("path");
6const vueConfig = require("../../../../vue.config");
7const defaultDir = path.resolve(__dirname, "../../../../"); //使用项目的根目录
8// const vueConfig = require("./vue.config");
9// const defaultDir = path.resolve(__dirname, "./"); //使用项目的根目录
10var MODE_0666 = parseInt("0666", 8);
11
12async function execCommand() {
13 const { stdout, stderr } = await exec("git branch -vv");
14 const { stdout: n_stdout, stderr: n_stderr } = await exec("npm -v");
15 const { stdout: o_stdout, stderr: o_stderr } = await exec("node -v");
16 if (stderr || n_stderr || o_stderr) {
17 console.log(stderr, n_stderr, o_stderr);
18 } else {
19 return [
20 stdout.split("\n").find((i) => i.startsWith("*")),
21 n_stdout,
22 o_stdout,
23 ];
24 }
25}
26
27function readFileText(absolutePath) {
28 //console.log(' \x1b[36mread file from\x1b[0m : ' + absolutePath.substr(-80));
29 return fs.readFileSync(absolutePath, "utf-8").toString();
30}
31
32function writeFile(absolutePath, str, mode) {
33 fs.writeFileSync(absolutePath, str, { mode: mode || MODE_0666 });
34 //console.log(' \x1b[36mwrite file to\x1b[0m : ' + absolutePath.substr(-80));
35}
36
37//当前时间
38let time = new Date().toLocaleString();
39
40//当前操作系统
41let sys = os.platform();
42
43//当前软件名、版本、依赖软件版本
44let packageStr = readFileText(path.join(defaultDir, "/package.json")),
45 pkg = JSON.parse(packageStr),
46 name = pkg.name, //软件名
47 version = pkg.version, //当前package.json version
48 dependencies = pkg.dependencies,
49 packages = Object.keys(dependencies),
50 realPackageStr = ""; //
51
52//依赖软件版本信息
53let lockStr = readFileText(path.join(defaultDir, "/package-lock.json")),
54 lockPkg = JSON.parse(lockStr),
55 lockDependencies = lockPkg.dependencies;
56for (let i = 0; i < packages.length; i++) {
57 realPackageStr +=
58 " ->" + packages[i] + ":" + lockDependencies[packages[i]].version + "\n";
59}
60
61//当前分支
62execCommand().then((res) => {
63 if (res) {
64 let str =
65 `=====build env info(仅仅记录打包时环境,用于问题追踪)=====\nbuild software:${name}\nbuild branch:${res[0]}\nbuild time:` +
66 `${time}\nbuild platform:${sys}\nnpm version:${res[1]}node version:${res[2]}package version(不准,经常忘记升级版本号):${version}\n` +
67 `package-lock versions:\n${realPackageStr}`;
68 //console.log(str);
69 //写到打包后的根目录
70 const writeFilePath = path.join(
71 defaultDir,
72 vueConfig.outputDir,
73 "buildEnv"
74 );
75 console.log(writeFilePath);
76 writeFile(writeFilePath, str);
77 }
78});
79
80
使用
- 发部相关工具
- 安装发布的工具
- package.json,scripts 相关打包命令后+
&& node ./node_modules/@audaque/vue-config/lib/buildEnv
包中 buildEnv 文件输出结果
评论