Skip to content

Commit

Permalink
step 1
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Apr 28, 2024
1 parent 91b3768 commit 30230d4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 28 deletions.
9 changes: 3 additions & 6 deletions src/core/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ import { PayloadType, PluginType } from './structures';
import assert from 'assert';
import type { Payload } from '../types/utility';

//function wrappers for empty ok / err
export const ok = /* @__PURE__*/ () => Ok.EMPTY;
export const err = /* @__PURE__*/ () => Err.EMPTY;
export const ok = () => Ok.EMPTY;
export const err = () => Err.EMPTY;

export function partitionPlugins(
arr: (AnyEventPlugin | AnyCommandPlugin)[] = [],
): [Plugin[], Plugin[]] {
export function partitionPlugins(arr: (AnyEventPlugin | AnyCommandPlugin)[] = []): [Plugin[], Plugin[]] {
const controlPlugins = [];
const initPlugins = [];

Expand Down
4 changes: 4 additions & 0 deletions src/core/module-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import type { Module } from '../types/core-modules';
import { existsSync } from 'fs';
import type { Logging } from './contracts/logging';


export const parseCallsite = (fpath: string) => {
return parse(fpath.replace(/file:\\?/, "")).name;
}
export const shouldHandle = (path: string, fpath: string) => {
const file_name = fpath+extname(path);
let newPath = join(dirname(path), file_name)
Expand Down
20 changes: 15 additions & 5 deletions src/core/modules.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ClientEvents } from 'discord.js';
import { EventType } from '../core/structures';
import type {
AnyEventPlugin,
} from '../types/core-plugin';
import type { AnyEventPlugin, } from '../types/core-plugin';
import type {
CommandModule,
EventModule,
Expand All @@ -11,18 +9,27 @@ import type {
} from '../types/core-modules';
import { partitionPlugins } from './_internal';
import type { Awaitable } from '../types/utility';

import callsites from 'callsites';
import * as Files from './module-loading'
import path, { basename } from 'path';
import * as Id from './id'
/**
* @since 1.0.0 The wrapper function to define command modules for sern
* @param mod
*/
export function commandModule(mod: InputCommand): CommandModule {
const [onEvent, plugins] = partitionPlugins(mod.plugins);
const initCallsite = callsites()[1].getFileName()?.replace(/file:\\?/, "");
if(!initCallsite) throw Error("initCallsite is null");
const filename = Files.parseCallsite(initCallsite);
mod.name ??= filename;
const id = Id.create(mod.name, mod.type)
return {
...mod,
__id: id,
onEvent,
plugins,
} as CommandModule;
} as unknown as CommandModule;
}
/**
* @since 1.0.0
Expand All @@ -31,6 +38,9 @@ export function commandModule(mod: InputCommand): CommandModule {
*/
export function eventModule(mod: InputEvent): EventModule {
const [onEvent, plugins] = partitionPlugins(mod.plugins);
const initCallsite = callsites()[1].getFileName();
console.log(initCallsite?.replace(/file:\\?/, ""))

return {
...mod,
plugins,
Expand Down
3 changes: 1 addition & 2 deletions src/core/presences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export type Config <T extends (keyof Dependencies)[]> =
* Create a Presence module which **MUST** be put in a file called presence.(language-extension)
* adjacent to the file where **Sern.init** is CALLED.
*/
export function module<T extends (keyof Dependencies)[]>(conf: Config<T>)
{ return conf; }
export const module = <T extends (keyof Dependencies)[]>(conf: Config<T>) => conf;


/**
Expand Down
20 changes: 9 additions & 11 deletions test/core/module-loading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ describe('module-loading', () => {
const filename = Files.fmtFileName(name+'.'+extension);
expect(filename).toBe(name)
})

// todo: handle commands with multiple extensions
// it('should properly extract filename from file, nested multiple', () => {
// const extension = faker.system.fileExt()
// const extension2 = faker.system.fileExt()
// const name = faker.system.fileName({ extensionCount: 0 })
// const filename = Files.fmtFileName(name+'.'+extension+'.'+extension2);
// console.log(filename, name)
// expect(filename).toBe(name)
//
// })
it('should get the filename of the commandmodule (linux)', () => {
const fname = "///home/pooba/Projects/sern/halibu/dist/commands/ping.js"
expect(Files.parseCallsite(fname)).toBe("ping")
})
it('should get the filename of the commandmodule (windows)', () => {
//const fname = "C:\\pooba\\Projects\\sern\\halibu\\dist\\commands\\ping.js"
//expect(Files.parseCallsite(fname)).toBe("ping")
})

})
7 changes: 3 additions & 4 deletions test/core/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { SpyInstance, afterAll, beforeEach, describe, expect, it, vi } from 'vit
import { CoreContainer } from '../../src/core/ioc/container';
import { DefaultLogging } from '../../src/core';
import { faker } from '@faker-js/faker';
import { commandModule } from '../../src';
import { commandModule, CommandType } from '../../src';
import * as Id from '../../src/core/id';
import { CommandMeta } from '../../src/types/core-modules';

describe('services', () => {
//@ts-ignore
let container: CoreContainer<Dependencies>;
Expand All @@ -23,9 +22,9 @@ describe('services', () => {
it('module-store.ts', async () => {
function createRandomCommandModules() {
return commandModule({
type: faker.number.int({ min: 1 << 0, max: 1 << 10 }),
type: CommandType.Slash,
description: faker.string.alpha(),
name: faker.string.alpha(),
name: faker.string.alpha({ length: { min: 5, max: 10 }}),
execute: () => {},
});
}
Expand Down

1 comment on commit 30230d4

@SrIzan10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hype

Please sign in to comment.