From e83a011ebd01dea2ec2cf71b1406594c569123de Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Wed, 24 Jan 2024 10:20:05 -0600 Subject: [PATCH] proposing api notation? --- TEST PLEASE DELETE.js | 20 -------------------- src/core/contracts/localizer.ts | 2 +- src/core/ioc/dependency-injection.ts | 11 ++++++++++- src/core/ioc/index.ts | 2 +- src/core/module-loading.ts | 1 + src/optional/localizer.ts | 7 ++++++- src/sern.ts | 2 +- src/types/core-modules.ts | 1 - src/types/ioc.ts | 4 ++-- 9 files changed, 22 insertions(+), 28 deletions(-) delete mode 100644 TEST PLEASE DELETE.js diff --git a/TEST PLEASE DELETE.js b/TEST PLEASE DELETE.js deleted file mode 100644 index 7fe3534f..00000000 --- a/TEST PLEASE DELETE.js +++ /dev/null @@ -1,20 +0,0 @@ -const { Localization } = require('shrimple-locales'); - -//ok so plan is, -/** - * - * await makeDependencies(({ include })=> { - * include('@sern/localizer') // preconfigure localizer, will expose the translate function - * }); - * - */ -const loc = new Localization({ - defaultLocale: 'en', - fallbackLocale: 'en', - locales: { - en: { somethingInside: { hey: 'hi' }, somethingOutside: 'hello' }, - es: { somethingInside: { hey: 'hola inside' }, somethingOutside: 'hola' } - } -}); - -console.log(loc.get('somethingOutside')); \ No newline at end of file diff --git a/src/core/contracts/localizer.ts b/src/core/contracts/localizer.ts index c7e76b27..364a0689 100644 --- a/src/core/contracts/localizer.ts +++ b/src/core/contracts/localizer.ts @@ -1,4 +1,4 @@ export interface Localizer { - translate(text: string) : string; + translate(text: string, local: string) : string; } diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index bd08466a..3fb996d1 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert'; import type { IntoDependencies } from '../../types/ioc'; import { useContainerRaw } from './base'; @@ -33,7 +34,9 @@ export function transient(cb: () => () => T) { * */ export function Service(key: T) { - return useContainerRaw().get(key)!; + const dep = useContainerRaw().get(key)!; + assert(dep, "Requested key " + key + " returned undefined"); + return dep; } /** * @since 3.0.0 @@ -51,3 +54,9 @@ export function useContainer() { return (...keys: [...V]) => keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies; } + + +export function $local(i: string, local: string) { + return Service('@sern/localizer').translate(i, local) +} + diff --git a/src/core/ioc/index.ts b/src/core/ioc/index.ts index e89f8b6f..77cf4bbc 100644 --- a/src/core/ioc/index.ts +++ b/src/core/ioc/index.ts @@ -1,2 +1,2 @@ export { makeDependencies } from './base'; -export { Service, Services, single, transient } from './dependency-injection'; +export { Service, Services, single, transient, $local } from './dependency-injection'; diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index 91a10111..81146880 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -18,6 +18,7 @@ export const shouldHandle = (path: string, fpath: string) => { export type ModuleResult = Promise>; + /** * Import any module based on the absolute path. * This can accept four types of exported modules diff --git a/src/optional/localizer.ts b/src/optional/localizer.ts index 4d85b7e3..569becb9 100644 --- a/src/optional/localizer.ts +++ b/src/optional/localizer.ts @@ -1,8 +1,11 @@ import type { Localizer, Init } from '../core/contracts' import { Localization } from 'shrimple-locales' import fs from 'node:fs/promises' +import { existsSync } from 'node:fs' import { join, resolve } from 'node:path'; import { filename } from '../core/module-loading' +import assert from 'node:assert'; + /** * @internal * @since 2.0.0 @@ -11,7 +14,8 @@ import { filename } from '../core/module-loading' export class ShrimpleLocalizer implements Localizer, Init { private __localization!: Localization; constructor(){} - translate(text: string): string { + translate(text: string, local: string): string { + this.__localization.changeLanguage(local); return this.__localization.get(text); } @@ -26,6 +30,7 @@ export class ShrimpleLocalizer implements Localizer, Init { private async readLocalizationDirectory() { const translationFiles = []; const localPath = resolve('locals'); + assert(existsSync(localPath), "You need to create a directory called \"locals\" for the localizer") for(const json of await fs.readdir(localPath)) { translationFiles.push({ [filename(json)]: JSON.parse(await fs.readFile(join(localPath, json), 'utf8')) }) diff --git a/src/sern.ts b/src/sern.ts index 495e331c..289d3dc2 100644 --- a/src/sern.ts +++ b/src/sern.ts @@ -1,6 +1,6 @@ import { handleCrash } from './handlers/_internal'; import callsites from 'callsites'; -import { err, ok, Files } from './core/_internal'; +import { Files } from './core/_internal'; import { merge } from 'rxjs'; import { Services } from './core/ioc'; import { Wrapper } from './types/core'; diff --git a/src/types/core-modules.ts b/src/types/core-modules.ts index e53320a4..d0f98349 100644 --- a/src/types/core-modules.ts +++ b/src/types/core-modules.ts @@ -20,7 +20,6 @@ import { AnyCommandPlugin, AnyEventPlugin, ControlPlugin, InitPlugin } from './c import { Awaitable, Args, SlashOptions, SernEventsMapping } from './utility'; - export interface CommandMeta { fullPath: string; id: string; diff --git a/src/types/ioc.ts b/src/types/ioc.ts index 9b821fd0..042d83db 100644 --- a/src/types/ioc.ts +++ b/src/types/ioc.ts @@ -26,11 +26,12 @@ export type DependencyList = [ export interface CoreDependencies { '@sern/client': () => Contracts.Emitter; - '@sern/logger'?: () => Contracts.Logging; '@sern/emitter': () => Contracts.Emitter; '@sern/store': () => Contracts.CoreModuleStore; '@sern/modules': () => Contracts.ModuleManager; '@sern/errors': () => Contracts.ErrorHandling; + '@sern/logger'?: () => Contracts.Logging; + '@sern/localizer'?: () => Contracts.Localizer } export type DependencyFromKey = Dependencies[T]; @@ -45,7 +46,6 @@ export interface DependencyConfiguration { //Extra modules that are preconfigured and ready to use! @sern/localizer is an example include?: string[] - build: ( root: Container, {}>, ) => Container;