77 lines
2.9 KiB
JavaScript
77 lines
2.9 KiB
JavaScript
// "Last change" of a project = newest modified file inside it, ignoring
|
|
// folders that change by themselves (node_modules, build output, caches, .git).
|
|
// The folder's own mtime is useless for this (it only changes when direct
|
|
// children are added/removed). Bounded walk, cached for a few minutes.
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
const SKIP = /^(node_modules|\.git|\.gradle|\.gradle-local|Library|Temp|Logs|obj|bin|build|builds|dist|out|release|publish|\.venv.*|venv|env|__pycache__|\.cache|\.expo|\.next|\.idea|\.vs|\.vscode|output|CMakeFiles|_deps|\.dart_tool|\.cxx|\.externalNativeBuild|cmake-build-.*)$/i
|
|
const IGNORE_FILES = /^(\.dashboard\.json|thumbs\.db|desktop\.ini|\.ds_store)$/i
|
|
const MAX_ENTRIES = 30000
|
|
const MAX_DEPTH = 10
|
|
const TTL_MS = 5 * 60 * 1000
|
|
const cache = new Map() // projectPath -> { value, at }
|
|
|
|
function scan(projectPath) {
|
|
let best = 0
|
|
let bestFile = ''
|
|
let seen = 0
|
|
const stack = [{ dir: projectPath, depth: 0 }]
|
|
while (stack.length) {
|
|
const { dir, depth } = stack.pop()
|
|
let entries
|
|
try { entries = fs.readdirSync(dir, { withFileTypes: true }) } catch { continue }
|
|
for (const e of entries) {
|
|
if (++seen > MAX_ENTRIES) return finish()
|
|
const full = path.join(dir, e.name)
|
|
if (e.isDirectory()) {
|
|
if (depth < MAX_DEPTH && !SKIP.test(e.name)) stack.push({ dir: full, depth: depth + 1 })
|
|
} else if (e.isFile() && !IGNORE_FILES.test(e.name)) {
|
|
let m = 0
|
|
try { m = fs.statSync(full).mtimeMs } catch { continue }
|
|
if (m > best) { best = m; bestFile = full }
|
|
}
|
|
}
|
|
}
|
|
return finish()
|
|
function finish() {
|
|
return best ? { at: new Date(best).toISOString(), file: path.relative(projectPath, bestFile), partial: seen > MAX_ENTRIES } : null
|
|
}
|
|
}
|
|
|
|
// Scans run in the background, one project per tick, so the server stays
|
|
// responsive; the list always gets the latest known value right away.
|
|
const queue = []
|
|
const queued = new Set()
|
|
let running = false
|
|
|
|
function enqueue(projectPath) {
|
|
if (queued.has(projectPath)) return
|
|
queued.add(projectPath)
|
|
queue.push(projectPath)
|
|
if (!running) { running = true; setImmediate(work) }
|
|
}
|
|
|
|
function work() {
|
|
const p = queue.shift()
|
|
if (!p) { running = false; return }
|
|
queued.delete(p)
|
|
try { cache.set(p, { value: scan(p), at: Date.now() }) } catch {}
|
|
setImmediate(work)
|
|
}
|
|
|
|
// -> { at, file, partial } | null (empty project) | undefined (not known yet);
|
|
// a stale or missing value is refreshed in the background.
|
|
function lastChange(projectPath) {
|
|
const c = cache.get(projectPath)
|
|
if (!c || Date.now() - c.at > TTL_MS) enqueue(projectPath)
|
|
return c ? c.value : undefined
|
|
}
|
|
|
|
function warm(projectPaths) { for (const p of projectPaths) enqueue(p) }
|
|
function pending() { return queue.length + (running ? 1 : 0) }
|
|
function forget(projectPath) { cache.delete(projectPath) }
|
|
function clear() { cache.clear(); queue.length = 0; queued.clear() }
|
|
|
|
module.exports = { lastChange, warm, pending, forget, clear }
|