|
#!/usr/bin/env node |
|
|
|
const https = require('https'); |
|
const os = require('os'); |
|
const path = require('path'); |
|
const crypto = require('crypto'); |
|
const url = require('url'); |
|
|
|
// ===== 1. 平台 & 架构映射 ===== |
|
function getPlatform() { |
|
const platform = process.platform; |
|
|
|
switch (platform) { |
|
case 'win32': |
|
return 'win32'; |
|
case 'darwin': |
|
return 'darwin'; |
|
case 'linux': |
|
return 'linux'; |
|
default: |
|
throw new Error(`Unsupported platform: ${platform}`); |
|
} |
|
} |
|
|
|
function getArch() { |
|
const arch = process.arch; |
|
|
|
switch (arch) { |
|
case 'x64': |
|
return 'x64'; |
|
case 'arm64': |
|
return 'arm64'; |
|
case 'ia32': |
|
return 'ia32'; |
|
default: |
|
throw new Error(`Unsupported arch: ${arch}`); |
|
} |
|
} |
|
|
|
// ===== 2. 获取最新 Electron 版本 ===== |
|
function getLatestVersion() { |
|
return new Promise((resolve, reject) => { |
|
https.get('https://registry.npmjs.org/electron/latest', (res) => { |
|
let data = ''; |
|
|
|
res.on('data', chunk => data += chunk); |
|
res.on('end', () => { |
|
try { |
|
const json = JSON.parse(data); |
|
resolve(json.version); |
|
} catch (err) { |
|
reject(err); |
|
} |
|
}); |
|
}).on('error', reject); |
|
}); |
|
} |
|
|
|
// ===== 3. 构造下载 URL ===== |
|
function getDownloadURL(version, platform, arch) { |
|
const base = 'https://github.com/electron/electron/releases/download'; |
|
const artifactName = 'electron'; |
|
const filename = `${artifactName}-v${version}-${platform}-${arch}.zip`; |
|
return `${base}/v${version}/${filename}`; |
|
} |
|
|
|
// ===== 4. 计算缓存目录(对齐 @electron/get 逻辑)===== |
|
function getCacheDirectory(downloadUrl) { |
|
const parsedDownloadUrl = url.parse(downloadUrl); |
|
const { pathname } = parsedDownloadUrl; |
|
|
|
// 去掉 filename,只保留目录部分 |
|
const strippedUrl = url.format({ |
|
protocol: parsedDownloadUrl.protocol, |
|
host: parsedDownloadUrl.host, |
|
pathname: path.dirname(pathname || 'electron') |
|
}); |
|
|
|
return crypto.createHash('sha256').update(strippedUrl).digest('hex'); |
|
} |
|
|
|
// ===== 5. 获取缓存根目录 ===== |
|
function getCacheRoot() { |
|
// 对齐 env-paths('electron', { suffix: '' }).cache 的逻辑 |
|
const platform = process.platform; |
|
const home = os.homedir(); |
|
|
|
switch (platform) { |
|
case 'win32': |
|
return path.join(home, 'AppData', 'Local', 'Cache', 'electron'); |
|
case 'darwin': |
|
return path.join(home, 'Library', 'Caches', 'electron'); |
|
case 'linux': |
|
return path.join(home, '.cache', 'electron'); |
|
default: |
|
throw new Error(`Unsupported platform: ${platform}`); |
|
} |
|
} |
|
|
|
// ===== 6. 获取文件名 ===== |
|
function getFileName(version, platform, arch) { |
|
return `electron-v${version}-${platform}-${arch}.zip`; |
|
} |
|
|
|
// ===== 主流程 ===== |
|
(async () => { |
|
try { |
|
// 获取版本(从参数或 npm) |
|
let version = process.argv[2]; |
|
if (!version) { |
|
console.log('未指定版本,正在查询最新版本...'); |
|
version = await getLatestVersion(); |
|
} |
|
|
|
// 获取平台信息 |
|
const platform = getPlatform(); |
|
const arch = getArch(); |
|
|
|
// 构造下载 URL |
|
const downloadUrl = getDownloadURL(version, platform, arch); |
|
const fileName = getFileName(version, platform, arch); |
|
|
|
// 计算缓存路径 |
|
const cacheRoot = process.env.electron_config_cache || getCacheRoot(); |
|
const cacheDir = getCacheDirectory(downloadUrl); |
|
const cachePath = path.join(cacheRoot, cacheDir, fileName); |
|
|
|
// 输出结果 |
|
console.log('\n========================================'); |
|
console.log('Electron 手动下载信息'); |
|
console.log('========================================\n'); |
|
console.log(`版本: ${version}`); |
|
console.log(`平台: ${platform}`); |
|
console.log(`架构: ${arch}`); |
|
console.log(`\n📥 下载 URL:`); |
|
console.log(`${downloadUrl}`); |
|
console.log(`\n💾 缓存路径:`); |
|
console.log(`${cachePath}`); |
|
console.log(`\n💡 说明:`); |
|
console.log(`1. 下载上述 URL 的 zip 文件`); |
|
console.log(`2. 将文件保存到缓存路径`); |
|
console.log(`3. 在项目中继续完成 electron 安装`); |
|
console.log('\n========================================\n'); |
|
|
|
} catch (err) { |
|
console.error('错误:', err.message); |
|
process.exit(1); |
|
} |
|
})(); |