wechat-web-devtools-linux/tools/update-node-node
2022-01-24 11:33:45 +08:00

110 lines
3.0 KiB
JavaScript

#!/usr/bin/env node
const path = require("path");
const fs = require("fs");
const { execSync, spawn } = require("child_process");
const util = require("util");
const nodeConfig = require("../conf/node.json");
const download = function () {
return new Promise((resolve, reject) => {
try {
fs.mkdirSync(path.resolve(__dirname, "../cache"));
} catch (error) {}
let url = nodeConfig.url
.replace("${version}", nodeConfig.version)
.replace("${version}", nodeConfig.version);
let localPath = path.resolve(
__dirname,
"../cache",
url.split("?")[0].split("/").slice(-1)[0]
);
if (fs.existsSync(localPath)) {
console.info(`Cache available ${path.basename(localPath)}`);
resolve(localPath);
return;
}
info(`Downloading ${url}`);
spawn("wget", ["c", url, "O", `${localPath}.tmp`], {
stdio: "inherit",
}).on("close", (code) => {
fs.renameSync(`${localPath}.tmp`, localPath);
resolve(localPath);
});
});
};
const extract = function (localPath) {
console.info(`Extracting ${localPath}`);
return new Promise((resolve, reject) => {
let extractPath = path.resolve(
__dirname,
`../tmp/${path.basename(localPath)}`
);
execSync(`rm -rf ${extractPath}`);
fs.mkdirSync(extractPath);
spawn("tar", ["xf", localPath, '-C', extractPath], { stdio: "inherit" }).on(
"close",
(code) => {
console.log(`Extracting result code: ${code}`)
resolve(extractPath);
}
);
});
};
const upgrade = function (extractPath) {
console.info(`Upgrading ${path.basename(extractPath)}`);
return new Promise((resolve, reject) => {
execSync(`rm -rf ${path.resolve(__dirname, "../node")}`);
fs.renameSync(
path.resolve(extractPath, fs.readdirSync(extractPath)[0]),
path.resolve(__dirname, "../node")
);
execSync(`rm -rf ${extractPath}`);
if (fs.existsSync(path.resolve(__dirname, "../nwjs"))) {
if (!fs.existsSync(path.resolve(__dirname, "../nwjs/node"))) {
fs.linkSync(
"../node/bin/node",
path.resolve(__dirname, "../nwjs/node")
);
}
if (!fs.existsSync(path.resolve(__dirname, "../nwjs/node.exe"))) {
fs.linkSync(
"node",
path.resolve(__dirname, "../nwjs/node.exe")
);
}
}
resolve();
});
};
const start = async () => {
try {
const localPath = await download();
const extractPath = await extract(localPath);
await upgrade(extractPath);
console.log(
`Succeeded upgrading nodeConfig to version ${nodeConfig.version}`
);
} catch (error) {
console.error("异常", error);
throw error
}
};
start();