Skip to content

Commit d6e3fbf

Browse files
committed
refactor: use scan files and remove classes
1 parent b5ae82b commit d6e3fbf

File tree

16 files changed

+238
-294
lines changed

16 files changed

+238
-294
lines changed

bin/index.mjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#!/usr/bin/env node
22

3-
import { Harmony } from '../dist/index.mjs'
4-
import { resolve } from 'pathe'
3+
import { createHarmony } from '../dist/index.mjs'
54
import 'dotenv/config'
65

76
const initHarmony = async () => {
8-
const harmony = await Harmony.create({ cwd: resolve('./playground') })
9-
7+
const harmony = await createHarmony(
8+
{ rootDir: './playground' },
9+
{ cwd: './playground' }
10+
)
1011
console.log(harmony)
11-
12-
harmony.initClient()
1312
}
1413

1514
initHarmony()

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"defu": "^6.1.4",
1919
"discord.js": "^14.15.1",
2020
"dotenv": "^16.4.5",
21+
"globby": "^14.0.1",
2122
"jiti": "^1.21.0",
2223
"pathe": "^1.1.2",
2324
"unbuild": "^2.0.0"

playground/harmony.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { defineHarmonyConfig } from '../src'
22

3-
export default defineHarmonyConfig({})
3+
export default defineHarmonyConfig({
4+
defaultPrefix: 'h!'
5+
})

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands.ts

