--- description: "SnapOtter 的本機開發環境設定、指令、程式碼慣例,以及如何新增工具。" i18n_source_hash: cb03724d2829 i18n_provenance: human i18n_output_hash: 64a00be99b89 --- # 開發者指南 {#developer-guide} 如何設定本機開發環境,並為 SnapOtter 貢獻程式碼。 ## 先決條件 {#prerequisites} - [Node.js](https://nodejs.org/) 22+ - [pnpm](https://pnpm.io/) 9+(`corepack enable && corepack prepare pnpm@latest --activate`) - [Docker](https://www.docker.com/)(本機 Postgres + Redis、容器建置與 AI 功能所必需) - Git 只有在你要處理 AI/ML 附屬程序(去背、放大、OCR)時,才需要 Python 3.10+。 ## 設定 {#setup} ```bash git clone https://github.com/snapotter-hq/snapotter.git cd snapotter docker compose -f docker-compose.dev.yml up -d # start Postgres + Redis pnpm install pnpm dev ``` 這會啟動兩個開發伺服器: | 服務 | URL | 說明 | |----------|--------------------------|------------------------------------| | 前端 | http://localhost:1349 | Vite 開發伺服器,代理 /api | | 後端 | http://localhost:13490 | Fastify API(透過代理存取) | 在瀏覽器中開啟 http://localhost:1349。以 `admin` / `admin` 登入。你會在首次登入時被提示變更密碼。 ## 專案結構 {#project-structure} ``` apps/ api/ Fastify backend web/ Vite + React frontend docs/ VitePress documentation (this site) packages/ shared/ Constants, types, i18n strings image-engine/ Sharp-based image operations media-engine/ FFmpeg spawn + progress parsing doc-engine/ qpdf, LibreOffice, ghostscript wrappers ai/ Python sidecar bridge for ML models tests/ unit/ Vitest unit tests integration/ Vitest integration tests (full API) e2e/ Playwright end-to-end specs fixtures/ Small test images ``` ## 指令 {#commands} ```bash pnpm dev # start frontend + backend pnpm build # build all workspaces pnpm typecheck # TypeScript check across monorepo pnpm lint # Biome lint + format check pnpm lint:fix # auto-fix lint + format pnpm test # unit + integration tests pnpm test:unit # unit tests only pnpm test:integration # integration tests only pnpm test:e2e # Playwright e2e tests pnpm test:coverage # tests with coverage report ``` ## 程式碼慣例 {#code-conventions} - 雙引號、分號、2 格縮排(由 Biome 強制) - 所有工作區皆使用 ES 模組 - 供 semantic-release 使用的[慣例式提交](https://www.conventionalcommits.org/) - 所有 API 輸入驗證皆使用 Zod - 不修改 Biome、TypeScript 或編輯器設定檔。修正程式碼,而非 linter。 ## 資料庫 {#database} 透過 Drizzle ORM(pg-core)使用 PostgreSQL 17。本機開發需要 Postgres 與 Redis 執行中 - 以下列指令啟動它們: ```bash docker compose -f docker-compose.dev.yml up -d ``` 這會在 5432 埠提供 Postgres,並在 6379 埠提供 Redis。接著產生並套用遷移: ```bash cd apps/api npx drizzle-kit generate # generate a migration from schema changes npx drizzle-kit migrate # apply pending migrations ``` 結構定義於 `apps/api/src/db/schema.ts`。資料表:users、sessions、settings、jobs、apiKeys、pipelines、teams、userFiles、roles、auditLog。 ## 新增工具 {#adding-a-new-tool} 每個工具都遵循相同的模式。以下是一個最小範例。 ### 1. 後端路由 {#_1-backend-route} 建立 `apps/api/src/routes/tools/my-tool.ts`: ```ts import { z } from "zod"; import type { FastifyInstance } from "fastify"; import { createToolRoute } from "../tool-factory.js"; const settingsSchema = z.object({ intensity: z.number().min(0).max(100).default(50), }); export function registerMyTool(app: FastifyInstance) { createToolRoute(app, { toolId: "my-tool", settingsSchema, async process(inputBuffer, settings, filename) { // Use sharp or other libraries to process the image const sharp = (await import("sharp")).default; const result = await sharp(inputBuffer) // ... your processing logic .toBuffer(); return { buffer: result, filename: filename.replace(/\.[^.]+$/, ".png"), contentType: "image/png", }; }, }); } ``` 然後在 `apps/api/src/routes/tools/index.ts` 中註冊它。 ### 2. 前端設定元件 {#_2-frontend-settings-component} 建立 `apps/web/src/components/tools/my-tool-settings.tsx`: ```tsx import { useState } from "react"; import { useToolProcessor } from "@/hooks/use-tool-processor"; import { useFileStore } from "@/stores/file-store"; export function MyToolSettings() { const { files } = useFileStore(); const { processFiles, processing, error, downloadUrl } = useToolProcessor("my-tool"); const [intensity, setIntensity] = useState(50); const handleProcess = () => { processFiles(files, { intensity }); }; return (