|
| 1 | +import fetch from 'node-fetch'; |
| 2 | +import * as fs from 'fs/promises'; |
| 3 | +import * as path from 'path'; |
| 4 | +import os from 'os'; |
| 5 | +import AdmZip from 'adm-zip'; |
| 6 | +import { exec } from 'child_process'; |
| 7 | +import { promisify } from 'util'; |
| 8 | +import t from '../i18n/index.js'; |
| 9 | +import logger from '../libs/logger.js'; |
| 10 | + |
| 11 | +const execAsync = promisify(exec); |
| 12 | + |
| 13 | +function getBinDir(): string { |
| 14 | + const home = os.homedir(); |
| 15 | + |
| 16 | + return path.join(home || '', '.deno', 'bin'); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * 下载文件 |
| 21 | + * @param url 远程文件URL |
| 22 | + * @param dest 本地保存路径 |
| 23 | + */ |
| 24 | +export async function downloadFile(url: string, dest: string): Promise<void> { |
| 25 | + const response = await fetch(url); |
| 26 | + |
| 27 | + if (!response.ok) { |
| 28 | + throw new Error( |
| 29 | + `Error downloading file: ${response.status} ${response.statusText}` |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + const fileStream = await fs.open(dest, 'w'); |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + response.body?.pipe(fileStream.createWriteStream()); |
| 36 | + response.body?.on('error', (err: Error) => { |
| 37 | + fileStream.close(); |
| 38 | + reject(err); |
| 39 | + }); |
| 40 | + fileStream.createWriteStream().on('finish', () => { |
| 41 | + fileStream.close(); |
| 42 | + resolve(); |
| 43 | + }); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * 解压Zip文件 adm 是同步的 |
| 49 | + * @param zipPath Zip文件路径 |
| 50 | + * @param extractPath 解压目标目录 |
| 51 | + */ |
| 52 | +export function unzipFile(zipPath: string, extractPath: string): void { |
| 53 | + const zip = new AdmZip(zipPath); |
| 54 | + zip.extractAllTo(extractPath, true); |
| 55 | + logger.info(`UnzipFile success: from ${zipPath} to ${extractPath}`); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * 获取用户的 PATH 环境变量(win下专用) |
| 60 | + * @returns 用户 PATH |
| 61 | + */ |
| 62 | +async function getUserPath(): Promise<string> { |
| 63 | + const { stdout } = await execAsync('reg query "HKCU\\Environment" /v Path'); |
| 64 | + const match = stdout.match(/Path\s+REG_EXPAND_SZ\s+(.*)/i); |
| 65 | + if (match && match[1]) { |
| 66 | + return match[1].trim(); |
| 67 | + } |
| 68 | + return ''; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * 检查 BinDir 是否在用户的 PATH 中(win下专用) |
| 73 | + * @param binDir BinDir 路径 |
| 74 | + * @returns 是否包含 |
| 75 | + */ |
| 76 | +async function isBinDirInPath(binDir: string): Promise<boolean> { |
| 77 | + const userPath = await getUserPath(); |
| 78 | + return userPath |
| 79 | + .split(';') |
| 80 | + .map((p) => p.toLowerCase()) |
| 81 | + .includes(binDir.toLowerCase()); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * 将 BinDir 添加到用户的 PATH 环境变量(win下专用) |
| 86 | + * @param binDir BinDir 路径 |
| 87 | + */ |
| 88 | +async function addBinDirToPath(binDir: string): Promise<void> { |
| 89 | + // 使用 setx 添加到 PATH |
| 90 | + // setx 对 PATH 的长度有2047字符的限制 |
| 91 | + const command = `setx Path "%Path%;${binDir}"`; |
| 92 | + try { |
| 93 | + await execAsync(command); |
| 94 | + logger.info(`Path add success: ${binDir}`); |
| 95 | + } catch (error) { |
| 96 | + throw new Error(`Add BinDir to Path failed: ${error}`); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export async function downloadRuntimeAndUnzipForWindows() { |
| 101 | + try { |
| 102 | + const BinDir = getBinDir(); |
| 103 | + const DenoZip = path.join(BinDir, 'deno.zip'); |
| 104 | + const Target = 'x86_64-pc-windows-msvc'; |
| 105 | + const DownloadUrl = `http://esa-runtime.myalicdn.com/runtime/deno-${Target}.zip`; |
| 106 | + |
| 107 | + try { |
| 108 | + await fs.mkdir(BinDir, { recursive: true }); |
| 109 | + } catch (error) { |
| 110 | + const err = error as Error; |
| 111 | + logger.error(`mkdir error ${BinDir}: ${err.message}`); |
| 112 | + process.exit(1); |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + await downloadFile(DownloadUrl, DenoZip); |
| 117 | + logger.success( |
| 118 | + `${t('deno_download_success').d('Download success')}: ${DenoZip}` |
| 119 | + ); |
| 120 | + } catch (error) { |
| 121 | + const err = error as Error; |
| 122 | + logger.error( |
| 123 | + `${t('deno_download_failed').d('Download failed')}: ${err.message}` |
| 124 | + ); |
| 125 | + process.exit(1); |
| 126 | + } |
| 127 | + |
| 128 | + logger.info(`Unzip file to: ${BinDir}`); |
| 129 | + try { |
| 130 | + unzipFile(DenoZip, BinDir); |
| 131 | + } catch (error) { |
| 132 | + const err = error as Error; |
| 133 | + logger.error( |
| 134 | + `${t('deno_unzip_failed').d('Unzip failed')}: ${err.message}` |
| 135 | + ); |
| 136 | + process.exit(1); |
| 137 | + } |
| 138 | + |
| 139 | + try { |
| 140 | + await fs.unlink(DenoZip); |
| 141 | + logger.info(`Delete temp file: ${DenoZip}`); |
| 142 | + } catch (error) { |
| 143 | + logger.warn(`Delete temp file ${DenoZip} failed: ${error}`); |
| 144 | + } |
| 145 | + |
| 146 | + try { |
| 147 | + const inPath = await isBinDirInPath(BinDir); |
| 148 | + if (!inPath) { |
| 149 | + logger.info(`${BinDir} not in PATH, adding...`); |
| 150 | + await addBinDirToPath(BinDir); |
| 151 | + } else { |
| 152 | + logger.info(`${BinDir} in PATH already`); |
| 153 | + } |
| 154 | + } catch (error) { |
| 155 | + const err = error as Error; |
| 156 | + logger.error( |
| 157 | + `${t('deno_add_path_failed').d('Add BinDir to Path failed')}: ${err.message}` |
| 158 | + ); |
| 159 | + process.exit(1); |
| 160 | + } |
| 161 | + |
| 162 | + logger.success(t('deno_install_success').d('Runtime install success!')); |
| 163 | + } catch (error) { |
| 164 | + const err = error as Error; |
| 165 | + logger.error(`Download Error: ${err.message}`); |
| 166 | + process.exit(1); |
| 167 | + } |
| 168 | +} |
0 commit comments