From 14e231af7d9e001ac70a10bf84af7571898fa6d9 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Tue, 9 Jan 2024 15:46:09 -0600 Subject: [PATCH] got optional localizer working --- package.json | 4 +-- src/core/ioc/base.ts | 42 +++++++++++++++++++---- src/core/ioc/dependency-injection.ts | 32 ++--------------- src/core/structures/optional/localizer.ts | 20 ----------- src/optional/localizer.ts | 35 +++++++++++++++++++ tsup.config.js | 2 +- yarn.lock | 8 ++--- 7 files changed, 79 insertions(+), 64 deletions(-) delete mode 100644 src/core/structures/optional/localizer.ts create mode 100644 src/optional/localizer.ts diff --git a/package.json b/package.json index 78b707b6..9dd90f30 100644 --- a/package.json +++ b/package.json @@ -48,9 +48,9 @@ "@typescript-eslint/eslint-plugin": "5.58.0", "@typescript-eslint/parser": "5.59.1", "discord.js": "^14.11.0", - "esbuild": "^0.17.0", "eslint": "8.39.0", "prettier": "2.8.8", + "shrimple-locales": "^0.1.2", "tsup": "^6.7.0", "typescript": "5.0.2", "vitest": "latest" @@ -96,7 +96,7 @@ "url": "git+https://github.com/sern-handler/handler.git" }, "homepage": "https://sern.dev", - "optionalDependencies": { + "peerDependencies": { "shrimple-locales": "^0.1.2" } } diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index 06c35e68..fdd0c6c6 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -1,12 +1,14 @@ import * as assert from 'assert'; -import { composeRoot, useContainer } from './dependency-injection'; -import type { DependencyConfiguration } from '../../types/ioc'; +import { useContainer } from './dependency-injection'; +import type { CoreDependencies, DependencyConfiguration } from '../../types/ioc'; import { CoreContainer } from './container'; import { Result } from 'ts-results-es'; import { DefaultServices } from '../_internal'; import { AnyFunction } from '../../types/utility'; import type { Logging } from '../contracts/logging'; import { requir } from '../module-loading'; +import { fileURLToPath } from 'node:url'; +import path from 'path'; //SIDE EFFECT: GLOBAL DI let containerSubject: CoreContainer>; @@ -93,13 +95,41 @@ export const insertLogger = (containerSubject: CoreContainer) => { .upsert({'@sern/logger': () => new DefaultServices.DefaultLogging}); } -export const insertLocalizer = async (containerSubject: CoreContainer) => { - const { ShrimpleLocalizer } = requir('../structures/services/localizer'); +const insertLocalizer = async (containerSubject: CoreContainer) => { + const packageDirectory = fileURLToPath(import.meta.url); + const pathToLocalizer= path.resolve(packageDirectory, "../", "optional", "localizer"); + const { ShrimpleLocalizer } = requir(pathToLocalizer); containerSubject - .add({'@sern/localizer': new ShrimpleLocalizer() }); + .upsert({'@sern/localizer': new ShrimpleLocalizer() }); } +/** + * Given the user's conf, check for any excluded/included dependency keys. + * Then, call conf.build to get the rest of the users' dependencies. + * Finally, update the containerSubject with the new container state + * @param conf + */ +function composeRoot( + container: CoreContainer>, + conf: DependencyConfiguration, +) { + //container should have no client or logger yet. + const hasLogger = conf.exclude?.has('@sern/logger'); + if (!hasLogger) { + insertLogger(container); + } + if(conf.include?.includes('@sern/localizer')) { + insertLocalizer(container); + } + //Build the container based on the callback provided by the user + conf.build(container as CoreContainer>); + + if (!hasLogger) { + container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' }); + } + container.ready(); +} export async function makeDependencies (conf: ValidDependencyConfig) { @@ -118,7 +148,7 @@ export async function makeDependencies } if(included.includes('@sern/localizer')) { - await insertLocalizer(containerSubject); + insertLocalizer(containerSubject); } containerSubject.ready(); diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index 617d8023..bd08466a 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -1,7 +1,5 @@ -import type { CoreDependencies, DependencyConfiguration, IntoDependencies } from '../../types/ioc'; -import { requir } from '../module-loading'; -import { insertLogger, useContainerRaw } from './base'; -import { CoreContainer } from './container'; +import type { IntoDependencies } from '../../types/ioc'; +import { useContainerRaw } from './base'; /** * @__PURE__ @@ -47,33 +45,7 @@ export function Services(...keys: [...T] return keys.map(k => container.get(k)!) as IntoDependencies; } -/** - * Given the user's conf, check for any excluded/included dependency keys. - * Then, call conf.build to get the rest of the users' dependencies. - * Finally, update the containerSubject with the new container state - * @param conf - */ -export function composeRoot( - container: CoreContainer>, - conf: DependencyConfiguration, -) { - //container should have no client or logger yet. - const hasLogger = conf.exclude?.has('@sern/logger'); - if (!hasLogger) { - insertLogger(container); - } - if(conf.include?.includes('@sern/localizer')) { - container.add({ '@sern/localizer': requir('shrimple-locales') }); - } - //Build the container based on the callback provided by the user - conf.build(container as CoreContainer>); - - if (!hasLogger) { - container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' }); - } - container.ready(); -} export function useContainer() { return (...keys: [...V]) => diff --git a/src/core/structures/optional/localizer.ts b/src/core/structures/optional/localizer.ts deleted file mode 100644 index c248111c..00000000 --- a/src/core/structures/optional/localizer.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Localizer, Init } from '../../contracts' -import { Localization } from 'shrimple-locales' -/** - * @internal - * @since 2.0.0 - * Version 4.0.0 will internalize this api. Please refrain from using ModuleStore! - */ -export class ShrimpleLocalizer implements Localizer, Init { - - __localization!: Localization; - translate(text: string): string { - return this.__localization.get(text); - } - - async init() { - //TODO - this.__localization = new Localization({ } as any); - } - -} diff --git a/src/optional/localizer.ts b/src/optional/localizer.ts new file mode 100644 index 00000000..4d85b7e3 --- /dev/null +++ b/src/optional/localizer.ts @@ -0,0 +1,35 @@ +import type { Localizer, Init } from '../core/contracts' +import { Localization } from 'shrimple-locales' +import fs from 'node:fs/promises' +import { join, resolve } from 'node:path'; +import { filename } from '../core/module-loading' +/** + * @internal + * @since 2.0.0 + * Version 4.0.0 will internalize this api. Please refrain from using ModuleStore! + */ +export class ShrimpleLocalizer implements Localizer, Init { + private __localization!: Localization; + constructor(){} + translate(text: string): string { + return this.__localization.get(text); + } + + async init() { + this.__localization = new Localization({ + defaultLocale: "en", + fallbackLocale: "en", + locales: await this.readLocalizationDirectory() + }); + } + + private async readLocalizationDirectory() { + const translationFiles = []; + const localPath = resolve('locals'); + for(const json of await fs.readdir(localPath)) { + translationFiles.push({ [filename(json)]: + JSON.parse(await fs.readFile(join(localPath, json), 'utf8')) }) + } + return translationFiles.reduce((acc, cur ) => ({ ...cur, ...acc }), {}); + } +} diff --git a/tsup.config.js b/tsup.config.js index 73a6edf4..0217257d 100644 --- a/tsup.config.js +++ b/tsup.config.js @@ -1,6 +1,6 @@ import { defineConfig } from 'tsup'; const shared = { - entry: ['src/index.ts', 'src/core/structures/optional/localizer.ts'], + entry: ['src/index.ts', 'src/optional/localizer.ts'], external: ['discord.js', 'iti', 'shrimple-locales'], platform: 'node', clean: true, diff --git a/yarn.lock b/yarn.lock index bcc54eb8..97585378 100644 --- a/yarn.lock +++ b/yarn.lock @@ -629,7 +629,6 @@ __metadata: "@typescript-eslint/parser": 5.59.1 callsites: ^3.1.0 discord.js: ^14.11.0 - esbuild: ^0.17.0 eslint: 8.39.0 iti: ^0.6.0 prettier: 2.8.8 @@ -639,9 +638,8 @@ __metadata: tsup: ^6.7.0 typescript: 5.0.2 vitest: latest - dependenciesMeta: - shrimple-locales: - optional: true + peerDependencies: + shrimple-locales: ^0.1.2 languageName: unknown linkType: soft @@ -1453,7 +1451,7 @@ __metadata: languageName: node linkType: hard -"esbuild@npm:^0.17.0, esbuild@npm:^0.17.6": +"esbuild@npm:^0.17.6": version: 0.17.19 resolution: "esbuild@npm:0.17.19" dependencies: