refactor: extract builder as a package

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-06-11 00:37:01 +08:00
parent a7e4bce1b5
commit c62f8b1628
38 changed files with 130 additions and 59 deletions

View File

@@ -10,7 +10,6 @@
"scripts": {
"analyze": "analyzer=1 vite build",
"build": "tsx scripts/build.ts",
"build:manifest": "tsx src/core/cli.ts",
"dev": "tsx scripts/dev.ts",
"format": "prettier --write \"src/**/*.ts\" ",
"lint": "eslint --fix",
@@ -18,8 +17,6 @@
"type-check": "tsc --noEmit"
},
"dependencies": {
"@aws-sdk/client-s3": "3.826.0",
"@aws-sdk/s3-request-presigner": "3.826.0",
"@essentials/request-timeout": "1.3.0",
"@headlessui/react": "2.2.4",
"@photo-gallery/data": "workspace:*",
@@ -41,11 +38,8 @@
"consola": "3.4.2",
"dotenv": "16.5.0",
"es-toolkit": "1.39.3",
"exif-reader": "2.0.2",
"foxact": "0.2.46",
"fuji-recipes": "1.0.2",
"heic-convert": "2.1.0",
"heic-to": "1.1.13",
"immer": "10.1.1",
"jotai": "2.12.5",
"masonic": "4.1.0",
@@ -62,7 +56,6 @@
"react-scan": "0.3.4",
"react-use-measure": "2.1.7",
"react-zoom-pan-pinch": "3.7.0",
"sharp": "0.34.2",
"sonner": "2.0.5",
"swiper": "11.2.8",
"tailwind-merge": "3.3.0",
@@ -97,4 +90,4 @@
"tailwindcss-uikit-colors": "1.0.0-alpha.1",
"vite-plugin-html": "3.2.2"
}
}
}

View File

@@ -29,7 +29,10 @@ export const precheck = async () => {
if (builderConfig.repo.enable) {
await pullAndLinkRemoteRepo()
} else {
await $({ cwd: workdir, stdio: 'inherit' })`pnpm build:manifest`
await $({
cwd: workdir,
stdio: 'inherit',
})`pnpm --filter @photo-gallery/builder cli`
}
}
}

View File

