@@ -49,21 +49,17 @@ import type { Harmonix, HarmonixConfig, HarmonixOptions } from './types'
4949import { version } from '../package.json'
5050
5151export const ctx = getContext < Harmonix > ( 'harmonix' )
52-
5352export const useHarmonix = ctx . use
5453
55- export const createHarmonix = async (
56- config : HarmonixConfig = { } ,
57- opts : LoadConfigOptions = { }
58- ) => {
59- if ( ! process . env . DISCORD_CLIENT_TOKEN ) {
60- createError (
61- 'Client token is required. Please provide it in the environment variable DISCORD_CLIENT_TOKEN.'
62- )
63- }
64- const options = await loadOptions ( config , opts )
65- const harmonix : Harmonix = {
66- options : options as HarmonixOptions ,
54+ const initHarmonix = async (
55+ config : HarmonixConfig ,
56+ options : LoadConfigOptions
57+ ) : Promise < Harmonix > => {
58+ const opts = await loadOptions ( config , options )
59+
60+ return {
61+ configFile : opts . configFile as string ,
62+ options : opts . options as HarmonixOptions ,
6763 events : new Collection ( ) ,
6864 commands : new Collection ( ) ,
6965 contextMenus : new Collection ( ) ,
@@ -72,36 +68,23 @@ export const createHarmonix = async (
7268 selectMenus : new Collection ( ) ,
7369 preconditions : new Collection ( )
7470 }
75-
76- consola . log ( colors . blue ( `Harmonix ${ colors . bold ( version ) } \n` ) )
77- await loadHarmonix ( harmonix , config , opts )
78-
79- return harmonix
8071}
8172
82- export const createDevHarmonix = async (
83- config : HarmonixConfig = { } ,
84- opts : LoadConfigOptions = { }
73+ const watchReload = (
74+ harmonix : Harmonix ,
75+ config : HarmonixConfig ,
76+ opts : LoadConfigOptions
8577) => {
86- if ( ! process . env . DISCORD_CLIENT_TOKEN ) {
87- createError (
88- 'Client token is required. Please provide it in the environment variable DISCORD_CLIENT_TOKEN.'
89- )
90- }
91- const options = await loadOptions ( config , opts )
92- const harmonix : Harmonix = {
93- options : options as HarmonixOptions ,
94- events : new Collection ( ) ,
95- commands : new Collection ( ) ,
96- contextMenus : new Collection ( ) ,
97- buttons : new Collection ( ) ,
98- modals : new Collection ( ) ,
99- selectMenus : new Collection ( ) ,
100- preconditions : new Collection ( )
101- }
102-
103- consola . log ( colors . blue ( `Harmonix ${ colors . bold ( version ) } \n` ) )
104- const watcher = watch ( harmonix . options . scanDirs , {
78+ const filesToWatch = [
79+ harmonix . options . dirs . commands ,
80+ harmonix . options . dirs . events ,
81+ harmonix . options . dirs . contextMenus ,
82+ harmonix . options . dirs . buttons ,
83+ harmonix . options . dirs . modals ,
84+ harmonix . options . dirs . selectMenus ,
85+ harmonix . options . dirs . preconditions
86+ ] . map ( ( file ) => resolve ( harmonix . options . rootDir , file ) )
87+ const watcher = watch ( [ ...filesToWatch , harmonix . configFile ] , {
10588 ignored : harmonix . options . ignore ,
10689 ignoreInitial : true
10790 } )
@@ -122,8 +105,26 @@ export const createDevHarmonix = async (
122105 100
123106 )
124107
125- await loadHarmonix ( harmonix , config , opts )
126108 watcher . on ( 'all' , ( event , path , stats ) => reload ( event , path , stats ) )
109+ }
110+
111+ export const createHarmonix = async (
112+ config : HarmonixConfig = { } ,
113+ opts : LoadConfigOptions = { } ,
114+ devMode : boolean = false
115+ ) => {
116+ if ( ! process . env . DISCORD_CLIENT_TOKEN ) {
117+ createError (
118+ 'Client token is required. Please provide it in the environment variable DISCORD_CLIENT_TOKEN.'
119+ )
120+ }
121+ const harmonix = await initHarmonix ( config , opts )
122+
123+ consola . log ( colors . blue ( `Harmonix ${ colors . bold ( version ) } \n` ) )
124+ await loadHarmonix ( harmonix , config , opts )
125+ if ( devMode ) {
126+ watchReload ( harmonix , config , opts )
127+ }
127128
128129 return harmonix
129130}
@@ -142,53 +143,54 @@ export const clearHarmonix = async (harmonix: Harmonix) => {
142143export const loadHarmonix = async (
143144 harmonix : Harmonix ,
144145 config : HarmonixConfig ,
145- opts : LoadConfigOptions
146+ options : LoadConfigOptions
146147) => {
147- const options = await loadOptions ( config , opts )
148-
149- harmonix . options = options as HarmonixOptions
150- const scannedCommands = await scanCommands ( harmonix )
151- const _commands = [ ...( harmonix . options . commands || [ ] ) , ...scannedCommands ]
152- const commands = _commands . map ( ( cmd ) => resolveCommand ( cmd , harmonix . options ) )
153-
154- const scannedEvents = await scanEvents ( harmonix )
155- const _events = [ ...( harmonix . options . events || [ ] ) , ...scannedEvents ]
156- const events = _events . map ( ( evt ) => resolveEvent ( evt , harmonix . options ) )
157-
158- const scannedContextMenus = await scanContextMenus ( harmonix )
159- const _contextMenus = [
148+ const opts = await loadOptions ( config , options )
149+
150+ harmonix . configFile = opts . configFile as string
151+ harmonix . options = opts . options as HarmonixOptions
152+ const [
153+ scannedEvents ,
154+ scannedCommands ,
155+ scannedContextMenus ,
156+ scannedButtons ,
157+ scannedModals ,
158+ scannedSelectMenus ,
159+ scannedPreconditions
160+ ] = await Promise . all ( [
161+ scanEvents ( harmonix ) ,
162+ scanCommands ( harmonix ) ,
163+ scanContextMenus ( harmonix ) ,
164+ scanButtons ( harmonix ) ,
165+ scanModals ( harmonix ) ,
166+ scanSelectMenus ( harmonix ) ,
167+ scanPreconditions ( harmonix )
168+ ] )
169+ const commands = [
170+ ...( harmonix . options . commands || [ ] ) ,
171+ ...scannedCommands
172+ ] . map ( ( cmd ) => resolveCommand ( cmd , harmonix . options ) )
173+ const events = [ ...( harmonix . options . events || [ ] ) , ...scannedEvents ] . map (
174+ ( evt ) => resolveEvent ( evt , harmonix . options )
175+ )
176+ const contextMenus = [
160177 ...( harmonix . options . contextMenus || [ ] ) ,
161178 ...scannedContextMenus
162- ]
163- const contextMenus = _contextMenus . map ( ( ctm ) =>
164- resolveContextMenu ( ctm , harmonix . options )
179+ ] . map ( ( ctm ) => resolveContextMenu ( ctm , harmonix . options ) )
180+ const buttons = [ ... ( harmonix . options . buttons || [ ] ) , ... scannedButtons ] . map (
181+ ( btn ) => resolveButton ( btn , harmonix . options )
165182 )
166-
167- const scannedButtons = await scanButtons ( harmonix )
168- const _buttons = [ ...( harmonix . options . buttons || [ ] ) , ...scannedButtons ]
169- const buttons = _buttons . map ( ( btn ) => resolveButton ( btn , harmonix . options ) )
170-
171- const scannedModals = await scanModals ( harmonix )
172- const _modals = [ ...( harmonix . options . modals || [ ] ) , ...scannedModals ]
173- const modals = _modals . map ( ( mdl ) => resolveModal ( mdl , harmonix . options ) )
174-
175- const scannedSelectMenus = await scanSelectMenus ( harmonix )
176- const _selectMenus = [
183+ const modals = [ ...( harmonix . options . modals || [ ] ) , ...scannedModals ] . map (
184+ ( mdl ) => resolveModal ( mdl , harmonix . options )
185+ )
186+ const selectMenus = [
177187 ...( harmonix . options . selectMenus || [ ] ) ,
178188 ...scannedSelectMenus
179- ]
180- const selectMenus = _selectMenus . map ( ( slm ) =>
181- resolveSelectMenu ( slm , harmonix . options )
182- )
183-
184- const scannedPreconditions = await scanPreconditions ( harmonix )
185- const _preconditions = [
189+ ] . map ( ( slm ) => resolveSelectMenu ( slm , harmonix . options ) )
190+ const preconditions = [
186191 ...( harmonix . options . preconditions || [ ] ) ,
187192 ...scannedPreconditions
188- ]
189- const preconditions = _preconditions . map ( ( prc ) =>
190- resolvePrecondition ( prc , harmonix . options )
191- )
193+ ] . map ( ( prc ) => resolvePrecondition ( prc , harmonix . options ) )
192194
193195 loadEvents ( harmonix , events )
194196 loadCommands ( harmonix , commands )
0 commit comments