From 5708dd9cb3a82ec57e3e6f49a94a2b43ede8c6e3 Mon Sep 17 00:00:00 2001 From: Khiet Tam Nguyen Date: Sat, 21 Oct 2023 15:47:24 +1100 Subject: [PATCH] Refactoring files.ts to reduce nesting --- src/files.ts | 48 ++++++++++++++++++++++++++++++------------------ src/jewire.ts | 7 +++---- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/files.ts b/src/files.ts index dfc44b9..4796259 100644 --- a/src/files.ts +++ b/src/files.ts @@ -13,36 +13,35 @@ import { Statement } from 'meriyah/dist/src/estree'; * * @returns {string} absolute path or an empty string if no caller */ -export const getCallerFilePath = (): string => { +export const getCallerDirname = (): string => { const orig = Error.prepareStackTrace; Error.prepareStackTrace = (_, stack) => stack; const err = new Error(); - Error.captureStackTrace(err, getCallerFilePath); + Error.captureStackTrace(err, getCallerDirname); const stack = err.stack as any; Error.prepareStackTrace = orig; - return stack[1].getFileName(); + const callerFilePath = stack[1].getFileName(); + /* istanbul ignore next */ + return path.dirname( + callerFilePath.startsWith('file://') + ? callerFilePath.substring(7) + : callerFilePath + ); }; /** * Find the module file path by checking for available extensions * as defined by VALID_FILE_EXTENSIONS * - * @param {string} modulePath - The path to the module - * @param {string} basePath - The base path for the module - * @returns {string} The resolved file path - * @throws {Error} If the file is not found + * @param {string} filePath The absolute path to the file + * @returns {string} The resolved file path with appended extension + * @throws {Error} If the file path does not match any valid extensions */ -export const findModuleFile = (modulePath: string, basePath: string): string => { - const filePath = path.join(basePath, modulePath); - if (fs.existsSync(filePath)) { - return filePath; - } - if (!path.extname(filePath)) { - for (const ext of VALID_FILE_EXTENSIONS) { - const extFilePath = path.join(basePath, `${modulePath}${ext}`); - if (fs.existsSync(extFilePath)) { - return extFilePath; - } +const findFileWithExtensions = (filePath: string): string => { + for (const ext of VALID_FILE_EXTENSIONS) { + const extFilePath = `${filePath}${ext}`; + if (fs.existsSync(extFilePath)) { + return extFilePath; } } throw new Error( @@ -50,6 +49,19 @@ export const findModuleFile = (modulePath: string, basePath: string): string => ); }; +/** + * Find the module file path + * + * @param {string} modulePath - The path to the module + * @param {string} basePath - The base path for the module + * @returns {string} The resolved file path + * @throws {Error} If the file is not found + */ +export const findModuleFile = (basePath: string, modulePath: string): string => { + const filePath = path.join(basePath, modulePath); + return fs.existsSync(filePath) ? filePath : findFileWithExtensions(filePath); +}; + /** * Appends the name of the node given to the appropriate symbol * diff --git a/src/jewire.ts b/src/jewire.ts index 3c64f6a..5a1b76e 100644 --- a/src/jewire.ts +++ b/src/jewire.ts @@ -1,8 +1,7 @@ -import path from 'path'; import rewire from 'rewire'; import entityClone from './clone'; import { Options } from './types'; -import { findModuleFile, getCallerFilePath, getModuleHiddenExports } from './files'; +import { findModuleFile, getCallerDirname, getModuleHiddenExports } from './files'; /** * Leverages rewire to extract hidden exports from a JavaScript module, but @@ -16,8 +15,8 @@ import { findModuleFile, getCallerFilePath, getModuleHiddenExports } from './fil */ const jewire = (relativePath: string, options: Options = {}): Record => { const filePath = findModuleFile( - relativePath, - options.basePath ?? path.dirname(getCallerFilePath()) + options.basePath ?? getCallerDirname(), + relativePath ); const hiddenExportInfo = getModuleHiddenExports(filePath); const hiddenExports = Object.values(hiddenExportInfo.symbols).flat();