UNPKG

trtc-electron-sdk

Version:

trtc electron sdk

152 lines (142 loc) 3.86 kB
/** * Utils for command line tools */ const semver = require("semver"); const pkg = require("../package.json"); const { Platform } = require("./constant"); module.exports.readCliArgv = function () { const cliArgv = process.argv; const newArgv = {}; let tmp = []; console.log("readCliArgv", cliArgv); for (let i = 0, len = cliArgv.length; i < len; i++) { if (i === 0) { newArgv.nodeDir = cliArgv[i]; continue; } if (i === 1) { newArgv.scriptDir = cliArgv[i]; continue; } tmp = cliArgv[i].replace("--", "").split("="); newArgv[tmp[0]] = tmp[1]; tmp = []; } return newArgv; }; module.exports.readProxyUrlFromEnv = function (downloadUrl) { if (downloadUrl.indexOf("https") === 0) { return process.env.https_proxy || ""; } return process.env.http_proxy || ""; }; module.exports.readArgvFromNpmEnv = function () { const result = {}; if (process.env.npm_config_trtc_electron_arch) { result.arch = process.env.npm_config_trtc_electron_arch; } if (process.env.npm_config_trtc_electron_platform) { result.platform = process.env.npm_config_trtc_electron_platform; } return result; }; module.exports.readArgvFromPkgJson = function () { let INIT_CWD = process.env["INIT_CWD"]; if (!INIT_CWD) return {}; let rootDirIndex = 0; let PkgJsonDir = ""; if (process.platform === "win32") { INIT_CWD = INIT_CWD.split("\\"); rootDirIndex = INIT_CWD.findIndex((item) => item === "node_modules"); PkgJsonDir = INIT_CWD.splice(0, rootDirIndex).join("\\\\") + "\\\\package.json"; } else { INIT_CWD = INIT_CWD.split("/"); rootDirIndex = INIT_CWD.findIndex((item) => item === "node_modules"); PkgJsonDir = INIT_CWD.splice(0, rootDirIndex).join("/") + "/package.json"; } const { trtc_electron } = require(PkgJsonDir); console.log( "INIT_CWD", INIT_CWD, "PkgJsonDir", PkgJsonDir, "trtc_electron", trtc_electron ); if (!trtc_electron) { return {}; } const { platform, arch } = trtc_electron; const result = {}; if (platform) { result.platform = platform; } if (arch) { result.arch = arch; } return result; }; module.exports.detectConfigArgv = function (configArgv) { if (Object.prototype.toString.call(configArgv) === "[object Object]") { const { platform, arch } = configArgv; if (platform === "darwin") { if (arch !== "x64" && arch !== "arm64") { configArgv.arch = "x64"; } } else if (platform === "win32") { if (arch !== "ia32" && arch !== "x64") { configArgv.arch = "x64"; } } else if (platform === "linux") { if (arch !== "x64" && arch !== "arm64") { configArgv.arch = "x64"; } } return configArgv; } else { return { platform: process.arch, arch: process.platform, }; } }; module.exports.detectOS = (archType = "", platformType = "") => { const platform = platformType !== "" ? platformType : process.platform; const arch = archType !== "" ? archType : process.arch; if (platform === "darwin") { if (arch === "x64") { return Platform.MACOS; } else { return Platform.MACOS_ARM; } } else if (platform === "win32") { if (arch === "x64") { return Platform.WINDOWS64; } else { return Platform.WINDOWS32; } } else if (platform === "linux") { if (arch === "x64") { return Platform.LINUX_X64; } else if (arch === "arm64") { return Platform.LINUX_ARM64; } else { // unsupported } } return Platform.UNSUPPORTED; }; module.exports.detectOwnVersion = () => { const { major, minor, patch } = semver.coerce(pkg.version); return { major, minor, patch, version: pkg.version, }; }; module.exports.detectOwnName = () => { const name = pkg.name; return name; };