Search code examples
typescriptglob

Glob isn't finding files properly


I have a Typescript library I'm writing and it uses a glob to find files in a given dir.

Here is the load function

public async loadEvents(): Promise<any> {
        console.log(process.cwd());
        const a = Glob.sync(`${this.eventDir}**/*.js`);
        console.log(a);
        for (const b of a) {
            const c = await import(b)
                .then( (event: Event) => {
                    this.Events.set(event.name!, event);
                    console.log(`[LOAD EVENTS] [LOADED] : ${event.name}, ${event}`);
                })
                .catch( (err) => {
                    console.log(`[LOAD EVENTS] [ERROR] : `, err);
                });
        }
    }

When I run the function I see: []

Here is tree output

C:.
│   config.js
│   index.js
│   tsconfig.tsbuildinfo
│
├───commands
│   ├───admin
│   │       reload.js
│   │       restart.js
│   │
│   └───misc
│           test.js
│
└───events
    ├───discord
    │       message.js
    │
    └───self
            message.js

I have an identical function to load from commands

    public async loadCommands(): Promise<any> {
        const a = Glob.sync(`${this.commandsDir}**/*.js`);
        console.log(a);
        for (const b of a) {
            const c = await import(b)
                .then( (command: Command) => {
                    this.Commands.set(command.cmdName, command);
                    console.log(`[LOAD EVENTS] [LOADED] : ${command.cmdName}, ${command}`);
                })
                .catch( (err) => {
                    console.log(`[LOAD COMMANDS] [ERROR] : `, err);
                });
        }
    }

and that returns an array of the files within the commands folder. I'm not understanding why the events function is not showing the files but the commands function does.

eventsDir = './events/';

commandsDir = './commands/';

output is

PS C:\Users\alexa\Documents\GitHub\bubo_re\build> node .
C:\Users\alexa\Documents\GitHub\bubo_re\build
[]

Solution

  • The current working directory was the issue. By adding

    this.eventsDir = Path.resolve(this.eventsDir);
    this.commandsDir = Path.resolve(this.commandsDir);
    

    The loader now works as intended.