// Detects what a project is made of (Unity, Node, .NET, ...) from marker files // and builds the list of start actions. Actions are always built here on the // server; the browser only sends back an action id. const fs = require('fs') const path = require('path') const SKIP = new Set([ 'node_modules', '.git', 'dist', 'build', 'builds', 'out', 'bin', 'obj', 'target', 'venv', '.venv', 'env', '__pycache__', '.gradle', '.idea', '.vscode', 'Library', 'Temp', 'Logs', 'output', '_archiv', '_Archiv', 'sprite-generator', 'Spritegenerator', 'sprite-gen', 'design-gen', '.expo', '.claude', ]) const NPM_SCRIPTS = ['dev', 'start', 'electron', 'android', 'web', 'build', 'preview', 'serve', 'tauri'] const PY_MAIN = ['main.py', 'app.py', 'run.py', 'game.py', 'server.py', 'gui.py'] const UNITY_EDITORS = 'C:\\Program Files\\Unity\\Hub\\Editor' const UNITY_HUB = 'C:\\Program Files\\Unity Hub\\Unity Hub.exe' function ls(dir) { try { return fs.readdirSync(dir, { withFileTypes: true }) } catch { return [] } } function readJson(file) { try { return JSON.parse(fs.readFileSync(file, 'utf-8')) } catch { return null } } // Looks at the project root and its direct subfolders (e.g. "DesktopAnt\DesktopAnt"). function detect(projectPath) { const subdirs = ls(projectPath) .filter((e) => e.isDirectory() && !e.name.startsWith('.') && !SKIP.has(e.name)) .map((e) => path.join(projectPath, e.name)) const found = [] for (const dir of [projectPath, ...subdirs]) { const isRoot = dir === projectPath const entries = ls(dir) const names = new Set(entries.map((e) => e.name)) const files = entries.filter((e) => e.isFile()).map((e) => e.name) const rel = path.relative(projectPath, dir) const unity = names.has('Assets') && names.has('ProjectSettings') if (unity) found.push({ type: 'unity', dir, rel }) if (names.has('package.json')) { const pkg = readJson(path.join(dir, 'package.json')) ?? {} const deps = { ...(pkg.dependencies ?? {}), ...(pkg.devDependencies ?? {}) } const flavor = deps.electron ? 'electron' : deps.expo ? 'expo' : deps['@capacitor/core'] ? 'capacitor' : deps.vite ? 'vite' : deps.react ? 'react' : null found.push({ type: 'node', dir, rel, flavor, scripts: Object.keys(pkg.scripts ?? {}), scriptCmds: pkg.scripts ?? {}, needsInstall: Object.keys(deps).length > 0 && !names.has('node_modules'), }) } if (!unity) { const sln = files.filter((f) => /\.sln$/i.test(f)) const proj = files.filter((f) => /\.(cs|fs|vb)proj$/i.test(f)) if (sln.length || proj.length) found.push({ type: 'dotnet', dir, rel, sln, proj }) } if (names.has('pubspec.yaml')) found.push({ type: 'flutter', dir, rel }) if (['build.gradle', 'build.gradle.kts', 'settings.gradle', 'settings.gradle.kts'].some((n) => names.has(n))) { found.push({ type: 'android', dir, rel, gradlew: names.has('gradlew.bat') }) } if (names.has('CMakeLists.txt')) found.push({ type: 'cmake', dir, rel }) const ino = files.filter((f) => /\.ino$/i.test(f)) if (ino.length) found.push({ type: 'arduino', dir, rel, ino }) if (isRoot) { const py = files.filter((f) => /\.py$/i.test(f)) if (py.length || names.has('requirements.txt') || names.has('pyproject.toml')) { const venv = path.join(dir, '.venv', 'Scripts', 'python.exe') found.push({ type: 'python', dir, rel, py, venv: fs.existsSync(venv) ? venv : null }) } } } return found } const TYPE_LABEL = { unity: 'Unity', node: 'Node', dotnet: '.NET', flutter: 'Flutter', android: 'Android', cmake: 'C++/CMake', arduino: 'Arduino', python: 'Python', web: 'Web', } function summary(found) { const labels = [] for (const f of found) { const l = f.type === 'node' && f.flavor ? f.flavor[0].toUpperCase() + f.flavor.slice(1) : TYPE_LABEL[f.type] if (!labels.includes(l)) labels.push(l) } return { types: labels, needsInstall: found.some((f) => f.type === 'node' && f.needsInstall) } } function unityEditor(dir) { try { const txt = fs.readFileSync(path.join(dir, 'ProjectSettings', 'ProjectVersion.txt'), 'utf-8') const ver = txt.match(/m_EditorVersion:\s*(\S+)/)?.[1] const exe = ver && path.join(UNITY_EDITORS, ver, 'Editor', 'Unity.exe') return { ver, exe: exe && fs.existsSync(exe) ? exe : null } } catch { return { ver: null, exe: null } } } // ── startable programs & scripts ───────────────────────────────────────── // Folders that never hold the project's own program (tests, drivers, caches, ...) const EXE_SKIP_DIRS = /^(node_modules|\.git|\.gradle|\.gradle-local|gradle|Library|Temp|Logs|obj|\.venv.*|venv|env|__pycache__|tests?|Tests|symbols|CMakeFiles|_deps|drivers?|_archiv|output|sprite-?generator|spritegenerator|sprite-?gen|design-?gen|\.expo|\.idea|\.vscode|\.cache|\.claude|android|ios|packages|Packages)$/i // Helper executables that are not "the app" const EXE_SKIP_NAMES = /^(unins.*|.*setup.*|.*install.*|.*redist.*|.*crash.?handler.*|createdump|butler|adb|aapt2?|ffmpeg|ffprobe|7za?|python.*|pythonw|node|npm|remove|remover|elevate|squirrel|update|dotnet|apphost|notification_helper|chrome_.*|.*\.vshost)$/i // Programs that exist next to the real app but are not what you want to start first const EXE_SECONDARY = /(server|helper|updater|console|cli|worker|daemon|service)$/i const SCRIPT_SKIP = /^(gradlew|gradle|gen|ctlstisc)\.(bat|cmd)$/i // Where a copy of an exe came from - better sources win when the same exe exists several times function exeRank(rel) { const r = rel.toLowerCase() if (/(^|\\)publish[^\\]*\\/.test(r)) return 6 if (/(^|\\)release\\/.test(r) && !/\\bin\\/.test(r)) return 5 if (/(^|\\)(dist|builds?|spiel|game)\\/.test(r)) return 4 if (!r.includes('\\')) return 4 // next to the project if (/\\bin\\release\\/.test(r) || /^bin\\release\\/.test(r)) return 3 if (/\\bin\\debug\\/.test(r) || /^bin\\debug\\/.test(r)) return 1 return 2 } const norm = (s) => s.toLowerCase().replace(/[^a-z0-9]/g, '') function walkFiles(root, maxDepth, test) { const out = [] const queue = [{ dir: root, depth: 0 }] let seen = 0 while (queue.length) { const { dir, depth } = queue.shift() for (const e of ls(dir)) { if (++seen > 20000) return out if (e.isDirectory()) { // "_name" subfolders hold vendored or archived stuff (_vbcable, _archiv, ...) if (depth < maxDepth && !EXE_SKIP_DIRS.test(e.name) && !e.name.startsWith('_')) queue.push({ dir: path.join(dir, e.name), depth: depth + 1 }) } else if (e.isFile() && test(e.name)) out.push(path.join(dir, e.name)) } } return out } function ago(ms) { const d = Math.floor((Date.now() - ms) / 86400000) return d <= 0 ? 'heute' : d === 1 ? 'gestern' : d < 30 ? `vor ${d} T.` : d < 365 ? `vor ${Math.floor(d / 30)} Mon.` : `vor ${Math.floor(d / 365)} J.` } // The project's own programs: one entry per exe name (best copy), the one // named like the project first, then newest. function findPrograms(name, projectPath) { const best = new Map() for (const file of walkFiles(projectPath, 6, (n) => /\.exe$/i.test(n) && !EXE_SKIP_NAMES.test(n.replace(/\.exe$/i, '')))) { const rel = path.relative(projectPath, file) let mtime = 0 try { mtime = fs.statSync(file).mtimeMs } catch {} const key = path.basename(file).toLowerCase() const cand = { file, rel, mtime, rank: exeRank(rel) } const cur = best.get(key) if (!cur || cand.rank > cur.rank || (cand.rank === cur.rank && cand.mtime > cur.mtime)) best.set(key, cand) } const pn = norm(name) return [...best.values()] .map((c) => { const en = norm(path.basename(c.file, path.extname(c.file))) return { ...c, match: nameMatches(pn, en) ? 1 : 0, secondary: EXE_SECONDARY.test(en) ? 1 : 0 } }) .sort((a, b) => (b.match - a.match) || (a.secondary - b.secondary) || (b.rank - a.rank) || (b.mtime - a.mtime)) .slice(0, 12) } // "TinyWold" (folder) should still match "TinyWorld.exe": contained, or a // common start of at least 5 letters. function nameMatches(projectNorm, exeNorm) { if (!projectNorm || !exeNorm) return false if (exeNorm.includes(projectNorm) || projectNorm.includes(exeNorm)) return true let i = 0 while (i < projectNorm.length && i < exeNorm.length && projectNorm[i] === exeNorm[i]) i++ return i >= 5 } // HTML pages that run by double-click: index.html up to 3 folders deep (game\index.html), // any .html directly in a release folder (single-file builds) and a root page named like // the project (Stammbaum\stammbaum.html). Pages that cannot run from file:// are left out: // ES modules (Vite dev page and built dist - browsers block module scripts on file://) and // absolute /paths. Those start via npm. const HTML_SKIP_PARTS = /(^|\\)(public|src|docs?|archive|_?archiv|mockups?|templates?|backups?|old|alt|kopie|copy|bak|node_modules|coverage|test-results|playwright-report|cache|\.cache)(\\|$)/i function findWebPages(name, projectPath) { const out = [] const pn = norm(name) for (const file of walkFiles(projectPath, 3, (n) => /\.html?$/i.test(n))) { const rel = path.relative(projectPath, file) const dir = path.dirname(rel) const base = path.basename(file, path.extname(file)) const isIndex = /^index$/i.test(base) const inRelease = /(^|\\)release$/i.test(dir) const namedRoot = dir === '.' && nameMatches(pn, norm(base)) if (!isIndex && !inRelease && !namedRoot) continue if (HTML_SKIP_PARTS.test(dir)) continue let head = '' try { head = fs.readFileSync(file, 'utf-8').slice(0, 200000) } catch { continue } if (/]+type=["']module["']/i.test(head) || /]+src=["']\/(?!\/)/i.test(head) || /\/@vite\//.test(head)) continue let mtime = 0 try { mtime = fs.statSync(file).mtimeMs } catch {} const rank = inRelease ? 3 : /^(game|spiel|web|www|app|html)(\\|$)/i.test(dir) ? 2 : dir === '.' ? 1 : 0 out.push({ file, rel, mtime, rank }) } // single-file release first, then game\index.html, then the root page return out.sort((a, b) => (b.rank - a.rank) || (b.mtime - a.mtime)).slice(0, 6) } // How a file is started: exe directly, html/lnk with the default app, bat/cmd in a console. function fileAction(id, label, file, projectPath, detail) { const rel = path.relative(projectPath, file) const ext = path.extname(file).toLowerCase() const base = { id, label, group: 'Programm', rel, file, detail } if (ext === '.exe') return { ...base, kind: 'exe', exe: file, args: [], cwd: path.dirname(file) } if (ext === '.bat' || ext === '.cmd') return { ...base, kind: 'console', cwd: path.dirname(file), command: `call "${path.basename(file)}"` } return { ...base, kind: 'open' } } // The program chosen in "Aktionen" (.dashboard.json "program", path inside the project) function resolveProgram(projectPath, program) { if (!program) return null const file = path.resolve(projectPath, program) if (!file.startsWith(projectPath + path.sep)) return null return { file, exists: fs.existsSync(file) && fs.statSync(file).isFile() } } // .bat/.cmd in the project root and one level below (e.g. Spiel\3D-Ansicht.bat) function findScripts(projectPath) { return walkFiles(projectPath, 1, (n) => /\.(bat|cmd)$/i.test(n) && !SCRIPT_SKIP.test(n)) .map((file) => { const base = path.basename(file) const group = /^(build|publish|deploy|release|make|compile|pack)/i.test(base) ? 'Bauen' : /(install|setup|einricht)/i.test(base) ? 'Einrichten' : 'Starten' return { file, rel: path.relative(projectPath, file), base, group } }) .slice(0, 12) } const where = (rel) => (rel ? ` (${rel})` : '') // Every action: { id, label, group, kind, cwd, command | exe+args | file } // kind: console = visible cmd window, exe = start program, open = default app, vscode function actions(name, projectPath, found, meta, tools) { const list = [ { id: 'vscode', label: 'In VS Code öffnen', group: 'Öffnen', kind: 'vscode', cwd: projectPath }, { id: 'explorer', label: 'Ordner im Explorer', group: 'Öffnen', kind: 'open', file: projectPath }, { id: 'console', label: 'Konsole im Projekt', group: 'Öffnen', kind: 'console', cwd: projectPath, command: 'git status 2>nul' }, ] if (meta.start) list.push({ id: 'custom', label: `Eigener Start: ${meta.start}`, group: 'Starten', kind: 'console', cwd: projectPath, command: meta.start }) // the program chosen for ▶ comes first; a missing file stays visible so it can be fixed const chosen = resolveProgram(projectPath, meta.program) if (chosen?.exists) { list.push(fileAction('program', path.basename(chosen.file, path.extname(chosen.file)), chosen.file, projectPath, `${path.relative(projectPath, chosen.file)} · für ▶ festgelegt`)) } const chosenFile = chosen?.exists ? chosen.file.toLowerCase() : null for (const f of found) { const r = f.rel switch (f.type) { case 'node': if (f.needsInstall) list.push({ id: `npm-install:${r}`, label: `npm install${where(r)}`, group: 'Einrichten', kind: 'console', cwd: f.dir, command: 'npm install' }) for (const s of NPM_SCRIPTS.filter((s) => f.scripts.includes(s))) { // a Vite dev server alone opens nothing visible - let it open the browser; // without node_modules the script would just fail, so install first const vite = /^\s*vite(\s|$)(?!.*\bbuild\b)/.test(f.scriptCmds?.[s] ?? '') && !/--open/.test(f.scriptCmds?.[s] ?? '') const cmd = `${f.needsInstall && s !== 'build' ? 'npm install && ' : ''}npm run ${s}${vite ? ' -- --open' : ''}` list.push({ id: `npm:${r}:${s}`, label: `npm run ${s}${where(r)}${vite ? ' (Browser)' : ''}`, group: s === 'build' ? 'Bauen' : 'Starten', kind: 'console', cwd: f.dir, command: cmd }) } break case 'dotnet': { const target = f.sln[0] ?? f.proj[0] list.push({ id: `dotnet-build:${r}:${target}`, label: `dotnet build ${target}`, group: 'Bauen', kind: 'console', cwd: f.dir, command: `dotnet build "${target}"` }) for (const p of f.proj.slice(0, 4)) list.push({ id: `dotnet-run:${r}:${p}`, label: `dotnet run ${p}`, group: 'Starten', kind: 'console', cwd: f.dir, command: `dotnet run --project "${p}"` }) break } case 'flutter': list.push({ id: `flutter:${r}`, label: `flutter run${where(r)}`, group: 'Starten', kind: 'console', cwd: f.dir, command: 'flutter pub get && flutter run' }) break case 'android': if (f.gradlew) list.push({ id: `gradle:${r}`, label: `Gradle assembleDebug${where(r)}`, group: 'Bauen', kind: 'console', cwd: f.dir, command: 'gradlew.bat assembleDebug' }) break case 'unity': { const ed = unityEditor(f.dir) if (ed.exe) list.push({ id: `unity:${r}`, label: `In Unity ${ed.ver} öffnen${where(r)}`, group: 'Starten', kind: 'exe', exe: ed.exe, args: ['-projectPath', f.dir] }) else if (fs.existsSync(UNITY_HUB)) list.push({ id: `unity-hub:${r}`, label: `Unity Hub öffnen${ed.ver ? ` (braucht ${ed.ver})` : ''}`, group: 'Starten', kind: 'exe', exe: UNITY_HUB, args: [] }) break } case 'python': { const main = PY_MAIN.find((m) => f.py.includes(m)) ?? f.py.find((p) => p.toLowerCase() === `${name.toLowerCase()}.py`) ?? (f.py.length === 1 ? f.py[0] : null) if (main) { const py = f.venv ? `"${f.venv}"` : 'py -3.12' list.push({ id: `py:${main}`, label: `Python: ${main}${f.venv ? ' (.venv)' : ''}`, group: 'Starten', kind: 'console', cwd: f.dir, command: `${py} "${main}"` }) } break } case 'arduino': list.push({ id: `ino:${r}:${f.ino[0]}`, label: `Arduino: ${f.ino[0]}`, group: 'Starten', kind: 'open', file: path.join(f.dir, f.ino[0]) }) break } } for (const p of findPrograms(name, projectPath)) { if (p.file.toLowerCase() === chosenFile) continue list.push({ id: `exe:${p.rel}`, label: path.basename(p.file, path.extname(p.file)), group: 'Programm', kind: 'exe', exe: p.file, args: [], cwd: path.dirname(p.file), rel: p.rel, detail: `${p.rel} · ${ago(p.mtime)}`, }) } for (const w of findWebPages(name, projectPath)) { if (w.file.toLowerCase() === chosenFile) continue const label = /^index\.html?$/i.test(path.basename(w.file)) ? `Web: ${path.dirname(w.rel) === '.' ? name : path.dirname(w.rel)}` : `Web: ${path.basename(w.file)}` list.push(fileAction(`html:${w.rel}`, label, w.file, projectPath, `${w.rel} · ${ago(w.mtime)} · im Browser`)) } for (const s of findScripts(projectPath)) { list.push({ id: `bat:${s.rel}`, label: s.base, group: s.group, kind: 'console', cwd: path.dirname(s.file), command: `call "${s.base}"`, detail: s.rel, }) } for (const g of tools.generators.filter((g) => g.kind === 'comfygen')) { list.push({ id: `gen:${g.rel}`, label: `Generator-Konsole (${g.rel})`, group: 'Werkzeuge', kind: 'console', cwd: path.join(projectPath, g.rel), command: 'gen' }) } return list } module.exports = { detect, summary, actions, resolveProgram, SKIP }