mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-01 20:46:08 +00:00
fix: reload lang file (#329)
This commit is contained in:
parent
3c4043b2b1
commit
008c2da719
@ -279,17 +279,7 @@ export const ReplaceRegex = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export async function magicImport(path: string) {
|
export async function magicImport(path: string) {
|
||||||
try {
|
return new Function('path', 'return import(`file:///${path}?update=${Date.now()}`)')(path);
|
||||||
if ('Deno' in globalThis) throw new Error('https://github.com/denoland/deno/issues/26136');
|
|
||||||
return require(path);
|
|
||||||
} catch (e: any) {
|
|
||||||
// (bun)dows moment
|
|
||||||
if (!('Bun' in globalThis) || e.message.includes('is unsupported. use "await import()" instead.'))
|
|
||||||
return new Function('path', 'return import(`file:///${path}?update=${Date.now()}`)')(
|
|
||||||
path.split('\\').join('\\\\'),
|
|
||||||
);
|
|
||||||
throw new Error(`Cannot import ${path}`, { cause: e });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type OnFailCallback = (error: unknown) => any;
|
export type OnFailCallback = (error: unknown) => any;
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { basename } from 'node:path';
|
||||||
import type { FileLoaded } from '../commands/handler';
|
import type { FileLoaded } from '../commands/handler';
|
||||||
import { BaseHandler, isCloudfareWorker, magicImport } from '../common';
|
import { BaseHandler, isCloudfareWorker, magicImport } from '../common';
|
||||||
import type { Locale, LocaleString } from '../types';
|
import type { Locale, LocaleString } from '../types';
|
||||||
@ -63,11 +64,18 @@ export class LangsHandler extends BaseHandler {
|
|||||||
if (isCloudfareWorker()) {
|
if (isCloudfareWorker()) {
|
||||||
throw new Error('Reload in cloudfare worker is not supported');
|
throw new Error('Reload in cloudfare worker is not supported');
|
||||||
}
|
}
|
||||||
const value = this.__paths[lang];
|
const path = this.__paths[lang];
|
||||||
|
if (!path) return null;
|
||||||
|
delete require.cache[path];
|
||||||
|
const value = await magicImport(path).then(x =>
|
||||||
|
this.onFile(lang, {
|
||||||
|
file: x,
|
||||||
|
name: basename(path),
|
||||||
|
path,
|
||||||
|
} satisfies LangInstance),
|
||||||
|
);
|
||||||
if (!value) return null;
|
if (!value) return null;
|
||||||
delete require.cache[value];
|
return (this.values[lang] = value.file);
|
||||||
|
|
||||||
return (this.values[lang] = await magicImport(value).then(x => this.onFile(lang, x)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async reloadAll(stopIfFail = true) {
|
async reloadAll(stopIfFail = true) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user