list.get.ts
1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { readdir, readFile, stat } from 'node:fs/promises'
import { join } from 'node:path'
export interface PromptItem {
filename: string
path: string
content: string
updatedAt: string
}
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const includeContent = query.content !== '0' && query.content !== 'false'
const promptDir = join(process.cwd(), 'prompt')
let files: string[] = []
try {
files = await readdir(promptDir)
} catch {
return { items: [] as PromptItem[] }
}
const items: PromptItem[] = []
for (const f of files) {
if (!f.endsWith('.md') || f === 'README.md') continue
const filePath = join(promptDir, f)
try {
const st = await stat(filePath)
const content = includeContent
? await readFile(filePath, 'utf-8')
: ''
items.push({
filename: f,
path: `prompt/${f}`,
content,
updatedAt: st.mtime.toISOString(),
})
} catch {
// 跳过无法读取的文件
}
}
// 按更新时间倒序
items.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime())
return { items }
})