Commit b1abde72 authored by nanahira's avatar nanahira

fix reload cdb plugin

parent a5470f96
Pipeline #19410 canceled with stages
in 15 seconds
import { ConsoleLogger, Injectable } from '@nestjs/common';
import { PluginDef, UsePlugin } from 'koishi-nestjs';
import { ConsoleLogger, Injectable, OnModuleInit } from '@nestjs/common';
import { InjectContext, PluginDef, UsePlugin } from 'koishi-nestjs';
import { ConfigService } from '@nestjs/config';
import path from 'path';
import fs from 'fs';
import simpleGit, { ResetMode, SimpleGit } from 'simple-git';
import YGOCardPlugin from 'koishi-plugin-ygocard';
import { Cron } from '@nestjs/schedule';
import { Context, ForkScope } from 'koishi';
async function exists(path: string) {
try {
......@@ -17,13 +18,16 @@ async function exists(path: string) {
}
@Injectable()
export class CdbLoaderService extends ConsoleLogger {
export class CdbLoaderService extends ConsoleLogger implements OnModuleInit {
private repoUrl = this.config.get<string>('CDB_REPO');
private repoPath = path.join(process.cwd(), 'ygopro-database');
private repoBranch = this.config.get<string>('CDB_BRANCH');
private git: SimpleGit;
constructor(private config: ConfigService) {
constructor(
private config: ConfigService,
@InjectContext() private ctx: Context,
) {
super('CdbLoaderService');
}
......@@ -39,10 +43,11 @@ export class CdbLoaderService extends ConsoleLogger {
this.git = simpleGit(this.repoPath);
}
@UsePlugin()
async load() {
private state: ForkScope;
async onModuleInit() {
await this.initRepo();
return PluginDef(YGOCardPlugin, {
this.state = this.ctx.plugin(YGOCardPlugin, {
databasePaths: [path.join(this.repoPath, 'locales', 'zh-CN')],
matchCount: 20,
lang: 'cn',
......@@ -55,6 +60,9 @@ export class CdbLoaderService extends ConsoleLogger {
this.log('Updating repo');
await this.git.fetch(['origin', this.repoBranch]);
await this.git.reset(ResetMode.HARD, ['origin/' + this.repoBranch]);
if (this.state) {
this.state.restart();
}
this.log('Repo updated');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment