mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 04:56:07 +00:00
feat: reload langs
This commit is contained in:
parent
983043f9c3
commit
084c85368e
@ -1,10 +1,11 @@
|
||||
import { BaseHandler } from '../common';
|
||||
import { BaseHandler, isCloudfareWorker, magicImport } from '../common';
|
||||
import { LangRouter } from './router';
|
||||
import type { FileLoaded } from '../commands/handler';
|
||||
import type { LocaleString, Locale } from '../types';
|
||||
|
||||
export class LangsHandler extends BaseHandler {
|
||||
values: Partial<Record<string, any>> = {};
|
||||
private __paths: Partial<Record<string, string>> = {};
|
||||
protected filter = (path: string) =>
|
||||
path.endsWith('.js') || (!path.endsWith('.d.ts') && path.endsWith('.ts')) || path.endsWith('.json');
|
||||
defaultLang?: string;
|
||||
@ -47,6 +48,7 @@ export class LangsHandler extends BaseHandler {
|
||||
parse(file: EventInstance) {
|
||||
const locale = file.name.split('.').slice(0, -1).join('.') || file.name;
|
||||
const result = this.onFile(locale, file.file);
|
||||
if ('path' in file) this.__paths[locale] = file.path as string;
|
||||
if (result) this.values[locale] = result;
|
||||
}
|
||||
|
||||
@ -56,6 +58,27 @@ export class LangsHandler extends BaseHandler {
|
||||
}
|
||||
}
|
||||
|
||||
async reload(lang: string) {
|
||||
if (isCloudfareWorker()) {
|
||||
throw new Error('Reload in cloudfare worker is not supported');
|
||||
}
|
||||
const value = this.__paths[lang];
|
||||
if (!value) return null;
|
||||
delete require.cache[value];
|
||||
|
||||
return (this.values[lang] = await magicImport(value).then(x => this.onFile(lang, x)));
|
||||
}
|
||||
|
||||
async reloadAll(stopIfFail = true) {
|
||||
for (const i in this.__paths) {
|
||||
try {
|
||||
await this.reload(i);
|
||||
} catch (e) {
|
||||
if (stopIfFail) throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onFile(_locale: string, file: FileLoaded<Record<string, any>>): Record<string, any> | false {
|
||||
return file.default ?? false;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user