From dff1188e4b1b674e72cc7f62380ee91307dc94bd Mon Sep 17 00:00:00 2001 From: fraxken Date: Fri, 1 Sep 2023 19:43:06 +0200 Subject: [PATCH] fix: use ESM dynamic import and export default --- README.md | 2 +- index.d.ts | 2 +- index.js | 11 ++++++++--- test/i18n.js | 9 +++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 016d46c..5e8a4fb 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ export function getTokenSync(token: string, ...parameters): string; export function getLanguages(): Promise; export function taggedString(str: string, ...keys: any[]): (...keys: any[]) => string; export function extend(language: string, tokens: Record): void; -export function extendFromSystemPath(path: string): void; +export function extendFromSystemPath(path: string): Promise; ``` > **Note** diff --git a/index.d.ts b/index.d.ts index fca6cb1..e89aaae 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,7 +8,7 @@ declare namespace i18n { export function getLanguages(): Promise; export function taggedString(str: string, ...keys: any[]): (...keys: any[]) => string; export function extend(language: string, tokens: Record): void; - export function extendFromSystemPath(path: string): void; + export function extendFromSystemPath(path: string): Promise; } export = i18n; diff --git a/index.js b/index.js index 92dd9c0..a81cb38 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ // Import Node.js Dependencies import fs from "node:fs"; import path from "node:path"; +import { pathToFileURL } from "node:url"; // Import Third-party Depedencies import cacache from "cacache"; @@ -94,7 +95,7 @@ export function extend(extendLanguage, opts = {}) { } } -export function extendFromSystemPath(languagesDirPath) { +export async function extendFromSystemPath(languagesDirPath) { if (!fs.existsSync(languagesDirPath)) { throw new Error(`The ${languagesDirPath} directory does not exist on this project.`); } @@ -103,7 +104,11 @@ export function extendFromSystemPath(languagesDirPath) { for (const file of files) { const langName = path.basename(file.name, ".js"); - const data = fs.readFileSync(path.join(languagesDirPath, file.name), "utf-8"); - extend(langName, JSON.parse(data)); + const fileLocation = path.join(languagesDirPath, file.name); + + const i18nTokensFile = await import(pathToFileURL(fileLocation)); + if ("default" in i18nTokensFile) { + extend(langName, i18nTokensFile.default); + } } } diff --git a/test/i18n.js b/test/i18n.js index 1909272..0097f97 100644 --- a/test/i18n.js +++ b/test/i18n.js @@ -149,8 +149,8 @@ describe("extend", () => { }); describe("extendFromSystemPath", () => { - it("extendFromSystemPath with non-existing languages directory", () => { - assert.throws( + it("extendFromSystemPath with non-existing languages directory", async() => { + await assert.rejects( () => i18n.extendFromSystemPath(kI18nDir), { name: "Error", @@ -164,10 +164,11 @@ describe("extendFromSystemPath", () => { fs.mkdirSync(kI18nDir); fs.writeFileSync( path.join(kI18nDir, "french.js"), - JSON.stringify({ hello: "Bonjour" }) + // eslint-disable-next-line quotes + `export default { hello: "Bonjour" }` ); - i18n.extendFromSystemPath(kI18nDir); + await i18n.extendFromSystemPath(kI18nDir); await i18n.setLocalLang("french"); assert.deepEqual(await i18n.getToken("hello"), "Bonjour");