@@ -3,10 +3,10 @@ import { existsSync, readFileSync } from 'node:fs'
import os from 'node:os'
import { inspect } from 'node:util'
import type { StorageConfig } from '@photo-gallery/builder'
import consola from 'consola'
import { merge } from 'es-toolkit'
import type { StorageConfig } from './apps/web/src/core/storage/interfaces.js'
import { env } from './env.js'
export interface BuilderConfig {

View File

@@ -4,10 +4,6 @@
"description": "Capturing beautiful moments in life, documenting daily warmth and emotions through my lens.",
"url": "https://gallery.innei.in",
"accentColor": "#007bff",
"ogImage": {
"width": 1200,
"height": 630
},
"author": {
"name": "Photo Gallery",
"url": "https://innei.in/"

View File

@@ -10,7 +10,7 @@
},
"scripts": {
"build": "pnpm --filter @photo-gallery/web build",
"build:manifest": "pnpm --filter @photo-gallery/web build:manifest",
"build:manifest": "pnpm --filter @photo-gallery/builder cli",
"build:update-remote-repo": "sh scripts/build-update-remote-repo.sh",
"dev": "pnpm --filter @photo-gallery/web dev",
"extract:font": "tsx scripts/extract-font-glyphs.ts",
@@ -20,6 +20,7 @@
"prepare": "simple-git-hooks"
},
"dependencies": {
"@photo-gallery/builder": "workspace:*",
"@t3-oss/env-core": "0.13.8",
"dotenv": "16.5.0",
"es-toolkit": "1.39.3",

1
packages/builder/.env Symbolic link
View File

@@ -0,0 +1 @@
../../.env

View File

@@ -1,4 +1,4 @@
# Photo Gallery Core 架构
# Photo Gallery Builder
这是照片库构建系统的核心模块,采用模块化设计,将不同功能分离到各自的模块中。

View File

@@ -0,0 +1,23 @@
{
"name": "@photo-gallery/builder",
"type": "module",
"version": "0.0.1",
"private": true,
"exports": {
".": "./src/index.ts"
},
"scripts": {
"cli": "tsx src/cli.ts"
},
"dependencies": {
"@aws-sdk/client-s3": "3.826.0",
"@aws-sdk/s3-request-presigner": "3.826.0",
"blurhash": "2.0.5",
"execa": "9.6.0",
"exif-reader": "2.0.2",
"fuji-recipes": "1.0.2",
"heic-convert": "2.1.0",
"heic-to": "1.1.13",
"sharp": "0.34.2"
}
}

View File

@@ -2,16 +2,15 @@ import cluster from 'node:cluster'
import { existsSync } from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { builderConfig } from '@builder'
import { $ } from 'execa'
import { defaultBuilder } from './builder/index.js'
import { logger } from './logger/index.js'
import { workdir } from './path.js'
import { runAsWorker } from './runAsWorker.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
async function main() {
// 检查是否作为 cluster worker 运行
if (
@@ -26,7 +25,7 @@ async function main() {
// 如果配置了远程仓库,则使用远程仓库
if (builderConfig.repo.enable) {
// 拉取远程仓库
const workdir = path.resolve(__dirname, '..', '..')
const hasExist = existsSync(path.resolve(workdir, 'assets-git'))
if (!hasExist) {
await $({
@@ -128,7 +127,6 @@ async function main() {
}
}
logger.main.info(` 默认并发数:${config.options.defaultConcurrency}`)
logger.main.info(` 最大照片数:${config.options.maxPhotos}`)
logger.main.info(
` Live Photo 检测:${config.options.enableLivePhotoDetection ? '启用' : '禁用'}`,
)

View File

@@ -4,6 +4,8 @@ import { fileURLToPath } from 'node:url'
import sharp from 'sharp'
import { workdir } from '~/path.js'
import type { Logger } from '../logger/index.js'
import type { ThumbnailResult } from '../types/photo.js'
import { generateBlurhash } from './blurhash.js'
@@ -15,8 +17,8 @@ const __dirname = path.dirname(__filename)
export async function thumbnailExists(photoId: string): Promise<boolean> {
try {
const thumbnailPath = path.join(
__dirname,
'../../../public/thumbnails',
workdir,
'public/thumbnails',
`${photoId}.webp`,
)
await fs.access(thumbnailPath)
@@ -42,7 +44,7 @@ export async function generateThumbnailAndBlurhash(
const blurhashLog = workerLogger?.blurhash
try {
const thumbnailDir = path.join(__dirname, '../../../public/thumbnails')
const thumbnailDir = path.join(workdir, 'public/thumbnails')
await fs.mkdir(thumbnailDir, { recursive: true })
const thumbnailPath = path.join(thumbnailDir, `${photoId}.webp`)

View File

@@ -4,7 +4,7 @@ export {
defaultBuilder,
PhotoGalleryBuilder,
} from './builder/index.js'
export type { StorageConfig } from './storage/interfaces.js'
// 日志系统
export { type Logger, logger, type WorkerLogger } from './logger/index.js'

View File

@@ -1,22 +1,17 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import type { _Object } from '@aws-sdk/client-s3'
import { workdir } from '~/path.js'
import type { Logger } from '../logger/index.js'
import type { PhotoManifestItem } from '../types/photo.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const manifestPath = path.join(workdir, 'src/data/photos-manifest.json')
// 读取现有的 manifest
export async function loadExistingManifest(): Promise<PhotoManifestItem[]> {
try {
const manifestPath = path.join(
__dirname,
'../../../src/data/photos-manifest.json',
)
const manifestContent = await fs.readFile(manifestPath, 'utf-8')
return JSON.parse(manifestContent) as PhotoManifestItem[]
} catch {
@@ -43,11 +38,6 @@ export async function saveManifest(
manifest: PhotoManifestItem[],
fsLogger?: Logger['fs'],
): Promise<void> {
const manifestPath = path.join(
__dirname,
'../../../src/data/photos-manifest.json',
)
// 按日期排序(最新的在前)
const sortedManifest = [...manifest].sort(
(a, b) => new Date(b.dateTaken).getTime() - new Date(a.dateTaken).getTime(),
@@ -82,8 +72,8 @@ export async function handleDeletedPhotos(
// 删除对应的缩略图文件
try {
const thumbnailPath = path.join(
__dirname,
'../../../public/thumbnails',
workdir,
'public/thumbnails',
`${existingItem.id}.webp`,
)
await fs.unlink(thumbnailPath)

View File

@@ -0,0 +1,6 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
export const workdir = path.resolve(__dirname, '../../../apps/web')

View File

@@ -4,6 +4,8 @@ import type { _Object } from '@aws-sdk/client-s3'
import type { Exif } from 'exif-reader'
import sharp from 'sharp'
import { workdir } from '~/path.js'
import { HEIC_FORMATS } from '../constants/index.js'
import { extractExifData } from '../image/exif.js'
import {
@@ -142,7 +144,7 @@ export async function processPhoto(
try {
const fs = await import('node:fs/promises')
const thumbnailPath = path.join(
process.cwd(),
workdir,
'public/thumbnails',
`${photoId}.webp`,
)

View File

@@ -1,4 +1,3 @@
import type { Logger } from '../logger/index.js'
// 存储对象的通用接口

View File

@@ -0,0 +1,42 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": [
"DOM",
"DOM.Iterable",
"ESNext"
],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"skipDefaultLibCheck": true,
"noImplicitAny": false,
"noEmit": true,
"jsx": "preserve",
"paths": {
"~/*": [
"./src/*"
],
"@pkg": [
"./package.json"
],
"@builder": [
"../../builder.config.ts"
],
"@env": [
"../../env.ts"
]
},
},
"include": [
"./src/**/*",
"./scripts/**/*"
]
}

51
pnpm-lock.yaml generated
View File

@@ -8,6 +8,9 @@ importers:
.:
dependencies:
'@photo-gallery/builder':
specifier: workspace:*
version: link:packages/builder
'@t3-oss/env-core':
specifier: 0.13.8
version: 0.13.8(typescript@5.8.3)(zod@3.25.57)
@@ -106,12 +109,6 @@ importers:
apps/web:
dependencies:
'@aws-sdk/client-s3':
specifier: 3.826.0
version: 3.826.0
'@aws-sdk/s3-request-presigner':
specifier: 3.826.0
version: 3.826.0
'@essentials/request-timeout':
specifier: 1.3.0
version: 1.3.0
@@ -175,21 +172,12 @@ importers:
es-toolkit:
specifier: 1.39.3
version: 1.39.3
exif-reader:
specifier: 2.0.2
version: 2.0.2
foxact:
specifier: 0.2.46
version: 0.2.46(react@19.1.0)
fuji-recipes:
specifier: 1.0.2
version: 1.0.2
heic-convert:
specifier: 2.1.0
version: 2.1.0
heic-to:
specifier: 1.1.13
version: 1.1.13
immer:
specifier: 10.1.1
version: 10.1.1
@@ -238,9 +226,6 @@ importers:
react-zoom-pan-pinch:
specifier: 3.7.0
version: 3.7.0(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
sharp:
specifier: 0.34.2
version: 0.34.2
sonner:
specifier: 2.0.5
version: 2.0.5(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
@@ -336,6 +321,36 @@ importers:
specifier: 3.2.2
version: 3.2.2(vite@6.3.5(@types/node@22.15.31)(jiti@2.4.2)(lightningcss@1.30.1)(terser@5.41.0)(tsx@4.19.4)(yaml@2.8.0))
packages/builder:
dependencies:
'@aws-sdk/client-s3':
specifier: 3.826.0
version: 3.826.0
'@aws-sdk/s3-request-presigner':
specifier: 3.826.0
version: 3.826.0
blurhash:
specifier: 2.0.5
version: 2.0.5
execa:
specifier: 9.6.0
version: 9.6.0
exif-reader:
specifier: 2.0.2
version: 2.0.2
fuji-recipes:
specifier: 1.0.2
version: 1.0.2
heic-convert:
specifier: 2.1.0
version: 2.1.0
heic-to:
specifier: 1.1.13
version: 1.1.13
sharp:
specifier: 0.34.2
version: 0.34.2
packages/data:
dependencies:
exif-reader: