Article directory
foreword
- Used to automatically modify the version number in the electron-vite total package
- Modify content package.json and nodejs script content
1. Node.js
API call of basic nodejs
import path from "path";
import fs from "fs";
import {
exec } from "child_process";
// 读取json文件、读取自定义配置文件
const url = path.join(process.cwd(), "package.json");
const json = fs.readFileSync(url, "utf-8");
const pkg = JSON.parse(json);
try {
// 修改build的version
pkg.version = process.env.npm_config_setv;
// 写入文件
fs.writeFileSync(url, JSON.stringify(pkg, null, 2));
const build = exec("npm run build");
build.stdout.on("data", (data) => console.log(data));
} catch (error) {
console.log("error", error);
}
Two, package.json
1. Add script script
"buildVersion": "node build.js --version"
2. Command line input
npm run buildVersion --setv = 2.3.4
- The version in package.json is automatically modified.
- Then run the packaging command directly through exec in child_process.