Skip to content

Commit

Permalink
Refactoring files.ts to reduce nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
nktnet committed Oct 21, 2023
1 parent f967114 commit 5708dd9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
48 changes: 30 additions & 18 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,55 @@ 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(
`No such file '${filePath}' with matching extensions [${VALID_FILE_EXTENSIONS}]`
);
};

/**
* 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
*
Expand Down
7 changes: 3 additions & 4 deletions src/jewire.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,8 +15,8 @@ import { findModuleFile, getCallerFilePath, getModuleHiddenExports } from './fil
*/
const jewire = (relativePath: string, options: Options = {}): Record<string, any> => {
const filePath = findModuleFile(
relativePath,
options.basePath ?? path.dirname(getCallerFilePath())
options.basePath ?? getCallerDirname(),
relativePath
);
const hiddenExportInfo = getModuleHiddenExports(filePath);
const hiddenExports = Object.values(hiddenExportInfo.symbols).flat();
Expand Down

0 comments on commit 5708dd9

Please sign in to comment.