trtc-electron-sdk
Version:
trtc electron sdk
228 lines (207 loc) • 6.75 kB
JavaScript
const download = require("download");
const path = require("path");
const fs = require("fs-extra");
const rimraf = require("rimraf");
const signale = require("signale");
const { HttpsProxyAgent } = require("hpagent");
const currentPath = process.cwd();
const pathFlagValue = `packages${
process.platform === "win32" ? "\\" : "/"
}trtc-electron-sdk`;
if (currentPath.indexOf(pathFlagValue) !== -1) {
signale.info(
"trtc-electron-sdk: development mode, skip native sdk lib download"
);
return;
}
const {
readCliArgv,
readArgvFromNpmEnv,
detectOS,
detectOwnVersion,
detectOwnName,
readProxyUrlFromEnv,
readArgvFromPkgJson,
detectConfigArgv,
} = require("./utils");
const { Platform } = require("./constant");
const urlDomains = [
"web.sdk.qcloud.com",
"web.sdk.cloud.tencent.cn",
"web.sdk.tencent.cn",
];
const buildDownloadInfo = (urlDomain) => {
let configArgv = Object.assign(
{
arch: process.arch,
platform: process.platform,
},
readCliArgv(),
readArgvFromNpmEnv(),
readArgvFromPkgJson()
);
configArgv = detectConfigArgv(configArgv);
// build os label
const osLabel = detectOS(configArgv.arch, configArgv.platform);
// build version label
const { version } = detectOwnVersion();
signale.info("buildDownloadInfo version=", version);
const name = detectOwnName();
signale.info("buildDownloadInfo name=", name);
console.log("detectedArgv", osLabel);
// generate download url
return {
platform: osLabel,
downloadUrl: `https://${urlDomain}/trtc/electron/download/${name}/${version}/${name}-${osLabel}-${version}.zip`,
name,
version,
archType: configArgv.arch,
};
};
async function main(urlDomain) {
const {
platform,
downloadUrl,
name,
version,
archType,
} = buildDownloadInfo(urlDomain);
const outputDir = path.join(__dirname, "../temp");
const buildDir = path.join(__dirname, "../build");
rimraf.sync(outputDir);
rimraf.sync(buildDir);
// print download info
signale.info("removeDir =", buildDir);
signale.info("outputDir =", outputDir);
signale.info("Package Version =", version);
signale.info("Platform =", platform);
signale.info("Download Url =", downloadUrl, "\n");
signale.pending("Downloading C++ addon Electron SDK...\n");
const downloadOptions = {
strip: 0,
extract: true,
};
const proxyUrl = readProxyUrlFromEnv(downloadUrl);
if (proxyUrl) {
downloadOptions.agent = {
https: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: "lifo",
proxy: proxyUrl,
}),
};
}
try {
await download(downloadUrl, outputDir, downloadOptions);
signale.success(`Download finished - ${platform}`);
fs.copySync(
path.join(outputDir, "./build/Release"),
path.join(buildDir, "Release")
);
signale.success(`copy success! - ${platform}`);
if (platform === Platform.WINDOWS32 || platform === Platform.WINDOWS64) {
// windows 下
rimraf.sync(outputDir);
} else if (platform === Platform.MACOS || platform === Platform.MACOS_ARM) {
// mac 下
const arch = archType;
fs.copySync(
path.join(outputDir, "./build/Release/trtc_electron_sdk.node"),
path.join(buildDir, "Release", arch, "trtc_electron_sdk.node")
);
fs.copySync(
path.join(outputDir, "./build/mac-framework", arch),
path.join(buildDir, "mac-framework", arch)
);
signale.success(`copy success! - mac ${arch}`, "\n");
let anotherPlatform, anotherArch;
if (arch === "x64") {
// download arm64
anotherPlatform = Platform.MACOS_ARM;
anotherArch = "arm64";
} else {
// arch = arm64, download x64
anotherPlatform = Platform.MACOS;
anotherArch = "x64";
}
const anotherDownloadUrl = `https://${urlDomain}/trtc/electron/download/${name}/${version}/${name}-${anotherPlatform}-${version}.zip`;
signale.info("Download Url =", anotherDownloadUrl, "\n");
signale.pending(`Downloading C++ addon Electron SDK... ${anotherArch}\n`);
await download(anotherDownloadUrl, path.join(outputDir, anotherPlatform), {
strip: 0,
extract: true,
})
signale.success(`Download finished - mac ${anotherArch}`);
fs.copySync(
path.join(
outputDir,
anotherPlatform,
"/build/Release/trtc_electron_sdk.node"
),
path.join(
buildDir,
"Release",
anotherArch,
"trtc_electron_sdk.node"
)
);
fs.copySync(
path.join(
outputDir,
anotherPlatform,
"/build/mac-framework",
anotherArch
),
path.join(buildDir, "mac-framework", anotherArch)
);
signale.success(`copy success! - mac ${anotherArch}`);
rimraf.sync(outputDir);
} else if (platform === Platform.LINUX_X64 || platform === Platform.LINUX_ARM64) {
// Linux 下
const arch = process.arch;
fs.copySync(
path.join(outputDir, "./build/Release"),
path.join(buildDir, "Release", arch)
);
signale.success(`copy success! - linux ${arch}`, "\n");
let anotherPlatform, anotherArch;
if (arch === "x64") {
// download arm64
anotherPlatform = Platform.LINUX_ARM64;
anotherArch = "arm64";
} else {
// arch = arm64, download x64
anotherPlatform = Platform.LINUX_X64;
anotherArch = "x64";
}
const anotherDownloadUrl = `https://${urlDomain}/trtc/electron/download/${name}/${version}/${name}-${anotherPlatform}-${version}.zip`;
signale.info("Download Url =", anotherDownloadUrl, "\n");
signale.pending(`Downloading C++ addon Electron SDK... ${anotherArch}\n`);
await download(anotherDownloadUrl, path.join(outputDir, anotherPlatform), {
strip: 0,
extract: true,
})
signale.success(`Download finished - linux ${anotherArch}`);
fs.copySync(
path.join(outputDir, anotherPlatform, "/build/Release"),
path.join(buildDir, "Release", anotherArch)
);
signale.success(`copy success! - linux ${anotherArch}`);
rimraf.sync(outputDir);
} else {
signale.warn(`Not supported platform: ${platform}`);
}
} catch(err) {
const anotherUrlDomain = urlDomains.shift();
if (anotherUrlDomain) {
signale.info(`Try to download from another address:${anotherUrlDomain}`);
main(anotherUrlDomain);
} else {
signale.error(`Failed! Download error - ${platform}`, err);
}
}
}
main(urlDomains.shift());