wechat-web-devtools-linux/tools/update-node-node
2022-02-23 22:09:39 +08:00

120 lines
3.8 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
const path = require("path");
const fs = require("fs");
const { execSync, spawn } = require("child_process");
const nodeConfig = require("../conf/node.json");
const { exit } = require("process");
const download = function () {
return new Promise((resolve, reject) => {
try {
fs.mkdirSync(path.resolve(__dirname, "../cache"));
} catch (error) {}
let url = process.env['ACTION'] === 'true'?nodeConfig["url-global"]:nodeConfig.url
url = 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;
}
console.info(`Downloading ${url} ---> ${localPath}`);
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) => {
try {
fs.mkdirSync(path.resolve(__dirname, "../tmp"));
} catch (error) {}
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"))) {
execSync(`cd ${path.resolve(__dirname, "../nwjs")} && ln -sr ../node/bin/node node`)
}
if (!fs.existsSync(path.resolve(__dirname, "../nwjs/node.exe"))) {
execSync(`cd ${path.resolve(__dirname, "../nwjs")} && ln -s node node.exe`)
}
}
resolve();
});
};
const start = async () => {
try {
const args = process.argv.slice(2);
if(args[0])nodeConfig.version=args[0]
const nodePath = path.resolve(__dirname, "../node/bin/node")
if(fs.existsSync(nodePath)){
const version = execSync(`${path.resolve(__dirname, "../node/bin/node")} --version`).toString().replace('\n', '')
if(version === `v${nodeConfig.version}`){
console.log(`Node版本${version} 已经安装,忽略`)
exit(0)
}else{
console.log(`已安装Node版本${version}将要安装v${nodeConfig.version}`)
}
}
const localPath = await download();
const extractPath = await extract(localPath);
await upgrade(extractPath);
execSync(`${path.resolve(__dirname, "../node/bin/npm")} install node-gyp`)
console.log(
`Succeeded upgrading nodeConfig to version ${nodeConfig.version}`
);
} catch (error) {
console.error("异常", error);
exit(-1)
}
};
start();