Files
ProjectDashboard/lib/nested.js

117 lines
5.6 KiB
JavaScript
Raw Permalink Normal View History

2026-11-12 20:23:53 +01:00
// 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 }