UNPKG

@plasosdk/plaso-electron-sdk

Version:

伯索课堂Electron SDK

184 lines (158 loc) 7.64 kB
/* eslint-disable camelcase */ const path = require('path'); const fs = require('fs-extra'); const https = require('https'); const { exec, execSync } = require('child_process'); const AdmZip = require('adm-zip'); const getConfig = require('./getConfig'); const LogFormatter = require('./logger'); const logger = new LogFormatter(); const minimist = require('minimist'); const { INIT_CWD } = minimist(process.argv.slice(2), { string: ['INIT_CWD'], default: {}, }); logger.info(`pass INIT_CWD ${INIT_CWD}`); const { platform, packageVersion, arch } = getConfig(); const agora_electron_sdk_v4_x32_in_win32 = 'npm install agora-electron-sdk@4.2.6-build.157-rc.1 --agora_electron_sdk_arch=ia32 --agora_electron_sdk_platform=win32 --global-style --no-save --no-package-lock'; const agora_electron_sdk_v4_x64_in_win32 = 'npm install agora-electron-sdk@4.2.6-build.157-rc.1 --agora_electron_sdk_arch=x64 --agora_electron_sdk_platform=win32 --global-style --no-save --no-package-lock'; const agora_electron_sdk_v4_in_darwin = 'npm install agora-electron-sdk@4.2.6-build.157-rc.1 --agora_electron_sdk_arch=x64 --agora_electron_sdk_platform=darwin --global-style --no-save --no-package-lock'; const trtc_electron_sdk_x32_in_win32 = 'npm install trtc-electron-sdk@11.8.112-beta.6 --trtc_electron_arch=ia32 --trtc_electron_platform=win32 --global-style --no-save --no-package-lock --legacy-peer-deps'; const trtc_electron_sdk_x64_in_win32 = 'npm install trtc-electron-sdk@11.8.112-beta.6 --trtc_electron_arch=x64 --trtc_electron_platform=win32 --global-style --no-save --no-package-lock --legacy-peer-deps'; const trtc_electron_sdk_in_darwin = 'npm install trtc-electron-sdk@11.8.112-beta.6 --trtc_electron_arch=x64 --trtc_electron_platform=darwin --global-style --no-save --no-package-lock --legacy-peer-deps'; const electron_remote = 'npm install @electron/remote --global-style --no-save --legacy-peer-deps'; const PLATFORM_TYPE = { DARWIN: 'darwin', WIN32: 'win32', }; const ARCH_TYPE = { X64: 'x64', IA32: 'ia32', ARM64: 'arm64', }; const oldTrtcPath = path.join(__dirname, '../node_modules', 'trtc-electron-sdk'); const newTrtcPath = path.join(__dirname, '../../../', 'trtc-electron-sdk'); const electron_sdk_plugins_dir = 'https://wwwr.plaso.cn/static/cdn/electron-sdk-plugins/'; const getPluginsZipName = () => { let flameshotZipName = 'flameshot_win.zip'; let lameZipName = 'lame_win.zip'; const plasoALDZipName = 'PlasoALD_2.0.zip'; if (platform === PLATFORM_TYPE.DARWIN) { lameZipName = 'lame_mac_x64.zip'; if (arch.includes('arm')) { flameshotZipName = 'flameshot_mac_arm64.zip'; } else { flameshotZipName = 'flameshot_mac_x64.zip'; } } return { flameshotZipName, lameZipName, plasoALDZipName }; }; const installCb = (url, reInstallCb, error, stdout, stderr) => { logger.info(`installCb 信息: ${url}, ${error?.message}, ${stdout}, ${stderr}`); if (error?.message) { reInstallCb?.(); } }; const npmInstall = () => { let agora_electron_sdk_v4 = agora_electron_sdk_v4_x32_in_win32; let trtc_electron_sdk = trtc_electron_sdk_x32_in_win32; if (platform === PLATFORM_TYPE.DARWIN) { agora_electron_sdk_v4 = agora_electron_sdk_v4_in_darwin; trtc_electron_sdk = trtc_electron_sdk_in_darwin; } else if (arch === ARCH_TYPE.X64) { agora_electron_sdk_v4 = agora_electron_sdk_v4_x64_in_win32; trtc_electron_sdk = trtc_electron_sdk_x64_in_win32; } let installAgoraNumber = 0; const installAgora = () => { installAgoraNumber++; if (installAgoraNumber > 3) return; exec(agora_electron_sdk_v4, async (error, stdout, stderr) => { installCb(agora_electron_sdk_v4, installAgora, error, stdout, stderr); if (!error?.message) { const oldAgoraV4Path = path.join(__dirname, '../node_modules', 'agora-electron-sdk'); const newAgoraV4Path = path.join(__dirname, '../../../', 'agora-electron-sdk-v4'); fs.move(oldAgoraV4Path, newAgoraV4Path, { overwrite: true }, (err) => { if (!err) { logger.info('更新agora_electron_sdk_v4成功'); } }); } }); }; installAgora(); let installTrtcNumber = 0; const installTrtc = () => { installTrtcNumber++; if (installTrtcNumber > 3) return; exec(trtc_electron_sdk, (error, stdout, stderr) => { installCb(trtc_electron_sdk, installTrtc, error, stdout, stderr); }); }; // installTrtc(); // const parentElectronRemote = path.join(__dirname, '../../../', '@electron/remote'); // fs.pathExists(parentElectronRemote).then((exists) => { // if (!exists) { // exec(electron_remote, (error, stdout, stderr) => { // installCb(electron_remote, () => {}, error, stdout, stderr); // const oldElectronRemote = path.join(__dirname, '../node_modules', '@electron'); // const newElectronRemote = path.join(__dirname, '../../../', '@electron'); // fs.move(oldElectronRemote, newElectronRemote, { overwrite: true }, (err) => {}); // }); // } // }); }; const downloadFile = (zipFileName, localDir) => { const remoteZipFilePath = `${electron_sdk_plugins_dir}${zipFileName}`; const localZipFilePath = path.join(localDir, zipFileName); logger.info(`will download ${zipFileName} to ${localZipFilePath}`); https .get(remoteZipFilePath, (response) => { const file = fs.createWriteStream(localZipFilePath); response.pipe(file); file.on('finish', () => { file.close(() => { logger.info(`文件${zipFileName}写入完成`); // 解压缩 if (platform === PLATFORM_TYPE.DARWIN) { execSync(`unzip -o ${localZipFilePath} -d ${localDir}`); } else { const zip = new AdmZip(localZipFilePath); zip.extractAllTo(localDir, true); } if (fs.existsSync(path.join(localDir, '__MACOSX'))) { fs.rmdir(path.join(localDir, '__MACOSX'), { recursive: true, force: true }); } fs.unlink(localZipFilePath, () => {}); logger.info(`文件${zipFileName}解压完成`); }); }); }) .on('error', (err) => { // 删除不完整的文件 fs.unlink(localZipFilePath, () => {}); logger.error(`download ${zipFileName} failed, delete it`, err.message); }); }; const downloadPrebuild = async () => { logger.info('Package Version: %s', packageVersion); logger.info('Platform: %s', platform); if (arch) logger.info('Arch: %s', arch); npmInstall(); const { flameshotZipName, plasoALDZipName } = getPluginsZipName(); const localPluginsDir = path.join(__dirname, '../lib'); if (!fs.existsSync(localPluginsDir)) { fs.mkdirSync(localPluginsDir); } downloadFile(flameshotZipName, localPluginsDir); if (platform === PLATFORM_TYPE.DARWIN) { downloadFile(plasoALDZipName, localPluginsDir); } }; downloadPrebuild();