162 lines
4.8 KiB
JavaScript
162 lines
4.8 KiB
JavaScript
#!/usr/bin/env node
|
|
// 扫描工作区,生成 manifest.json 给控制台用
|
|
// 用法: node scan.mjs [目录] 默认 /root/workspace
|
|
import { readdir, stat, readFile, writeFile, readlink } from 'node:fs/promises'
|
|
import { join, basename, extname } from 'node:path'
|
|
import { execFile } from 'node:child_process'
|
|
import { promisify } from 'node:util'
|
|
|
|
const run = promisify(execFile)
|
|
const ROOT = process.argv[2] || '/root/workspace'
|
|
const OUT = new URL('./manifest.json', import.meta.url).pathname
|
|
|
|
// 已经部署到服务器的站点(端口 → 名字)
|
|
const DEPLOYED = {
|
|
devbox: 8090,
|
|
imgbed: 8088,
|
|
'dine-order': 8099,
|
|
raycast3d: 8100,
|
|
console: 8089,
|
|
}
|
|
|
|
const SKIP = new Set(['node_modules', '.git', 'data', 'dist', 'build', '.cache'])
|
|
|
|
async function du(dir, depth = 0) {
|
|
let bytes = 0
|
|
let files = 0
|
|
let entries = []
|
|
try {
|
|
entries = await readdir(dir, { withFileTypes: true })
|
|
} catch {
|
|
return { bytes, files }
|
|
}
|
|
for (const e of entries) {
|
|
if (SKIP.has(e.name)) continue
|
|
const p = join(dir, e.name)
|
|
try {
|
|
if (e.isDirectory()) {
|
|
const r = await du(p, depth + 1)
|
|
bytes += r.bytes
|
|
files += r.files
|
|
} else if (e.isFile()) {
|
|
const s = await stat(p)
|
|
bytes += s.size
|
|
files += 1
|
|
}
|
|
} catch {}
|
|
}
|
|
return { bytes, files }
|
|
}
|
|
|
|
async function gitInfo(dir) {
|
|
try {
|
|
const opt = { cwd: dir, timeout: 5000 }
|
|
const [branch, head, status, remote] = await Promise.all([
|
|
run('git', ['rev-parse', '--abbrev-ref', 'HEAD'], opt).then((r) => r.stdout.trim()).catch(() => ''),
|
|
run('git', ['log', '-1', '--pretty=%h|%s|%cI'], opt).then((r) => r.stdout.trim()).catch(() => ''),
|
|
run('git', ['status', '--porcelain'], opt).then((r) => r.stdout.trim()).catch(() => ''),
|
|
run('git', ['remote', 'get-url', 'origin'], opt).then((r) => r.stdout.trim()).catch(() => ''),
|
|
])
|
|
if (!head) return null
|
|
const [sha, subject, date] = head.split('|')
|
|
let repo = ''
|
|
let webRepo = ''
|
|
const m = remote.match(/(?:github\.com|:3000)[/:]([\w.-]+)\/([\w.-]+?)(?:\.git)?$/)
|
|
if (m) {
|
|
repo = `${m[1]}/${m[2]}`
|
|
webRepo = remote.includes('github.com') ? `https://github.com/${repo}` : `http://110.42.217.133:3000/${repo}`
|
|
}
|
|
return { branch, sha, subject, date, dirty: status !== '', remote, repo, webRepo }
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
async function readmeTitle(dir) {
|
|
for (const name of ['README.md', 'readme.md', 'README.MD']) {
|
|
try {
|
|
const text = await readFile(join(dir, name), 'utf8')
|
|
const first = text.split('\n').find((l) => /^#\s+/.test(l.trim()))
|
|
if (first) return first.replace(/^#\s+/, '').trim().slice(0, 80)
|
|
const para = text.split(/\n{2,}/).find((p) => p.trim() && !p.startsWith('#') && !p.startsWith('!') && !p.startsWith('<'))
|
|
if (para) return para.replace(/[*_`>#-]/g, '').trim().slice(0, 80)
|
|
} catch {}
|
|
}
|
|
return ''
|
|
}
|
|
|
|
function kindOf(name, hasGit, hasReadme) {
|
|
if (DEPLOYED[name]) return 'site'
|
|
if (name.endsWith('-backup') || name.includes('mirror')) return 'mirror'
|
|
if (/^(local-|dsh-)/.test(name)) return 'plugin'
|
|
if (hasGit) return 'repo'
|
|
return 'dir'
|
|
}
|
|
|
|
function human(n) {
|
|
const u = ['B', 'KB', 'MB', 'GB']
|
|
let i = 0
|
|
let v = n
|
|
while (v >= 1024 && i < u.length - 1) {
|
|
v /= 1024
|
|
i++
|
|
}
|
|
return `${v < 10 ? v.toFixed(1) : Math.round(v)} ${u[i]}`
|
|
}
|
|
|
|
const entries = []
|
|
let totalBytes = 0
|
|
let totalFiles = 0
|
|
|
|
for (const e of await readdir(ROOT, { withFileTypes: true })) {
|
|
if (e.name.startsWith('.')) continue
|
|
const p = join(ROOT, e.name)
|
|
const s = await stat(p)
|
|
if (e.isDirectory()) {
|
|
const size = await du(p)
|
|
const git = await gitInfo(p)
|
|
const title = await readmeTitle(p)
|
|
totalBytes += size.bytes
|
|
totalFiles += size.files
|
|
entries.push({
|
|
name: e.name,
|
|
kind: kindOf(e.name, !!git, !!title),
|
|
bytes: size.bytes,
|
|
size: human(size.bytes),
|
|
files: size.files,
|
|
mtime: s.mtime.toISOString(),
|
|
title,
|
|
git,
|
|
port: DEPLOYED[e.name] || 0,
|
|
})
|
|
} else {
|
|
totalBytes += s.size
|
|
totalFiles += 1
|
|
entries.push({
|
|
name: e.name,
|
|
kind: 'file',
|
|
ext: extname(e.name).replace('.', ''),
|
|
bytes: s.size,
|
|
size: human(s.size),
|
|
files: 1,
|
|
mtime: s.mtime.toISOString(),
|
|
title: '',
|
|
git: null,
|
|
port: 0,
|
|
})
|
|
}
|
|
}
|
|
|
|
entries.sort((a, b) => b.bytes - a.bytes)
|
|
|
|
const manifest = {
|
|
root: ROOT,
|
|
scannedAt: new Date().toISOString(),
|
|
totals: { bytes: totalBytes, size: human(totalBytes), files: totalFiles, projects: entries.length },
|
|
entries,
|
|
}
|
|
|
|
await writeFile(OUT, JSON.stringify(manifest, null, 2))
|
|
console.log(`扫描完成:${entries.length} 项 · ${human(totalBytes)} · ${totalFiles} 个文件`)
|
|
for (const e of entries) console.log(` ${e.size.padStart(9)} ${e.name}${e.git ? ` (${e.git.branch})` : ''}`)
|