lukeonline

source for my website
git clone https://git.lukeonline.net/repos/lukeonline
Log | Files | Refs | README | LICENSE

repos.ts (2224B)


      1 import fs from 'node:fs'
      2 import path from 'node:path'
      3 
      4 const reposRoot = path.join(process.cwd(), 'src/content/repos')
      5 
      6 export interface Repo {
      7   name: string
      8   head: string
      9   defaultBranch: string
     10   branches: string[]
     11   tags: string[]
     12   commitCount: number
     13 }
     14 
     15 export interface Commit {
     16   sha: string
     17   shortSha: string
     18   parents: string[]
     19   author: {
     20     name: string
     21     email: string
     22   }
     23   date: string
     24   message: string
     25 }
     26 
     27 export interface RepoFile {
     28   path: string
     29   mode: string
     30   type: string
     31   sha: string
     32 }
     33 
     34 export function getCommits(name: string): Commit[] {
     35   const file = path.join(reposRoot, name, 'commits.json')
     36 
     37   return JSON.parse(fs.readFileSync(file, 'utf-8'))
     38 }
     39 
     40 export function getTree(name: string): RepoFile[] {
     41   const file = path.join(reposRoot, name, 'tree.json')
     42 
     43   return JSON.parse(fs.readFileSync(file, 'utf-8'))
     44 }
     45 
     46 export function getCommit(name: string, sha: string) {
     47   const file = path.join(reposRoot, name, 'commits', `${sha}.json`)
     48 
     49   return JSON.parse(fs.readFileSync(file, 'utf-8'))
     50 }
     51 
     52 export function getFile(name: string, encodedPath: string) {
     53   const file = path.join(reposRoot, name, 'files', `${encodedPath}.json`)
     54 
     55   return JSON.parse(fs.readFileSync(file, 'utf-8'))
     56 }
     57 
     58 export function getFileByPath(name: string, filePath: string) {
     59   const encodedPath = Buffer.from(filePath, 'utf-8').toString('base64url')
     60 
     61   return getFile(name, encodedPath)
     62 }
     63 
     64 export function getDirectoryChildren(tree: RepoFile[], directory: string) {
     65   const prefix = directory ? `${directory}/` : ''
     66 
     67   const directories = new Set<string>()
     68   const files = new Set<string>()
     69 
     70   for (const file of tree) {
     71     if (!file.path.startsWith(prefix)) {
     72       continue
     73     }
     74 
     75     const remaining = file.path.slice(prefix.length)
     76     const child = remaining.split('/')[0]
     77 
     78     if (remaining.includes('/')) {
     79       directories.add(child)
     80     } else {
     81       files.add(child)
     82     }
     83   }
     84 
     85   return {
     86     directories: [...directories].sort(),
     87     files: [...files].sort(),
     88   }
     89 }
     90 
     91 export function encodeFilePath(filePath: string) {
     92   return Buffer.from(filePath, 'utf-8').toString('base64url')
     93 }
     94 
     95 export function decodeFilePath(encodedPath: string) {
     96   return Buffer.from(encodedPath, 'base64url').toString('utf-8')
     97 }