59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import 'dotenv/config'
|
|
import path from 'node:path'
|
|
import { discoverDecks, validateDeck, type ValidationIssue } from './lib/content.ts'
|
|
import { loadProjectConfig, type EasySlidesConfig } from './lib/config.ts'
|
|
import { formatStaticOverflowResult } from './lib/overflow-report.ts'
|
|
import { auditStaticDecks } from './lib/static-overflow.ts'
|
|
import { hasPresenterToken } from './lib/token.ts'
|
|
|
|
export async function checkProject(cwd = process.cwd(), providedConfig?: EasySlidesConfig) {
|
|
const config = providedConfig ?? await loadProjectConfig(cwd)
|
|
const decks = await discoverDecks(cwd, config.slidesDir)
|
|
const issues: ValidationIssue[] = []
|
|
|
|
if (decks.length === 0)
|
|
issues.push({ level: 'error', file: path.join(cwd, config.slidesDir), message: `没有发现 ${config.slidesDir}/<slug>/slides.md` })
|
|
|
|
for (const deck of decks)
|
|
issues.push(...await validateDeck(deck))
|
|
|
|
const staticOverflow = await auditStaticDecks(decks)
|
|
|
|
if (!hasPresenterToken()) {
|
|
issues.push({
|
|
level: 'warning',
|
|
file: path.join(cwd, '.env'),
|
|
message: '未设置 PRESENTER_TOKEN;生产构建将隐藏演示者入口',
|
|
})
|
|
}
|
|
|
|
return { config, decks, issues, staticOverflow }
|
|
}
|
|
|
|
export function printIssues(issues: ValidationIssue[], cwd = process.cwd()) {
|
|
for (const issue of issues) {
|
|
const marker = issue.level === 'error' ? 'ERROR' : 'WARN '
|
|
console.log(`${marker} ${path.relative(cwd, issue.file)}: ${issue.message}`)
|
|
}
|
|
}
|
|
|
|
export async function runCheck() {
|
|
const result = await checkProject()
|
|
printIssues(result.issues)
|
|
formatStaticOverflowResult(result.staticOverflow).forEach(line => console.log(line))
|
|
const errors = result.issues.filter(issue => issue.level === 'error')
|
|
if (errors.length > 0) {
|
|
console.error(`\n检查失败:${errors.length} 个错误`)
|
|
process.exitCode = 1
|
|
return
|
|
}
|
|
|
|
const warnings = result.issues.filter(issue => issue.level === 'warning').length
|
|
+ result.staticOverflow.issues.length
|
|
+ result.staticOverflow.errors.length
|
|
console.log(`\n检查通过:${result.decks.length} 套演示,${warnings} 个警告`)
|
|
}
|
|
|
|
if (import.meta.url === new URL(process.argv[1], 'file:').href)
|
|
await runCheck()
|