trtc-electron-sdk
Version:
trtc electron sdk
245 lines (225 loc) • 7.54 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 buildDownloadInfo = () => {
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://web.sdk.qcloud.com/trtc/electron/download/${name}/${version}/${name}-${osLabel}-${version}.zip`,
name,
version,
archType: configArgv.arch,
};
};
const main = () => {
const {
platform,
downloadUrl,
name,
version,
archType,
} = buildDownloadInfo();
const outputDir = path.join(__dirname, "../temp");
const buildDir = path.join(__dirname, "../build");
rimraf.sync(outputDir);
rimraf(buildDir, (err) => {
if (err) {
signale.fatal(err);
process.exit(1);
}
// 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,
}),
};
}
download(downloadUrl, outputDir, downloadOptions)
.then(() => {
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://web.sdk.qcloud.com/trtc/electron/download/${name}/${version}/${name}-${anotherPlatform}-${version}.zip`;
signale.info("Download Url =", anotherDownloadUrl, "\n");
signale.pending(
`Downloading C++ addon Electron SDK... ${anotherArch}\n`
);
download(anotherDownloadUrl, path.join(outputDir, anotherPlatform), {
strip: 0,
extract: true,
})
.then(() => {
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);
})
.catch((err) => {
signale.fatal(`download error! - mac ${anotherPlatform}`, err);
});
} 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://web.sdk.qcloud.com/trtc/electron/download/${name}/${version}/${name}-${anotherPlatform}-${version}.zip`;
signale.info("Download Url =", anotherDownloadUrl, "\n");
signale.pending(
`Downloading C++ addon Electron SDK... ${anotherArch}\n`
);
download(anotherDownloadUrl, path.join(outputDir, anotherPlatform), {
strip: 0,
extract: true,
})
.then(() => {
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);
})
.catch((err) => {
signale.fatal(`download error! - linux ${anotherPlatform}`, err);
});
} else {
signale.warn(`Not supported platform: ${platform}`);
}
})
.catch((err) => {
signale.fatal(`Failed! Download error - ${platform}`, err);
});
});
};
main();