|
1 | 1 | import path from "path"; |
2 | 2 | import { promisify } from 'util' |
3 | 3 | import { pipeline } from 'stream/promises'; |
4 | | -import { exec, spawn } from 'child_process' |
| 4 | +import { exec, spawn, execFile } from 'child_process' |
5 | 5 | import { app, nativeImage } from "electron"; |
6 | 6 | import axios from "axios"; |
7 | 7 | import fso from "original-fs"; |
8 | 8 | import unzipper from "unzipper"; |
9 | 9 | import fs from "fs"; |
| 10 | +import * as fsp from "fs/promises"; |
10 | 11 | import { Logger } from "./Logger.js"; |
11 | 12 | import { YM_ASAR_PATH } from './patcher.js'; |
| 13 | +import zlib from 'node:zlib'; |
12 | 14 |
|
13 | 15 |
|
14 | 16 | const execAsync = promisify(exec); |
| 17 | +const execFileAsync = promisify(execFile); |
15 | 18 | const spawnAsync = promisify(spawn); |
| 19 | +const unzipPromise = promisify(zlib.unzip); |
| 20 | +const zstdDecompressPromise = zlib.zstdDecompress ? promisify(zlib.zstdDecompress) : undefined; |
| 21 | + |
16 | 22 | const logger = new Logger("utils"); |
17 | 23 |
|
18 | 24 | export const isWin = process.platform === 'win32'; |
@@ -197,7 +203,47 @@ export function formatTimeStampDiff(date1, date2) { |
197 | 203 | return result; |
198 | 204 | } |
199 | 205 |
|
| 206 | +export async function copyFile(target, dest) { |
| 207 | + try { |
| 208 | + await fso.promises.copyFile(target, dest); |
| 209 | + } catch (error) { |
| 210 | + if (process.platform === 'linux' && error.code === 'EACCES') { |
| 211 | + const encodedTarget = target.replaceAll("'", "\\'"); |
| 212 | + const encodedDest = dest.replaceAll("'", "\\'"); |
| 213 | + await execFileAsync('pkexec', ['bash', '-c', `cp '${encodedTarget}' '${encodedDest}'`]); |
| 214 | + } else { |
| 215 | + logger.error('File copying failed:', error); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +export async function createDirIfNotExist(target) { |
| 221 | + if (!fs.existsSync(target)) { |
| 222 | + try { |
| 223 | + await fsp.mkdir(target); |
| 224 | + } catch (error) { |
| 225 | + if (process.platform === 'linux' && error.code === 'EACCES') { |
| 226 | + const encodedTarget = target.replaceAll("'", "\\'"); |
| 227 | + await execFileAsync('pkexec', ['bash', '-c', `mkdir -p '${encodedTarget}'`]); |
| 228 | + } else { |
| 229 | + logger.error('Directory creation failed:', error) |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +export async function decompressFile(target, dest, compressionType) { |
| 236 | + const compressedData = await fso.promises.readFile(target); |
| 237 | + |
| 238 | + const decompressedData = await (compressionType === 'zst' ? zstdDecompressPromise(compressedData) : unzipPromise(compressedData)); |
| 239 | + |
| 240 | + await fso.promises.writeFile(dest, decompressedData); |
| 241 | +} |
| 242 | + |
200 | 243 | export async function unzipFolder(zipPath, outputFolder) { |
| 244 | + |
| 245 | + await createDirIfNotExist(outputFolder); |
| 246 | + |
201 | 247 | await new Promise((resolve, reject) => { |
202 | 248 | fs.createReadStream(zipPath) |
203 | 249 | .pipe(unzipper.Extract({ path: outputFolder })) |
|
0 commit comments