#!/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 { 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"))) { fs.symlinkSync( path.resolve(__dirname, "../node/bin/node"), path.resolve(__dirname, "../nwjs/node") ); } if (!fs.existsSync(path.resolve(__dirname, "../nwjs/node.exe"))) { fs.symlinkSync( "node", path.resolve(__dirname, "../nwjs/node.exe") ); } } resolve(); }); }; const start = async () => { try { 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();