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 { ConsoleLogger, Injectable, OnModuleInit } from '@nestjs/common';
import { PluginDef, UsePlugin } from 'koishi-nestjs'; import { InjectContext, PluginDef, UsePlugin } from 'koishi-nestjs';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import simpleGit, { ResetMode, SimpleGit } from 'simple-git'; import simpleGit, { ResetMode, SimpleGit } from 'simple-git';
import YGOCardPlugin from 'koishi-plugin-ygocard'; import YGOCardPlugin from 'koishi-plugin-ygocard';
import { Cron } from '@nestjs/schedule'; import { Cron } from '@nestjs/schedule';
import { Context, ForkScope } from 'koishi';
async function exists(path: string) { async function exists(path: string) {
try { try {
...@@ -17,13 +18,16 @@ async function exists(path: string) { ...@@ -17,13 +18,16 @@ async function exists(path: string) {
} }
@Injectable() @Injectable()
export class CdbLoaderService extends ConsoleLogger { export class CdbLoaderService extends ConsoleLogger implements OnModuleInit {
private repoUrl = this.config.get<string>('CDB_REPO'); private repoUrl = this.config.get<string>('CDB_REPO');
private repoPath = path.join(process.cwd(), 'ygopro-database'); private repoPath = path.join(process.cwd(), 'ygopro-database');
private repoBranch = this.config.get<string>('CDB_BRANCH'); private repoBranch = this.config.get<string>('CDB_BRANCH');
private git: SimpleGit; private git: SimpleGit;
constructor(private config: ConfigService) { constructor(
private config: ConfigService,
@InjectContext() private ctx: Context,
) {
super('CdbLoaderService'); super('CdbLoaderService');
} }
...@@ -39,10 +43,11 @@ export class CdbLoaderService extends ConsoleLogger { ...@@ -39,10 +43,11 @@ export class CdbLoaderService extends ConsoleLogger {
this.git = simpleGit(this.repoPath); this.git = simpleGit(this.repoPath);
} }
@UsePlugin() private state: ForkScope;
async load() {
async onModuleInit() {
await this.initRepo(); await this.initRepo();
return PluginDef(YGOCardPlugin, { this.state = this.ctx.plugin(YGOCardPlugin, {
databasePaths: [path.join(this.repoPath, 'locales', 'zh-CN')], databasePaths: [path.join(this.repoPath, 'locales', 'zh-CN')],
matchCount: 20, matchCount: 20,
lang: 'cn', lang: 'cn',
...@@ -55,6 +60,9 @@ export class CdbLoaderService extends ConsoleLogger { ...@@ -55,6 +60,9 @@ export class CdbLoaderService extends ConsoleLogger {
this.log('Updating repo'); this.log('Updating repo');
await this.git.fetch(['origin', this.repoBranch]); await this.git.fetch(['origin', this.repoBranch]);
await this.git.reset(ResetMode.HARD, ['origin/' + this.repoBranch]); await this.git.reset(ResetMode.HARD, ['origin/' + this.repoBranch]);
if (this.state) {
this.state.restart();
}
this.log('Repo updated'); 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