feat: saving files and folders when updating modpaks
This commit is contained in:
@ -348,6 +348,7 @@ export function initMinecraftHandlers() {
|
||||
apiReleaseUrl = 'https://api.github.com/repos/DIKER0K/Comfort/releases/latest',
|
||||
versionFileName = 'comfort_version.txt',
|
||||
packName = 'Comfort',
|
||||
preserveFiles = [], // Новый параметр: список файлов/папок для сохранения
|
||||
} = options || {};
|
||||
|
||||
const appPath = path.dirname(app.getPath('exe'));
|
||||
@ -375,6 +376,7 @@ export function initMinecraftHandlers() {
|
||||
}
|
||||
|
||||
const tempDir = path.join(appPath, 'temp');
|
||||
const packDir = path.join(versionsDir, packName); // Директория пакета
|
||||
|
||||
// Создаем/очищаем временную директорию
|
||||
if (fs.existsSync(tempDir)) {
|
||||
@ -400,10 +402,61 @@ export function initMinecraftHandlers() {
|
||||
fs.mkdirSync(versionsDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Сохраняем файлы/папки, которые нужно оставить
|
||||
const backupDir = path.join(tempDir, 'backup');
|
||||
fs.mkdirSync(backupDir, { recursive: true });
|
||||
|
||||
// Проверка и бэкап указанных файлов/папок
|
||||
for (const filePath of preserveFiles) {
|
||||
const fullPath = path.join(packDir, filePath);
|
||||
|
||||
if (fs.existsSync(fullPath)) {
|
||||
const backupPath = path.join(backupDir, filePath);
|
||||
|
||||
// Создаем необходимые директории для бэкапа
|
||||
const backupDirPath = path.dirname(backupPath);
|
||||
if (!fs.existsSync(backupDirPath)) {
|
||||
fs.mkdirSync(backupDirPath, { recursive: true });
|
||||
}
|
||||
|
||||
// Копируем файл или директорию
|
||||
if (fs.lstatSync(fullPath).isDirectory()) {
|
||||
fs.cpSync(fullPath, backupPath, { recursive: true });
|
||||
console.log(`Директория ${filePath} сохранена во временный бэкап`);
|
||||
} else {
|
||||
fs.copyFileSync(fullPath, backupPath);
|
||||
console.log(`Файл ${filePath} сохранен во временный бэкап`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Распаковываем архив напрямую в папку versions
|
||||
await extract(zipPath, { dir: versionsDir });
|
||||
fs.unlinkSync(zipPath);
|
||||
|
||||
// Восстанавливаем файлы/папки из бэкапа
|
||||
for (const filePath of preserveFiles) {
|
||||
const backupPath = path.join(backupDir, filePath);
|
||||
if (fs.existsSync(backupPath)) {
|
||||
const targetPath = path.join(packDir, filePath);
|
||||
|
||||
// Создаем необходимые директории для восстановления
|
||||
const targetDirPath = path.dirname(targetPath);
|
||||
if (!fs.existsSync(targetDirPath)) {
|
||||
fs.mkdirSync(targetDirPath, { recursive: true });
|
||||
}
|
||||
|
||||
// Копируем обратно файл или директорию
|
||||
if (fs.lstatSync(backupPath).isDirectory()) {
|
||||
fs.cpSync(backupPath, targetPath, { recursive: true });
|
||||
console.log(`Директория ${filePath} восстановлена из бэкапа`);
|
||||
} else {
|
||||
fs.copyFileSync(backupPath, targetPath);
|
||||
console.log(`Файл ${filePath} восстановлен из бэкапа`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Сохраняем новую версию
|
||||
fs.writeFileSync(versionFilePath, latestVersion);
|
||||
|
||||
|
Reference in New Issue
Block a user