Compare commits

..

3 Commits

2 changed files with 22 additions and 17 deletions

View File

@ -1,5 +1,5 @@
import type { Postgres } from '$lib/server/database/postgres'; 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 fs from 'fs';
import compile from '$lib/server/article_compiler'; import compile from '$lib/server/article_compiler';
import type { TableSchema } from '$lib/server/database/table'; 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.query('update tag set ref_count = 0');
await db.begin(); await db.begin();
let i = 0;
for (const { path, id } of articleFiles) { for (const { path, id } of articleFiles) {
await db.savepoint(id); await db.savepoint(`load${i}`);
console.log(`Processing ${id}...`); console.log(`Processing ${id}...`);
try { try {
const gitlog = await git.log({ const gitlog = await git.log({
@ -66,38 +67,29 @@ const load = async (db: Postgres, git: SimpleGit) => {
} }
} catch (err) { } catch (err) {
console.log(err); console.log(err);
await db.rollbackTo(id); await db.rollbackTo(`load${i}`);
} finally { } finally {
console.log(''); console.log('');
await db.releaseSavepoint(id); await db.releaseSavepoint(`load${i}`);
i++;
} }
} }
await db.commit(); 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 = { type ArticleFileItem = {
path: string, path: string,
id: string, id: string,
} }
const cloneRepo = async (git: SimpleGit) => { 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..'); console.log('Pulling articles from git..');
await git.pull(); await git.pull();
} else { } else {
console.log('Cloning articles from git..'); 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<ArticleFileItem[]> => {
} }
scanDir('./articles/article'); scanDir('./articles/article');
return articleFiles; 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);
} }

View File

@ -1,7 +1,9 @@
import fs from 'fs';
import { simpleGit, } from 'simple-git'; import { simpleGit, } from 'simple-git';
import type { SimpleGit, SimpleGitOptions } from 'simple-git'; import type { SimpleGit, SimpleGitOptions } from 'simple-git';
console.log(process.cwd()+'/articles'); console.log(process.cwd()+'/articles');
fs.mkdirSync(process.cwd()+'/articles', { recursive: true });
export const gitOptions: Partial<SimpleGitOptions> = { export const gitOptions: Partial<SimpleGitOptions> = {
baseDir: process.cwd()+'/articles', baseDir: process.cwd()+'/articles',
binary: 'git', binary: 'git',