From a7f5ea269fb344e221d10dbdc26a1611ffc8138f Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 6 Aug 2023 10:43:34 -0500 Subject: [PATCH] fix: invalid id for cts, mts, cjs, mjs files, node paths (#318) * better error messages * fix: invalid id for cts, mts, cjs, mjs files --- .gitignore | 4 ++++ package.json | 2 +- src/core/module-loading.ts | 20 +++++++------------- test/core/module-loading.test.ts | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 test/core/module-loading.test.ts diff --git a/.gitignore b/.gitignore index 54e89a1a..b6a7e19a 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,7 @@ dist # Yarn files .yarn/install-state.gz .yarn/build-state.yml + +.yalc + +yalc.lock diff --git a/package.json b/package.json index 22aaa07c..6fb839f4 100644 --- a/package.json +++ b/package.json @@ -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": { ".": { diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index a84ff69a..0298c7b1 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -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'; @@ -25,27 +25,21 @@ export type ModuleResult = Promise>; export async function importModule(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(absPath: string): ModuleResult { let module = await importModule(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. diff --git a/test/core/module-loading.test.ts b/test/core/module-loading.test.ts new file mode 100644 index 00000000..6e19d743 --- /dev/null +++ b/test/core/module-loading.test.ts @@ -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) +// +// }) +})