unterordner scanner
This commit is contained in:
@@ -305,7 +305,8 @@ function lastFor(root, folder, targetId) {
|
||||
else if (d.last) {
|
||||
if (!Array.isArray(d.last.failed) && d.last.log) d.last.failed = failedFromLog(d.last.log, root, destOf(t, root)) ?? undefined
|
||||
if (Array.isArray(d.last.failed)) {
|
||||
full = d.last.failed.includes(folder)
|
||||
// failed lists top-level folders; a project in a subfolder counts by its top folder
|
||||
full = d.last.failed.includes(folder) || d.last.failed.includes(folder.split(path.sep)[0])
|
||||
? { at: d.last.at, ok: false, how: 'Komplett-Backup – Fehler in diesem Projekt' }
|
||||
: { at: d.last.at, ok: true, how: 'Komplett-Backup' }
|
||||
}
|
||||
|
||||
116
lib/nested.js
Normal file
116
lib/nested.js
Normal file
@@ -0,0 +1,116 @@
|
||||
// Projects in subfolders: finds project folders below a projects folder's direct
|
||||
// children, so they can be shown as projects of their own (chosen in the UI,
|
||||
// stored per projects folder in settings: roots[].nested / nestedIgnored).
|
||||
// - "collection" folders (no project markers themselves, e.g. "Arduino Projekte")
|
||||
// are searched up to MAX_DEPTH; a hit is not searched further
|
||||
// - deep: also sub-projects inside real projects (mostly parts of them, so the
|
||||
// UI never preselects these)
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const MAX_DEPTH = 3
|
||||
const SKIP = new Set(['node_modules', 'library', 'temp', 'obj', 'bin', 'build', 'builds', 'dist', 'out', '.git', '.venv', 'venv', '__pycache__', 'packages', 'releases', 'docs', 'itchio', 'sprite-generator', 'spritegenerator'])
|
||||
const STRONG = new Set(['git', 'dashboard', 'Unity', '.NET-sln', 'Unreal', 'Flutter'])
|
||||
const APPS = new Set(['.NET', 'Node', 'Gradle', 'Flutter', 'Python', 'HTML', 'Arduino'])
|
||||
const NOT_PROJECTS = new Set(['project-dashboard', '$recycle.bin', 'system volume information', 'recovery', 'config.msi'])
|
||||
|
||||
function ls(dir) {
|
||||
try { return fs.readdirSync(dir, { withFileTypes: true }) } catch { return [] }
|
||||
}
|
||||
|
||||
// project markers on the folder's own level (cheap: one readdir)
|
||||
function markers(dir) {
|
||||
const entries = ls(dir)
|
||||
const names = new Set(entries.map((e) => e.name.toLowerCase()))
|
||||
const files = entries.filter((e) => e.isFile()).map((e) => e.name.toLowerCase())
|
||||
const m = []
|
||||
if (names.has('.git')) m.push('git')
|
||||
if (names.has('.dashboard.json')) m.push('dashboard')
|
||||
if (names.has('assets') && names.has('projectsettings')) m.push('Unity')
|
||||
if (names.has('package.json')) m.push('Node')
|
||||
if (files.some((f) => f.endsWith('.sln'))) m.push('.NET-sln')
|
||||
else if (files.some((f) => f.endsWith('.csproj'))) m.push('.NET')
|
||||
if (names.has('pubspec.yaml')) m.push('Flutter')
|
||||
if (names.has('build.gradle') || names.has('build.gradle.kts')) m.push('Gradle')
|
||||
if (names.has('cmakelists.txt')) m.push('CMake')
|
||||
if (names.has('platformio.ini') || files.some((f) => f.endsWith('.ino'))) m.push('Arduino')
|
||||
if (names.has('cargo.toml')) m.push('Rust')
|
||||
if (names.has('go.mod')) m.push('Go')
|
||||
if (files.some((f) => f.endsWith('.py'))) m.push('Python')
|
||||
if (names.has('index.html')) m.push('HTML')
|
||||
if (files.some((f) => f.endsWith('.uproject'))) m.push('Unreal')
|
||||
if (names.has('readme.md') || names.has('claude.md')) m.push('README')
|
||||
return m
|
||||
}
|
||||
const isProject = (dir) => markers(dir).length > 0
|
||||
|
||||
const subdirs = (dir) => ls(dir).filter((e) => e.isDirectory() && !e.name.startsWith('.') && !SKIP.has(e.name.toLowerCase()))
|
||||
|
||||
// projects below `dir` (not `dir` itself), relative to `base`
|
||||
function findBelow(base, dir, depth, maxDepth, out) {
|
||||
if (depth > maxDepth) return
|
||||
for (const e of subdirs(dir)) {
|
||||
const full = path.join(dir, e.name)
|
||||
const m = markers(full)
|
||||
if (m.length) { out.push({ rel: path.relative(base, full), markers: m, depth }); continue }
|
||||
findBelow(base, full, depth + 1, maxDepth, out)
|
||||
}
|
||||
}
|
||||
|
||||
// { containers: [{ rel, projects: [{ rel, markers }] }], inProjects: [{ rel, markers, parent }] }
|
||||
function scan(rootPath, { deep = false } = {}) {
|
||||
const containers = []
|
||||
const inProjects = []
|
||||
for (const e of ls(rootPath)) {
|
||||
if (!e.isDirectory() || e.name.startsWith('.') || NOT_PROJECTS.has(e.name.toLowerCase()) || e.name === '_Tools') continue
|
||||
const dir = path.join(rootPath, e.name)
|
||||
if (!isProject(dir)) {
|
||||
const found = []
|
||||
findBelow(rootPath, dir, 1, MAX_DEPTH, found)
|
||||
if (found.length) containers.push({ rel: e.name, projects: found.map(({ rel, markers }) => ({ rel, markers })) })
|
||||
} else if (deep) {
|
||||
// inside a real project almost every folder has some marker (Engine\CMakeLists,
|
||||
// tools\*.py ...): only count what looks like a project of its own
|
||||
const found = []
|
||||
findBelow(rootPath, dir, 1, 2, found)
|
||||
// (a solution, a repo ...), or an app one folder deeper, e.g. Tools\DownloaderTool
|
||||
for (const f of found) {
|
||||
if (f.markers.some((m) => STRONG.has(m)) || (f.depth >= 2 && f.markers.some((m) => APPS.has(m)))) inProjects.push({ rel: f.rel, markers: f.markers, parent: e.name })
|
||||
}
|
||||
}
|
||||
}
|
||||
return { containers, inProjects }
|
||||
}
|
||||
|
||||
// "Arduino Projekte\KlopfSchloss" - normalised, inside the root, 2..MAX_DEPTH+1 levels, or null
|
||||
function cleanRel(rootPath, rel) {
|
||||
if (typeof rel !== 'string' || !rel.trim()) return null
|
||||
const norm = path.normalize(rel.trim()).replace(/^[\\/]+|[\\/]+$/g, '')
|
||||
const parts = norm.split(path.sep)
|
||||
if (parts.length < 2 || parts.length > MAX_DEPTH + 1 || parts.some((x) => !x || x === '.' || x === '..')) return null
|
||||
const full = path.resolve(rootPath, norm)
|
||||
if (!full.toLowerCase().startsWith(path.resolve(rootPath).toLowerCase() + path.sep)) return null
|
||||
return norm
|
||||
}
|
||||
|
||||
// how many collection-folder finds are neither chosen nor dismissed (cached)
|
||||
const hintCache = new Map()
|
||||
function hint(roots) {
|
||||
let n = 0
|
||||
for (const r of roots) {
|
||||
const key = `${r.path}|${(r.nested ?? []).join('|')}|${(r.nestedIgnored ?? []).join('|')}`
|
||||
let c = hintCache.get(r.path)
|
||||
if (!c || c.key !== key || Date.now() - c.at > 5 * 60 * 1000) {
|
||||
const known = new Set([...(r.nested ?? []), ...(r.nestedIgnored ?? [])].map((x) => x.toLowerCase()))
|
||||
let count = 0
|
||||
try { for (const k of scan(r.path).containers) for (const p of k.projects) if (!known.has(p.rel.toLowerCase())) count++ } catch {}
|
||||
c = { key, at: Date.now(), count }
|
||||
hintCache.set(r.path, c)
|
||||
}
|
||||
n += c.count
|
||||
}
|
||||
return n
|
||||
}
|
||||
const forgetHint = () => hintCache.clear()
|
||||
|
||||
module.exports = { scan, markers, isProject, cleanRel, hint, forgetHint, MAX_DEPTH }
|
||||
@@ -87,6 +87,21 @@ function cleanAuto(a, old) {
|
||||
|
||||
let current = null
|
||||
|
||||
// projects in subfolders: { nested: chosen relative paths, nestedIgnored: seen, not chosen }
|
||||
function nestedOf(r) {
|
||||
const list = (v) => (Array.isArray(v) ? [...new Set(v.filter((x) => typeof x === 'string' && x && !x.split(/[\\/]/).includes('..')))].slice(0, 200) : [])
|
||||
return { nested: list(r?.nested), nestedIgnored: list(r?.nestedIgnored) }
|
||||
}
|
||||
|
||||
// Stores the scanner's choice for one projects folder (paths already checked by lib/nested.js).
|
||||
function setNested(rootId, chosen, ignored) {
|
||||
const s = get()
|
||||
const roots = s.roots.map((r) => (r.id === rootId ? { ...r, ...nestedOf({ nested: chosen, nestedIgnored: ignored }) } : r))
|
||||
current = { ...s, roots }
|
||||
persist(current)
|
||||
return current
|
||||
}
|
||||
|
||||
function load() {
|
||||
const d = defaults()
|
||||
const saved = process.env.DASHBOARD_ROOT && !process.env.DASHBOARD_DATA ? null : readJson(FILE)
|
||||
@@ -96,10 +111,13 @@ function load() {
|
||||
backup.auto = cleanAuto(saved?.backup?.auto)
|
||||
// older settings had one "projectsRoot"
|
||||
let roots = Array.isArray(saved?.roots)
|
||||
? saved.roots.filter((r) => r && typeof r.path === 'string' && r.path).map((r) => ({ id: String(r.id || newId()), name: String(r.name || rootName(r.path)), path: r.path }))
|
||||
? saved.roots.filter((r) => r && typeof r.path === 'string' && r.path).map((r) => ({ id: String(r.id || newId()), name: String(r.name || rootName(r.path)), path: r.path, ...nestedOf(r) }))
|
||||
: typeof saved?.projectsRoot === 'string' ? rootsFrom(saved.projectsRoot) : d.roots
|
||||
// an explicit DASHBOARD_ROOT always wins (tests)
|
||||
if (process.env.DASHBOARD_ROOT) roots = [{ id: 'env', name: rootName(path.resolve(process.env.DASHBOARD_ROOT)), path: path.resolve(process.env.DASHBOARD_ROOT) }]
|
||||
if (process.env.DASHBOARD_ROOT) {
|
||||
const envPath = path.resolve(process.env.DASHBOARD_ROOT)
|
||||
roots = [{ id: 'env', name: rootName(envPath), path: envPath, ...nestedOf(roots.find((r) => samePath(r.path, envPath))) }]
|
||||
}
|
||||
current = withActive({
|
||||
roots,
|
||||
activeRoots: Array.isArray(saved?.activeRoots) ? saved.activeRoots : [saved?.activeRoot],
|
||||
@@ -177,6 +195,8 @@ function save(input) {
|
||||
id: old && samePath(old.path, p) ? old.id : newId(),
|
||||
name: String(r.name ?? '').trim().slice(0, 40) || rootName(p),
|
||||
path: p,
|
||||
// projects in subfolders (chosen in the scanner) stay with the folder
|
||||
...nestedOf(old && samePath(old.path, p) ? old : s.roots.find((x) => samePath(x.path, p))),
|
||||
})
|
||||
// a selected folder stays selected even if its path (and so its id) changed
|
||||
if (r.id) renamed.set(String(r.id), roots.at(-1).id)
|
||||
@@ -302,4 +322,4 @@ function subfolders(dir) {
|
||||
return { path: p, folders: list.slice(0, 200) }
|
||||
}
|
||||
|
||||
module.exports = { get, load, save, setActive, addRoots, activeRootList, subfolders, backupSubdir, configured, targetName, DATA_DIR, APP_DIR, DEFAULT_EXCLUDE_DIRS }
|
||||
module.exports = { setNested, get, load, save, setActive, addRoots, activeRootList, subfolders, backupSubdir, configured, targetName, DATA_DIR, APP_DIR, DEFAULT_EXCLUDE_DIRS }
|
||||
|
||||
@@ -57,8 +57,16 @@ function scriptFor(root) {
|
||||
function available() { return true }
|
||||
const cleanupAvailable = () => !!scriptFor(roots[0])
|
||||
|
||||
function psArgs(root, extra) {
|
||||
return ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', scriptFor(root), '-Root', root, ...extra, '-Json']
|
||||
// effRoot: the folder cleanup.ps1 treats as its root - for a project in a subfolder
|
||||
// ("Sammel\A") its parent folder, because the script only knows direct children
|
||||
function psArgs(root, extra, effRoot = root) {
|
||||
return ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', scriptFor(root), '-Root', effRoot, ...extra, '-Json']
|
||||
}
|
||||
// { effRoot, project, prefix } for cleanup.ps1; prefix turns its paths into "<folder>\..."
|
||||
function scriptTarget(root, folder) {
|
||||
const parent = path.dirname(folder)
|
||||
if (parent === '.' || !parent) return { effRoot: root, project: folder, prefix: '' }
|
||||
return { effRoot: path.join(root, parent), project: path.basename(folder), prefix: parent + path.sep }
|
||||
}
|
||||
|
||||
function parseJson(stdout) {
|
||||
@@ -125,11 +133,12 @@ async function sizeTree(dir) {
|
||||
|
||||
async function scanProject(root, folder) {
|
||||
const dir = path.join(root, folder)
|
||||
const st = scriptTarget(root, folder)
|
||||
const [split, cand] = await Promise.all([
|
||||
sizeTree(dir),
|
||||
scriptFor(root) ? run('powershell.exe', psArgs(root, ['-Project', folder, '-Tier', ALL_TIERS]), { timeout: 10 * 60 * 1000 }) : null,
|
||||
scriptFor(root) ? run('powershell.exe', psArgs(root, ['-Project', st.project, '-Tier', ALL_TIERS], st.effRoot), { timeout: 10 * 60 * 1000 }) : null,
|
||||
])
|
||||
const candidates = cand ? parseJson(cand.stdout) : []
|
||||
const candidates = (cand ? parseJson(cand.stdout) : []).map((c) => (st.prefix && typeof c.Path === 'string' ? { ...c, Path: st.prefix + c.Path } : c))
|
||||
const deletable = { Cache: 0, Release: 0, Output: 0 }
|
||||
for (const c of candidates) if (!c.Skip) deletable[c.Tier] += c.MB
|
||||
const d = (all[rootKey(root)] ??= {})
|
||||
@@ -159,7 +168,9 @@ async function clean(root, folder, paths) {
|
||||
const only = paths.filter((p) => typeof p === 'string' && p.startsWith(prefix) && !p.includes('|'))
|
||||
if (!only.length) throw new Error('nichts ausgewählt')
|
||||
if (!scriptFor(root)) throw new Error('cleanup.ps1 nicht gefunden')
|
||||
const r = await run('powershell.exe', psArgs(root, ['-Project', folder, '-Tier', ALL_TIERS, '-Only', only.join('|'), '-Apply']), { timeout: 30 * 60 * 1000 })
|
||||
const st = scriptTarget(root, folder)
|
||||
const rel = only.map((p) => p.slice(st.prefix.length))
|
||||
const r = await run('powershell.exe', psArgs(root, ['-Project', st.project, '-Tier', ALL_TIERS, '-Only', rel.join('|'), '-Apply'], st.effRoot), { timeout: 30 * 60 * 1000 })
|
||||
const result = parseJson(r.stdout)
|
||||
await scanProject(root, folder)
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user