Skip to content

Commit

Permalink
fix: invalid id for cts, mts, cjs, mjs files, node paths (#318)
Browse files Browse the repository at this point in the history
* better error messages

* fix: invalid id for cts, mts, cjs, mjs files
  • Loading branch information
jacoobes authored Aug 6, 2023
1 parent 52d6368 commit a7f5ea2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ dist
# Yarn files
.yarn/install-state.gz
.yarn/build-state.yml

.yalc

yalc.lock
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "3.0.1",
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.",
"main": "./dist/index.js",
"module": "./dist/cjs/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
Expand Down
20 changes: 7 additions & 13 deletions src/core/module-loading.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Result } from 'ts-results-es';
import { type Observable, from, mergeMap, ObservableInput } from 'rxjs';
import { readdir, stat } from 'fs/promises';
import { basename, extname, join, resolve } from 'path';
import { basename, extname, join, resolve, parse } from 'path';
import assert from 'assert';
import { createRequire } from 'node:module';
import type { ImportPayload, Wrapper } from '../types/core';
Expand All @@ -25,27 +25,21 @@ export type ModuleResult<T> = Promise<ImportPayload<T>>;
export async function importModule<T>(absPath: string) {
let module = await import(absPath).then(esm => esm.default);

assert(
module,
'Found no default export for command module at ' +
absPath +
'Forgot to ignore with "!"? (!filename.ts)?',
);
assert(module, `Found no export for module at ${absPath}. Forgot to ignore with "!"? (!${basename(absPath)})?`);
if ('default' in module) {
module = module.default;
}
return Result.wrap(() => module.getInstance()).unwrapOr(module) as T;
return Result
.wrap(() => module.getInstance())
.unwrapOr(module) as T;
}
export async function defaultModuleLoader<T extends Module>(absPath: string): ModuleResult<T> {
let module = await importModule<T>(absPath);
assert.ok(
module,
"Found an undefined module. Forgot to ignore it with a '!' ie (!filename.ts)?",
);
assert(module, `Found an undefined module: ${absPath}`);
return { module, absPath };
}

export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
export const fmtFileName = (fileName: string) => parse(fileName).name;

/**
* a directory string is converted into a stream of modules.
Expand Down
23 changes: 23 additions & 0 deletions test/core/module-loading.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, it, expect } from 'vitest'
import { faker } from '@faker-js/faker'
import * as Files from '../../src/core/module-loading'
describe('module-loading', () => {
it('should properly extract filename from file, nested once', () => {
const extension = faker.system.fileExt()
const name = faker.system.fileName({ extensionCount: 0 })
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)
//
// })
})

0 comments on commit a7f5ea2

Please sign in to comment.