From 5b441ff0cf6eefee765fa2d70b2d4c147ef767c9 Mon Sep 17 00:00:00 2001 From: Hare Date: Fri, 27 Sep 2024 00:48:22 +0900 Subject: [PATCH] fix: Clone and analyze the repository correctly --- src/lib/server/data_loader.ts | 37 +++++++++++++++++++---------------- src/lib/server/git.ts | 2 ++ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/lib/server/data_loader.ts b/src/lib/server/data_loader.ts index a050696..f0ea537 100644 --- a/src/lib/server/data_loader.ts +++ b/src/lib/server/data_loader.ts @@ -1,5 +1,5 @@ import type { Postgres } from '$lib/server/database/postgres'; -import type { SimpleGit } from 'simple-git'; +import { type SimpleGit, CheckRepoActions } from 'simple-git'; import fs from 'fs'; import compile from '$lib/server/article_compiler'; import type { TableSchema } from '$lib/server/database/table'; @@ -11,8 +11,9 @@ const load = async (db: Postgres, git: SimpleGit) => { await db.query('update tag set ref_count = 0'); await db.begin(); + let i = 0; for (const { path, id } of articleFiles) { - await db.savepoint(id); + await db.savepoint(`load${i}`); console.log(`Processing ${id}...`); try { const gitlog = await git.log({ @@ -66,38 +67,29 @@ const load = async (db: Postgres, git: SimpleGit) => { } } catch (err) { console.log(err); - await db.rollbackTo(id); + await db.rollbackTo(`load${i}`); } finally { console.log(''); - await db.releaseSavepoint(id); + await db.releaseSavepoint(`load${i}`); + i++; } } await db.commit(); } -export async function init(db: Postgres, git: SimpleGit) { - await createTable(db, server_table); - await cloneRepo(git); - await load(db, git); -} - -export async function reload(db: Postgres, git: SimpleGit) { - await cloneRepo(git); - await load(db, git); -} - type ArticleFileItem = { path: string, id: string, } const cloneRepo = async (git: SimpleGit) => { - if (fs.existsSync('./articles/')) { + const isRepoRoot = await git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT); + if (isRepoRoot) { console.log('Pulling articles from git..'); await git.pull(); } else { console.log('Cloning articles from git..'); - await git.clone('git@gitea.hareworks.net:Hare/blog-articles.git', 'articles'); + await git.clone('git@gitea.hareworks.net:Hare/blog-articles.git', './'); } } @@ -138,4 +130,15 @@ const crawlArticles = async (db: Postgres): Promise => { } scanDir('./articles/article'); return articleFiles; +} + +export async function init(db: Postgres, git: SimpleGit) { + await createTable(db, server_table); + await cloneRepo(git); + await load(db, git); +} + +export async function reload(db: Postgres, git: SimpleGit) { + await cloneRepo(git); + await load(db, git); } \ No newline at end of file diff --git a/src/lib/server/git.ts b/src/lib/server/git.ts index dc2e70a..86bbb2d 100644 --- a/src/lib/server/git.ts +++ b/src/lib/server/git.ts @@ -1,7 +1,9 @@ +import fs from 'fs'; import { simpleGit, } from 'simple-git'; import type { SimpleGit, SimpleGitOptions } from 'simple-git'; console.log(process.cwd()+'/articles'); +fs.mkdirSync(process.cwd()+'/articles', { recursive: true }); export const gitOptions: Partial = { baseDir: process.cwd()+'/articles', binary: 'git', -- 2.43.0