|
| 1 | +// src/app/api/documents/list/route.ts |
| 2 | + |
| 3 | +'use server'; |
| 4 | +import { NextResponse } from 'next/server'; |
| 5 | +import * as git from 'isomorphic-git'; |
| 6 | +import fs from 'fs'; |
| 7 | +import path from 'path'; |
| 8 | +import { DOC_POOL_DIR, findTaxonomyDocRepoPath } from '@/app/api/utils'; |
| 9 | +import { KnowledgeFile } from '@/types'; |
| 10 | +import { devLog } from '@/utils/devlog'; |
| 11 | + |
| 12 | +const BASE_BRANCH = 'main'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Function to retrieve documents from taxonomy-knowledge-docs. |
| 16 | + * @returns An array of document name. |
| 17 | + */ |
| 18 | +const getKnowledgeFiles = async (): Promise<KnowledgeFile[]> => { |
| 19 | + const REPO_DIR = findTaxonomyDocRepoPath(); |
| 20 | + const knowledgeFiles: KnowledgeFile[] = []; |
| 21 | + |
| 22 | + // Ensure the repository path exists |
| 23 | + if (!fs.existsSync(REPO_DIR)) { |
| 24 | + devLog("Taxonomy knowledge doc directory doesn't exist at :", REPO_DIR); |
| 25 | + return knowledgeFiles; |
| 26 | + } |
| 27 | + |
| 28 | + // Check if the branch exists |
| 29 | + const branches = await git.listBranches({ fs, dir: REPO_DIR }); |
| 30 | + if (!branches.includes(BASE_BRANCH)) { |
| 31 | + throw new Error(`Branch "${BASE_BRANCH}" does not exist.`); |
| 32 | + } |
| 33 | + |
| 34 | + // Checkout the specified branch |
| 35 | + await git.checkout({ fs, dir: REPO_DIR, ref: BASE_BRANCH }); |
| 36 | + |
| 37 | + // Read all files in the repository root directory |
| 38 | + |
| 39 | + const docPoolDir = path.join(REPO_DIR, DOC_POOL_DIR); |
| 40 | + |
| 41 | + // Ensure the doc-pool directory exist |
| 42 | + if (!fs.existsSync(docPoolDir)) { |
| 43 | + devLog(`${DOC_POOL_DIR} directory doesn't exist.`); |
| 44 | + return knowledgeFiles; |
| 45 | + } |
| 46 | + |
| 47 | + const allFiles = await fs.promises.readdir(docPoolDir, { recursive: true }); |
| 48 | + |
| 49 | + // Filter for Markdown files only |
| 50 | + const markdownFiles = allFiles.filter((file) => path.extname(file).toLowerCase() === '.md'); |
| 51 | + |
| 52 | + for (const file of markdownFiles) { |
| 53 | + const filePath = path.join(REPO_DIR, file); |
| 54 | + |
| 55 | + // Check if the file is a regular file |
| 56 | + const stat = fs.statSync(filePath); |
| 57 | + if (!stat.isFile()) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + // Retrieve the latest commit SHA for the file on the specified branch |
| 63 | + const logs = await git.log({ |
| 64 | + fs, |
| 65 | + dir: REPO_DIR, |
| 66 | + ref: BASE_BRANCH, |
| 67 | + filepath: file, |
| 68 | + depth: 1 // Only the latest commit |
| 69 | + }); |
| 70 | + |
| 71 | + if (logs.length === 0) { |
| 72 | + // No commits found for this file; skip it |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + const latestCommit = logs[0]; |
| 77 | + |
| 78 | + const commitDate = new Date(latestCommit.commit.committer.timestamp * 1000).toISOString(); |
| 79 | + knowledgeFiles.push({ |
| 80 | + filename: path.basename(file), |
| 81 | + commitDate: commitDate |
| 82 | + }); |
| 83 | + } catch (error) { |
| 84 | + console.error(`Failed to retrieve commit for file ${file}:`, error); |
| 85 | + throw new Error(`Failed to retrieve commit for file: ${error}`); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return knowledgeFiles; |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * Handler to retrieve knowledge files from the taxonomy-knowledge-doc document pool. |
| 94 | + */ |
| 95 | +export async function GET() { |
| 96 | + try { |
| 97 | + const knowledgeFiles = await getKnowledgeFiles(); |
| 98 | + return NextResponse.json({ files: knowledgeFiles }, { status: 200 }); |
| 99 | + } catch (error) { |
| 100 | + console.error('Failed to fetch list of files from document pool:', error); |
| 101 | + return NextResponse.json({ error: (error as Error).message }, { status: 500 }); |
| 102 | + } |
| 103 | +} |
0 commit comments