Lines changed: 31 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,41 @@
1-
import createJiti from 'jiti'
2-
import { filename } from 'pathe/utils'
3-
import type { CommandExecute, CommandResult } from '../types'
1+
import jiti from 'jiti'
42
import { dirname } from 'pathe'
5-
6-
const jiti = createJiti(undefined as unknown as string, {
7-
interopDefault: true,
8-
requireCache: false,
9-
esmResolve: true,
10-
extensions: ['.ts', '.js']
11-
})
12-
13-
interface CommandContext {
14-
name: string
15-
category: string
16-
slash: boolean
17-
execute: CommandExecute<boolean>
18-
}
19-
20-
interface CommandOptions {
21-
name?: string
22-
description?: string
23-
category?: string
24-
arguments?: any[]
25-
nsfw?: boolean
26-
slash?: boolean
27-
ownerOnly?: boolean
28-
guildOnly?: boolean
29-
userPermissions?: any[]
30-
botPermissions?: any[]
31-
cooldown?: number
32-
}
33-
34-
export class Command {
35-
public name: string
36-
public description: string
37-
public category: string
38-
public arguments: any[]
39-
public nsfw: boolean
40-
public slash: boolean
41-
public ownerOnly: boolean
42-
public guildOnly: boolean
43-
public userPermissions: any[]
44-
public botPermissions: any[]
45-
public cooldown: number
46-
47-
public execute: CommandExecute<boolean>
48-
49-
constructor(context: CommandContext, options: CommandOptions = {}) {
50-
this.name = options.name ?? context.name
51-
this.description = options.description ?? ''
52-
this.category = options.category ?? context.category
53-
this.arguments = options.arguments ?? []
54-
this.nsfw = options.nsfw ?? false
55-
this.slash = options.slash || context.slash
56-
this.ownerOnly = options.ownerOnly ?? false
57-
this.guildOnly = options.guildOnly ?? false
58-
this.userPermissions = options.userPermissions ?? []
59-
this.botPermissions = options.botPermissions ?? []
60-
this.cooldown = options.cooldown ?? 0
61-
62-
this.execute = context.execute
63-
}
64-
}
65-
66-
export const getCommand = (path: string) => {
67-
const command = jiti(path) as CommandResult<boolean>
68-
const context: CommandContext = {
69-
name: filename(path).split('.')[0],
70-
category: filename(dirname(path)),
71-
slash: filename(path).endsWith('.slash'),
72-
execute: command.execute
3+
import { filename } from 'pathe/utils'
4+
import type {
5+
CommandExecute,
6+
CommandOptions,
7+
HarmonyCommand,
8+
HarmonyCommandInput
9+
} from '../types'
10+
import { Harmony } from './harmony'
11+
12+
export const resolveHarmonyCommand = (
13+
cmd: HarmonyCommandInput,
14+
harmonyOptions: Harmony['options']
15+
): HarmonyCommand<boolean> => {
16+
if (typeof cmd === 'string') {
17+
const _jiti = jiti(harmonyOptions.rootDir, {
18+
interopDefault: true
19+
})
20+
const _cmdPath = _jiti.resolve(cmd)
21+
const command = _jiti(_cmdPath) as HarmonyCommand<boolean>
22+
const options: CommandOptions = {
23+
name: filename(_cmdPath).split('.')[0],
24+
category: filename(dirname(_cmdPath)),
25+
slash: command.options.slash || filename(_cmdPath).endsWith('.slash'),
26+
...command.options
27+
}
28+
29+
return { options, execute: command.execute }
30+
} else {
31+
return cmd
7332
}
74-
75-
return new Command(context, command.options)
7633
}
7734

7835
export const defineCommand = <Slash extends boolean>(
7936
options: CommandOptions & { slash?: Slash },
8037
execute: CommandExecute<Slash>
81-
): CommandResult<Slash> => {
38+
): HarmonyCommand<Slash> => {
8239
return {
8340
options,
8441
execute

src/config.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
import { LoadConfigOptions, loadConfig } from 'c12'
2-
import { defu } from 'defu'
2+
import { resolve } from 'pathe'
33
import type { HarmonyConfig } from '../types'
44

5-
export interface LoadHarmonyConfigOptions
6-
extends LoadConfigOptions<HarmonyConfig> {}
5+
const HarmonyDefaults: HarmonyConfig = {
6+
scanDirs: [],
7+
ignore: []
8+
}
79

8-
export const loadHarmonyConfig = async (opts: LoadHarmonyConfigOptions) => {
10+
export const loadOptions = async (
11+
configOverrides: HarmonyConfig = {},
12+
opts: LoadConfigOptions
13+
) => {
914
const { config } = await loadConfig<HarmonyConfig>({
1015
name: 'harmony',
1116
configFile: 'harmony.config',
1217
rcFile: '.harmonyrc',
1318
dotenv: true,
1419
globalRc: true,
20+
overrides: configOverrides,
21+
defaults: HarmonyDefaults,
1522
...opts
1623
})
1724

18-
return defu(config, {
19-
defaultPrefix: '!',
20-
dir: {
21-
commands: './commands',
22-
events: './events'
23-
}
24-
})
25+
if (!config) {
26+
throw new Error('No configuration found')
27+
}
28+
const options = config
29+
30+
options.rootDir = resolve(options.rootDir || '.')
31+
options.srcDir = resolve(options.srcDir || options.rootDir)
32+
options.scanDirs?.unshift(options.srcDir)
33+
options.scanDirs = options.scanDirs?.map((dir) =>
34+
resolve(options.srcDir!, dir!)
35+
)
36+
options.scanDirs = [...new Set(options.scanDirs)]
37+
options.defaultPrefix = options.defaultPrefix || '!'
38+
39+
return options
2540
}
2641

2742
export const defineHarmonyConfig = (config: HarmonyConfig) => {

src/events.ts

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,39 @@
1-
import createJiti from 'jiti'
1+
import jiti from 'jiti'
22
import { filename } from 'pathe/utils'
33
import type {
44
DefineEvent,
55
DefineEventWithOptions,
66
EventCallback,
7-
EventResult
7+
EventOptions,
8+
HarmonyEvent,
9+
HarmonyEventInput
810
} from '../types'
11+
import { Harmony } from './harmony'
12+
13+
export const resolveHarmonyEvent = (
14+
evt: HarmonyEventInput,
15+
harmonyOptions: Harmony['options']
16+
): HarmonyEvent => {
17+
if (typeof evt === 'string') {
18+
const _jiti = jiti(harmonyOptions.rootDir, {
19+
interopDefault: true
20+
})
21+
const _evtPath = _jiti.resolve(evt)
22+
const event = _jiti(_evtPath) as HarmonyEvent
23+
const options: EventOptions = {
24+
name: filename(_evtPath).split('.')[0],
25+
once: event.options.once || filename(_evtPath).endsWith('.once')
26+
}
927

10-
const jiti = createJiti(undefined as unknown as string, {
11-
interopDefault: true,
12-
requireCache: false,
13-
esmResolve: true,
14-
extensions: ['.ts', '.js']
15-
})
16-
17-
interface EventContext {
18-
name: string
19-
once: boolean
20-
execute: EventCallback
21-
}
22-
23-
interface EventOptions {
24-
once?: boolean
25-
}
26-
27-
export class Event {
28-
public name: string
29-
public once: boolean
30-
31-
public callback: EventCallback
32-
33-
constructor(context: EventContext, options: EventOptions = {}) {
34-
this.name = context.name
35-
this.once = options.once || context.once
36-
37-
this.callback = context.execute
38-
}
39-
}
40-
41-
export const getEvent = (path: string) => {
42-
const event = jiti(path) as EventResult
43-
const context: EventContext = {
44-
name: filename(path).split('.')[0],
45-
once: filename(path).endsWith('.once'),
46-
execute: event.callback
28+
return { options, callback: event.callback }
29+
} else {
30+
return evt
4731
}
48-
49-
return new Event(context, event.options)
5032
}
5133

5234
export const defineEvent: DefineEvent & DefineEventWithOptions = (
5335
...args: [EventOptions | EventCallback, EventCallback?]
54-
): EventResult => {
36+
): HarmonyEvent => {
5537
let options: EventOptions = {}
5638

5739
if (args.length === 1) {

0 commit comments

Comments
 (0)