unterordner scanner

This commit is contained in:
2026-11-12 20:23:53 +01:00
parent 7824ddb6dc
commit 0a38884eac
8 changed files with 390 additions and 32 deletions

View File

@@ -34,6 +34,7 @@ const itch = require('./lib/itch')
const create = require('./lib/create')
const archive = require('./lib/archive')
const itchpack = require('./lib/itchpack')
const nested = require('./lib/nested')
const scheduler = require('./lib/scheduler')
// The projects folder comes from the settings (lib/settings.js) and can be
@@ -206,18 +207,31 @@ function projectList() {
for (const root of ROOTS) {
let entries
try { entries = fs.readdirSync(root.path, { withFileTypes: true }) } catch { continue }
for (const e of entries) {
if (e.isDirectory() && !e.name.startsWith('.') && !NOT_PROJECTS.has(e.name.toLowerCase())) raw.push({ folder: e.name, root })
// projects in subfolders chosen in the scanner (lib/nested.js); a pure
// collection folder above them is hidden, a real project stays
const sub = (root.nested ?? []).filter((rel) => { try { return fs.statSync(path.join(root.path, rel)).isDirectory() } catch { return false } })
const hide = new Set()
for (const rel of sub) {
const top = rel.split(path.sep)[0]
if (!nested.isProject(path.join(root.path, top))) hide.add(top.toLowerCase())
}
for (const e of entries) {
if (e.isDirectory() && !e.name.startsWith('.') && !NOT_PROJECTS.has(e.name.toLowerCase()) && !hide.has(e.name.toLowerCase())) raw.push({ folder: e.name, root, parent: '' })
}
for (const rel of sub) raw.push({ folder: rel, root, parent: path.dirname(rel) })
}
// key: the folder's name; on a clash "(parent folder)" / "(projects folder)" is added
const base = (p) => path.basename(p.folder)
const count = new Map()
for (const p of raw) count.set(p.folder.toLowerCase(), (count.get(p.folder.toLowerCase()) ?? 0) + 1)
for (const p of raw) count.set(base(p).toLowerCase(), (count.get(base(p).toLowerCase()) ?? 0) + 1)
const used = new Set()
const list = raw.map((p) => {
let key = count.get(p.folder.toLowerCase()) > 1 ? `${p.folder} (${p.root.name})` : p.folder
const b = base(p)
let key = count.get(b.toLowerCase()) > 1 ? `${b} (${p.parent ? path.basename(p.parent) : p.root.name})` : b
if (used.has(key.toLowerCase())) key = `${b} (${p.root.name})`
while (used.has(key.toLowerCase())) key += ` [${p.root.id}]`
used.add(key.toLowerCase())
return { key, folder: p.folder, root: p.root }
return { key, folder: p.folder, root: p.root, parent: p.parent }
})
projMap = new Map(list.map((p) => [p.key, p]))
return list
@@ -243,7 +257,9 @@ function keyOf(rootPath, folder) {
function resolveProject(key) {
const p = projectRef(key)
if (!p) return null
if (!fs.existsSync(p.path) || !fs.statSync(p.path).isDirectory() || path.dirname(p.path) !== p.root.path) return null
// a direct child of a shown projects folder, or a chosen project in a subfolder (projMap)
const inside = p.path.toLowerCase().startsWith(p.root.path.toLowerCase() + path.sep)
if (!inside || !fs.existsSync(p.path) || !fs.statSync(p.path).isDirectory()) return null
return p.path
}
@@ -281,6 +297,7 @@ async function describeProject(p, target) {
return {
name: p.key,
folder: p.folder,
parent: p.parent ?? '',
root: p.root.id,
rootName: p.root.name,
mtime: stat.mtime.toISOString(),
@@ -294,7 +311,7 @@ async function describeProject(p, target) {
lastBackup: backup.lastFor(p.root.path, p.folder, target),
storage: storageSummary(p),
...meta,
published: itch.forProject(p.folder, meta.itch),
published: itch.forProject(path.basename(p.folder), meta.itch),
}
}
@@ -375,13 +392,15 @@ app.get('/api/projects', handle(async (req, res) => {
cleanupAvailable: storage.available(),
cleanAvailable: storage.cleanupAvailable(),
activityPending: activity.pending(),
// projects in subfolders found but not yet chosen/dismissed (lib/nested.js)
nestedHint: ROOTS.length ? nested.hint(s.roots.filter((r) => fs.existsSync(r.path))) : 0,
})
}))
// itch.io profile: games found + which project claims them (lib/itch.js)
async function itchStatus() {
const s = itch.state
const claimed = projectList().map((p) => itch.forProject(p.folder, readStatusFile(path.join(p.root.path, p.folder)).itch)?.url).filter(Boolean)
const claimed = projectList().map((p) => itch.forProject(path.basename(p.folder), readStatusFile(path.join(p.root.path, p.folder)).itch)?.url).filter(Boolean)
return { profile: s.profile, at: s.at, error: s.error, games: s.games, unmatched: itch.unmatched(claimed) }
}
app.get('/api/itch', handle(async (req, res) => { await itch.ensure(); res.json(await itchStatus()) }))
@@ -531,21 +550,34 @@ app.post('/api/projects/:name/move', handle(async (req, res) => {
if (!ref) return res.status(404).json({ error: 'unknown project' })
const root = settings.get().roots.find((r) => r.id === (req.body?.root || ref.root.id))
if (!root || !fs.existsSync(root.path)) return res.status(400).json({ error: 'Projektordner nicht gefunden.' })
// a project in a subfolder is renamed where it is (same projects folder);
// moved to another projects folder it lands at the top there
const stay = ref.parent && root.id === ref.root.id
let r
try {
r = await create.move({ from: ref.path, rootPath: root.path, name: req.body?.name })
r = await create.move({ from: ref.path, rootPath: stay ? path.join(root.path, ref.parent) : root.path, name: req.body?.name })
} catch (e) {
return res.status(400).json({ error: String(e.message ?? e) })
}
const newRoot = path.resolve(root.path)
storage.rename(ref.root.path, ref.folder, newRoot, r.name)
const newFolder = stay ? path.join(ref.parent, r.name) : r.name
if (ref.parent) {
const old = settings.get().roots.find((x) => x.id === ref.root.id)
if (old) {
const keep = (old.nested ?? []).filter((x) => x.toLowerCase() !== ref.folder.toLowerCase())
settings.setNested(old.id, stay ? [...keep, newFolder] : keep, old.nestedIgnored ?? [])
nested.forgetHint()
applySettings()
}
}
storage.rename(ref.root.path, ref.folder, newRoot, newFolder)
thumbCache.delete(key)
quickCache.delete(key)
icons.forget(ref.path)
projectList()
const shown = ROOTS.some((x) => x.id === root.id)
if (shown) activity.warm([r.path])
res.json({ ok: true, name: r.name, root: root.id, rootName: root.name, shown, key: shown ? keyOf(newRoot, r.name) : r.name })
res.json({ ok: true, name: r.name, root: root.id, rootName: root.name, shown, key: shown ? keyOf(newRoot, newFolder) : r.name })
}))
// Adds projects folders from the dropdown. body: { paths: [...], activate }
app.post('/api/roots/add', (req, res) => {
@@ -559,6 +591,46 @@ app.post('/api/roots/add', (req, res) => {
res.status(400).json({ error: String(e.message ?? e) })
}
})
// Projects in subfolders (lib/nested.js): scan every registered projects folder.
// ?deep=1 also lists sub-projects inside real projects.
app.get('/api/nested/scan', (req, res) => {
const deep = req.query.deep === '1'
const out = settings.get().roots.filter((r) => fs.existsSync(r.path)).map((r) => {
const chosen = new Set((r.nested ?? []).map((x) => x.toLowerCase()))
const ignored = new Set((r.nestedIgnored ?? []).map((x) => x.toLowerCase()))
const flag = (x) => ({ ...x, chosen: chosen.has(x.rel.toLowerCase()), ignored: ignored.has(x.rel.toLowerCase()) })
const sc = nested.scan(r.path, { deep })
// chosen entries the scan doesn't show any more (e.g. inside a real project, scanned without deep)
const seen = new Set([...sc.containers.flatMap((c) => c.projects), ...sc.inProjects].map((x) => x.rel.toLowerCase()))
const extra = (r.nested ?? []).filter((x) => !seen.has(x.toLowerCase()) && fs.existsSync(path.join(r.path, x)))
.map((rel) => ({ rel, markers: nested.markers(path.join(r.path, rel)), parent: rel.split(path.sep)[0] }))
return {
id: r.id, name: r.name, path: r.path,
containers: sc.containers.map((c) => ({ rel: c.rel, projects: c.projects.map(flag) })),
inProjects: [...sc.inProjects, ...extra].map(flag),
}
})
res.json({ roots: out, deep })
})
// Stores the choice. body: { roots: { <rootId>: { chosen: [rel], ignored: [rel] } } }
app.post('/api/nested', (req, res) => {
if (storage.busy() || backup.busy() || create.busy()) return res.status(409).json({ error: 'Erst warten, bis Scan/Backup fertig ist.' })
const input = req.body?.roots
if (!input || typeof input !== 'object') return res.status(400).json({ error: 'roots fehlt' })
let chosenCount = 0
for (const r of settings.get().roots) {
const v = input[r.id]
if (!v) continue
const ok = (list) => (Array.isArray(list) ? list : []).map((x) => nested.cleanRel(r.path, x))
.filter((x) => x && fs.existsSync(path.join(r.path, x)) && fs.statSync(path.join(r.path, x)).isDirectory())
const chosen = ok(v.chosen)
settings.setNested(r.id, chosen, ok(v.ignored).filter((x) => !chosen.includes(x)))
chosenCount += chosen.length
}
nested.forgetHint()
applySettings()
res.json({ ok: true, chosen: chosenCount, projects: projectList().length })
})
// Subfolders of a folder, for adding several projects folders at once
app.get('/api/subfolders', (req, res) => {
try { res.json(settings.subfolders(String(req.query.path ?? ''))) } catch (e) { res.status(400).json({ error: String(e.message ?? e) }) }