code cleanup

This commit is contained in:
olcxja 2026-09-26 10:13:50 +02:00
commit e8cd664965
8 changed files with 362 additions and 406 deletions

View file

@ -2,24 +2,12 @@
const fsp = require('fs').promises
const path = require('path')
const fs = require('fs')
const { DIRS } = require('./paths')
const { newGame } = require('./library')
const { dedupeDir } = require('./fsutil')
function walk(dir, base) {
return fsp.readdir(dir, { withFileTypes: true }).then(async entries => {
let total = 0
for (const e of entries) {
const full = path.join(dir, e.name)
if (e.isDirectory()) total += await walk(full, base)
else if (e.isFile()) {
const st = await fsp.stat(full)
total += st.size
}
}
return total
}).catch(() => 0)
}
// Directories that typically hold installers/redistributables, not the game.
const SKIPPED_DIRS = new Set(['redist', 'directx', '_commonredist', 'support', 'dotnet', 'vc'])
function copyRecursive(src, dst, onProgress) {
return fsp.readdir(src, { withFileTypes: true }).then(async entries => {
@ -38,41 +26,28 @@ function copyRecursive(src, dst, onProgress) {
}
function moveFolder(src, dst, onProgress) {
if (path.parse(src).root !== path.parse(dst).root) {
return copyRecursive(src, dst, onProgress).then(() => fsp.rm(src, { recursive: true, force: true }))
}
return fsp.rename(src, dst).catch(() => copyRecursive(src, dst, onProgress).then(() => fsp.rm(src, { recursive: true, force: true })))
}
function dedupeName(base, name) {
let candidate = path.join(base, name)
let n = 2
while (fs.existsSync(candidate)) {
candidate = path.join(base, `${name} (${n})`)
n++
}
return candidate
const fallback = () => copyRecursive(src, dst, onProgress).then(() => fsp.rm(src, { recursive: true, force: true }))
if (path.parse(src).root !== path.parse(dst).root) return fallback()
return fsp.rename(src, dst).catch(fallback)
}
async function importGame(srcDir, mode, onProgress) {
const name = path.basename(srcDir)
let workdir = srcDir
if (mode === 'copy') {
const dst = dedupeName(DIRS.APPS, name)
await copyRecursive(srcDir, dst, onProgress)
workdir = dst
} else if (mode === 'move') {
const dst = dedupeName(DIRS.APPS, name)
await moveFolder(srcDir, dst, onProgress)
if (mode === 'copy' || mode === 'move') {
const dst = dedupeDir(DIRS.APPS, name)
if (mode === 'copy') await copyRecursive(srcDir, dst, onProgress)
else await moveFolder(srcDir, dst, onProgress)
workdir = dst
}
const exePath = await findDefaultExe(workdir)
const game = newGame({ name, exePath, workdir, mode })
return game
return newGame({ name, exePath, workdir, mode })
}
// Largest .exe in the tree, skipping redistributable folders.
async function findDefaultExe(dir) {
let best = ''
let bestSize = -1
const walk = async d => {
let entries = []
try {
@ -83,12 +58,13 @@ async function findDefaultExe(dir) {
for (const e of entries) {
const full = path.join(d, e.name)
if (e.isDirectory()) {
const lower = e.name.toLowerCase()
if (lower === 'redist' || lower === 'directx' || lower === '_commonredist' || lower === 'support' || lower === 'dotnet' || lower === 'vc') continue
await walk(full)
if (!SKIPPED_DIRS.has(e.name.toLowerCase())) await walk(full)
} else if (e.name.toLowerCase().endsWith('.exe')) {
const st = await fsp.stat(full)
if (!best || st.size > (await fsp.stat(best)).size) best = full
const size = await fsp.stat(full).then(st => st.size).catch(() => -1)
if (size > bestSize) {
best = full
bestSize = size
}
}
}
}
@ -96,4 +72,4 @@ async function findDefaultExe(dir) {
return best
}
module.exports = { importGame, findDefaultExe, copyRecursive, moveFolder, walk }
module.exports = { importGame, findDefaultExe, copyRecursive, moveFolder }