feat: moves getGitDate to a shared utils file

This commit is contained in:
Lewis Wynne 2026-01-23 21:10:08 +00:00
parent 9137556a4a
commit 223bdf525a
4 changed files with 16 additions and 23 deletions

View file

@ -5,6 +5,7 @@ import bookmarksRaw from '../data/bookmarks.yaml?raw';
import fs from 'node:fs'; import fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import type { APIContext } from 'astro'; import type { APIContext } from 'astro';
import { getGitDate } from '../utils';
interface Bookmark { interface Bookmark {
date: string; date: string;
@ -14,7 +15,7 @@ interface Bookmark {
interface TxtFile { interface TxtFile {
name: string; name: string;
mtime: Date; date: Date;
} }
export async function GET(context: APIContext) { export async function GET(context: APIContext) {
@ -27,7 +28,7 @@ export async function GET(context: APIContext) {
.filter(file => file.endsWith('.txt')) .filter(file => file.endsWith('.txt'))
.map(name => ({ .map(name => ({
name, name,
mtime: fs.statSync(path.join(txtDir, name)).mtime, date: getGitDate(path.join(txtDir, name)),
})) }))
: []; : [];
@ -40,7 +41,7 @@ export async function GET(context: APIContext) {
})), })),
...txtFiles.map(txt => ({ ...txtFiles.map(txt => ({
title: txt.name, title: txt.name,
pubDate: txt.mtime, pubDate: txt.date,
link: `/txt/${txt.name}`, link: `/txt/${txt.name}`,
description: txt.name, description: txt.name,
})), })),

View file

@ -5,8 +5,8 @@ import yaml from 'js-yaml';
import bookmarksRaw from '../data/bookmarks.yaml?raw'; import bookmarksRaw from '../data/bookmarks.yaml?raw';
import fs from 'node:fs'; import fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import { execSync } from 'node:child_process';
import { getApprovedEntries, type GuestbookEntry } from '../lib/db'; import { getApprovedEntries, type GuestbookEntry } from '../lib/db';
import { getGitDate } from '../utils';
interface Bookmark { interface Bookmark {
date: string; date: string;
@ -24,15 +24,6 @@ interface TxtConfig {
pinned?: string[]; pinned?: string[];
} }
function getGitDate(filePath: string): Date {
try {
const timestamp = execSync(`git log -1 --format=%cI -- "${filePath}"`, { encoding: 'utf8' }).trim();
return timestamp ? new Date(timestamp) : new Date(0);
} catch {
return new Date(0);
}
}
const posts = await getCollection('posts'); const posts = await getCollection('posts');
// Group by category (default: "posts") // Group by category (default: "posts")

View file

@ -2,8 +2,8 @@
import Layout from '../../layouts/Layout.astro'; import Layout from '../../layouts/Layout.astro';
import fs from 'node:fs'; import fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import { execSync } from 'node:child_process';
import yaml from 'js-yaml'; import yaml from 'js-yaml';
import { getGitDate } from '../../utils';
interface TxtFile { interface TxtFile {
name: string; name: string;
@ -15,15 +15,6 @@ interface TxtConfig {
pinned?: string[]; pinned?: string[];
} }
function getGitDate(filePath: string): Date {
try {
const timestamp = execSync(`git log -1 --format=%cI -- "${filePath}"`, { encoding: 'utf8' }).trim();
return timestamp ? new Date(timestamp) : new Date(0);
} catch {
return new Date(0);
}
}
const txtDir = path.join(process.cwd(), 'public/txt'); const txtDir = path.join(process.cwd(), 'public/txt');
const configPath = path.join(txtDir, 'config.yaml'); const configPath = path.join(txtDir, 'config.yaml');
const config: TxtConfig = fs.existsSync(configPath) const config: TxtConfig = fs.existsSync(configPath)

10
apps/blog/src/utils.ts Normal file
View file

@ -0,0 +1,10 @@
import { execSync } from 'node:child_process';
export function getGitDate(filePath: string): Date {
try {
const timestamp = execSync(`git log -1 --format=%cI -- "${filePath}"`, { encoding: 'utf8' }).trim();
return timestamp ? new Date(timestamp) : new Date(0);
} catch {
return new Date(0);
}
}