From ea77ae4488f08b5f62c70f225e116251793818be Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Fri, 14 Jun 2024 20:05:49 -0500 Subject: [PATCH] publisher --- packages/ioc/package.json | 12 ++++----- packages/localizer/index.ts | 14 +++++------ packages/publisher/index.ts | 44 ++++++++++++++++----------------- packages/publisher/package.json | 2 +- yarn.lock | 2 +- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/packages/ioc/package.json b/packages/ioc/package.json index 9d63bc6..bb5ad56 100644 --- a/packages/ioc/package.json +++ b/packages/ioc/package.json @@ -3,15 +3,15 @@ "version": "1.0.3", "description": "Dependency Injection system", "main": "dist/index.js", - "module": "./dist/index.js", + "module": "./dist/index.js", "exports": { - "." : { - "import": "./dist/index.js", - "require": "./dist/index.js" + ".": { + "import": "./dist/index.js", + "require": "./dist/index.js" }, "./global": { - "import": "./dist/global.js", - "require": "./dist/global.js" + "import": "./dist/global.js", + "require": "./dist/global.js" } }, "scripts": { diff --git a/packages/localizer/index.ts b/packages/localizer/index.ts index 689990b..a08347e 100644 --- a/packages/localizer/index.ts +++ b/packages/localizer/index.ts @@ -67,6 +67,7 @@ export const local = (i: string, local: string) => { /** * An init plugin to add localization fields to a command module. * Your localization configuration should look like, + * sets nloc and dloc on locals field of module. * @param root {string} If you have conflicting command names, you may configure the root of the name. (= command/{root}) * Below is es-ES.json (spanish) * ```json @@ -88,16 +89,13 @@ export const localize = (root?: string) => CommandInitPlugin(({ module, deps }) => { if(module.type === CommandType.Slash || module.type === CommandType.Both) { const { localizer, '@sern/logger':log } = deps - log?.info({ message: "Localizing "+ module.name }); const resolvedRoot = 'command/'+(root??module.name); - dfsApplyLocalization(module.options ?? [], deps, [resolvedRoot]); + log?.info({ message: "Localizing "+ resolvedRoot }); //@ts-ignore - return controller.next({ - locals: { - nloc: localizer.translationsFor(resolvedRoot+".name"), - dloc: localizer.translationsFor(resolvedRoot+'.description') - } - }); + dfsApplyLocalization(module.options ?? [], deps, [resolvedRoot]); + Reflect.set(module.locals, 'nloc', localizer.translationsFor(resolvedRoot+".name")) + Reflect.set(module.locals, 'dloc', localizer.translationsFor(resolvedRoot+'.description')) + return controller.next(); } else { //@ts-ignore return controller.stop("Cannot localize this type of module " + module.name); diff --git a/packages/publisher/index.ts b/packages/publisher/index.ts index 7a779bd..1d3e135 100644 --- a/packages/publisher/index.ts +++ b/packages/publisher/index.ts @@ -39,8 +39,6 @@ const serializePerms = (perms: unknown) => { const BASE_URL = new URL('https://discord.com/api/v10/applications/'); const PUBLISHABLE = 0b1110; -const PUBLISH = Symbol.for('@sern/publish') - export class Publisher implements Init { constructor(private modules: Map, @@ -63,21 +61,26 @@ export class Publisher implements Init { throw e; } const GLOBAL_URL = new URL(`${appid}/commands`, BASE_URL); - + interface LocalPublish { + guildIds?: string[] + default_member_permissions: string, + integration_types: string[], + contexts: number[] + } const listener = async () => { this.logger.info({ message: 'publishing modules' }); const modules = Array.from(this.modules.values()) .filter(module => (module.type & PUBLISHABLE) != 0) .map(module => { + const publish = module.locals.publish as LocalPublish || {} return { - //@ts-ignore - guildIds: module.publish.guildIds ?? [], + guildIds: publish?.guildIds ?? [], toJSON() { const applicationType = intoApplicationType(module.type); const { default_member_permissions, - integration_types=['Guild'],//@ts-ignore - contexts } = module.publish ?? {}; + integration_types, + contexts } = publish; return { name: module.name, type: applicationType, //@ts-ignore we know description is at least empty str or filled @@ -85,8 +88,7 @@ export class Publisher implements Init { //@ts-ignore shutup options: optionsTransformer(module?.options), default_member_permissions, - integration_types, - contexts, + integration_types, contexts, //@ts-ignore name_localizations: module.locals.nloc, //@ts-ignore @@ -96,6 +98,7 @@ export class Publisher implements Init { } }) const [globalCommands, guildedCommands] = modules.reduce( + //technically these aren't sern/handler modules. ([globals, guilded], module) => { const isPublishableGlobally = !module.guildIds || module.guildIds.length === 0; if (isPublishableGlobally) { @@ -119,7 +122,7 @@ export class Publisher implements Init { const guildIdMap: Map = new Map(); const responsesMap = new Map(); guildedCommands.forEach((entry) => { - const guildIds: string[] = entry[PUBLISH].guildIds ?? []; + const guildIds: string[] = entry.guildIds ?? []; if (guildIds) { guildIds.forEach((guildId) => { if (guildIdMap.has(guildId)) { @@ -197,6 +200,7 @@ const IntegrationType = { * the publishConfig plugin. * If your commandModule requires extra properties such as publishing for certain guilds, you would * put those options in there. + * sets 'publish' on locals field for modules. * @param {ValidPublishOptions} config options to configure how this module is published */ export const publishConfig = (config: ValidPublishOptions) => { @@ -207,20 +211,16 @@ export const publishConfig = (config: ValidPublishOptions) => { } let _config=config if(typeof _config === 'function') { - _config = _config(absPath, module); + _config = _config(absPath, module as CommandModule); } const { contexts, defaultMemberPermissions, integrationTypes:integration_types, guildIds } = _config - //@ts-ignore - return controller.next({ - locals: { - publish: { - guildIds, - contexts, - integration_types: integration_types?.map(i => Reflect.get(IntegrationType, i)), - default_member_permissions: serializePerms(defaultMemberPermissions), - } - } - }); + Reflect.set(module.locals, 'publish', { + guildIds, + contexts, + integration_types: integration_types?.map(i => Reflect.get(IntegrationType, i)), + default_member_permissions: serializePerms(defaultMemberPermissions), + }) + return controller.next(); }) } diff --git a/packages/publisher/package.json b/packages/publisher/package.json index 16bcb3f..340de9d 100644 --- a/packages/publisher/package.json +++ b/packages/publisher/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "tsc", "watch": "tsc --watch", - "test": "vitest --run" + "test": "exit 0" }, "devDependencies": { "@sern/handler": "^3.3.0", diff --git a/yarn.lock b/yarn.lock index 7f15779..7f51199 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2448,7 +2448,7 @@ __metadata: "typescript@patch:typescript@^5.0.0#~builtin": version: 5.4.5 - resolution: "typescript@patch:typescript@npm%3A5.4.5#~builtin::version=5.4.5&hash=f3b441" + resolution: "typescript@patch:typescript@npm%3A5.4.5#~builtin::version=5.4.5&hash=14eedb" bin: tsc: bin/tsc tsserver: bin/tsserver