Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit c60d87e

Browse files
fix(refactor): Move some functions to utils & add mkdir before unzipper.Extract
1 parent 04b0450 commit c60d87e

File tree

2 files changed

+51
-38
lines changed

2 files changed

+51
-38
lines changed

src/main/modules/patcher.js

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import {
2626
isWin,
2727
isYandexMusicRunning,
2828
launchYandexMusic,
29-
unzipFolder
29+
unzipFolder,
30+
createDirIfNotExist,
31+
copyFile,
32+
decompressFile,
3033
} from "./utils.js";
3134
import { LATEST_MOD_RELEASE_URL, YM_RELEASE_METADATA_URL } from '../constants/urls.js';
3235

@@ -258,42 +261,6 @@ async function downloadAsar(callback, metadata) {
258261

259262
}
260263

261-
async function copyFile(target, dest) {
262-
try {
263-
await fso.promises.copyFile(target, dest);
264-
} catch (error) {
265-
if (process.platform === 'linux' && error.code === 'EACCES') {
266-
const encodedTarget = target.replaceAll("'", "\\'");
267-
const encodedDest = dest.replaceAll("'", "\\'");
268-
await execFileAsync('pkexec', ['bash', '-c', `cp '${encodedTarget}' '${encodedDest}'`]);
269-
} else {
270-
logger.error('File copying failed:', error);
271-
}
272-
}
273-
}
274-
275-
async function createDirIfNotExist(target) {
276-
if (!fs.existsSync(target)) {
277-
try {
278-
await fsp.mkdir(target);
279-
} catch (error) {
280-
if (process.platform === 'linux' && error.code === 'EACCES') {
281-
const encodedTarget = target.replaceAll("'", "\\'");
282-
await execFileAsync('pkexec', ['bash', '-c', `mkdir -p '${encodedTarget}'`]);
283-
} else {
284-
logger.error('Directory creation failed:', error)
285-
}
286-
}
287-
}
288-
}
289-
290-
async function decompressFile(target, dest, compressionType) {
291-
const compressedData = await fso.promises.readFile(target);
292-
293-
const decompressedData = await (compressionType === 'zst' ? zstdDecompressPromise(compressedData) : unzipPromise(compressedData));
294-
295-
await fso.promises.writeFile(dest, decompressedData);
296-
}
297264

298265
export async function getReleaseMetadata(releaseUrl = undefined) {
299266
try {

src/main/modules/utils.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import path from "path";
22
import { promisify } from 'util'
33
import { pipeline } from 'stream/promises';
4-
import { exec, spawn } from 'child_process'
4+
import { exec, spawn, execFile } from 'child_process'
55
import { app, nativeImage } from "electron";
66
import axios from "axios";
77
import fso from "original-fs";
88
import unzipper from "unzipper";
99
import fs from "fs";
10+
import * as fsp from "fs/promises";
1011
import { Logger } from "./Logger.js";
1112
import { YM_ASAR_PATH } from './patcher.js';
13+
import zlib from 'node:zlib';
1214

1315

1416
const execAsync = promisify(exec);
17+
const execFileAsync = promisify(execFile);
1518
const spawnAsync = promisify(spawn);
19+
const unzipPromise = promisify(zlib.unzip);
20+
const zstdDecompressPromise = zlib.zstdDecompress ? promisify(zlib.zstdDecompress) : undefined;
21+
1622
const logger = new Logger("utils");
1723

1824
export const isWin = process.platform === 'win32';
@@ -197,7 +203,47 @@ export function formatTimeStampDiff(date1, date2) {
197203
return result;
198204
}
199205

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+
200243
export async function unzipFolder(zipPath, outputFolder) {
244+
245+
await createDirIfNotExist(outputFolder);
246+
201247
await new Promise((resolve, reject) => {
202248
fs.createReadStream(zipPath)
203249
.pipe(unzipper.Extract({ path: outputFolder }))

0 commit comments

Comments
 (0)