agora-rdc-core
Version:
94 lines (82 loc) • 3.04 kB
JavaScript
const { promises: fs } = require('fs');
const os = require('os');
const path = require('path');
const util = require('util');
const ora = require('ora');
const download = require('download');
const rimraf = require('rimraf');
const throttle = require('lodash.throttle');
const napiBuildUtils = require('napi-build-utils');
const copy = require('copy-concurrently');
const pkg = require('../package.json');
const CWD = process.cwd();
const BUILD_FOLDER = path.join(CWD, 'build');
const CACHE_FOLDER = path.join(CWD, 'build/.cache');
const RELEASE_FOLDER = path.join(CWD, 'build/Release');
const DIST_URL = 'https://download.agora.io';
const SUPPORTED_PLATFORM = ['darwin', 'win32'];
const arch = process.env.npm_config_arch || process.arch;
const RTM_ARTIFACTS = {
'darwin-x64': 'Agora_RTM_SDK_for_Mac_Unity_v1_4_10',
'darwin-arm64': 'Agora_RTM_SDK_for_Mac_Unity_v1_4_10',
'win32-x64': 'Agora_RTM_SDK_for_Windows_x64_v1.4.10',
'win32-ia32': 'Agora_RTM_SDK_for_Windows_x86_v1.4.10',
};
const NAPI_VERSION = napiBuildUtils.getBestNapiBuildVersion();
function getRtmArtifactPath() {
return `rtmsdk/release/${RTM_ARTIFACTS[`${os.platform()}-${arch}`]}.zip`;
}
function getAddonPath() {
return `rdc/release/agora-rdc-core-v${pkg.version}-napi-v${NAPI_VERSION}-${os.platform()}-${arch}.tar.gz`;
}
function getAssetUrl(path) {
return `${DIST_URL}/${path}`;
}
async function downloadArtifact(source, dest) {
const spinner = ora(`Downloading ${source}`).start();
// https://github.com/kevva/decompress-unzip/issues/12
await download(source, dest, {
extract: true,
map: (file) => {
if (file.type === 'file' && file.path.endsWith('/')) {
file.type = 'directory';
}
return file;
},
}).on(
'downloadProgress',
throttle(({ percent, transferred, total }) => {
spinner.info(`Downloading progress ${(percent * 100).toFixed(2)}% ${transferred}/${total}kb ${source} `);
}, 500),
);
spinner.succeed(`Downloaded ${source}`);
}
async function install() {
if (!SUPPORTED_PLATFORM.includes(os.platform())) {
throw Error(`Platform: ${os.platform()} is not supported!`);
}
if (!RTM_ARTIFACTS[`${os.platform()}-${arch}`]) {
throw Error(`Arch: ${arch} is not supported!`);
}
await util.promisify(rimraf)(BUILD_FOLDER);
await fs.mkdir(BUILD_FOLDER);
await fs.mkdir(RELEASE_FOLDER);
const addonAssetUrl = getAssetUrl(getAddonPath());
await downloadArtifact(addonAssetUrl, CWD);
const rtmAssetUrl = getAssetUrl(getRtmArtifactPath());
await downloadArtifact(rtmAssetUrl, CACHE_FOLDER);
if (os.platform() === 'darwin') {
await copy(
`${CACHE_FOLDER}/Agora_RTM_SDK_for_Mac/libs/AgoraRtmKit.framework`,
`${RELEASE_FOLDER}/AgoraRtmKit.framework`,
);
}
if (os.platform() === 'win32') {
await copy(`${CACHE_FOLDER}/product_sdk/sdk/dll/agora_rtm_sdk.dll`, `${RELEASE_FOLDER}/agora_rtm_sdk.dll`);
}
await util.promisify(rimraf)(CACHE_FOLDER);
}
install().catch((error) => {
console.error(error);
process.exit(-1);
});