Skip to content

Commit

Permalink
fix: use require.cache to resolve root
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Apr 30, 2024
1 parent 82a8830 commit 40387ad
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ function traverseFilePathUntil(filename: string, predicate: (filename: string) =
return current
}

// Update to path.dirname(url.fileURLToPath(import.meta.url)) whenever we migrate to ESM
/* eslint-disable unicorn/prefer-module */
loadConfig.root = traverseFilePathUntil(
require.main?.path ?? module.path,
(p) => !(p.includes('node_modules') || p.includes('.pnpm') || p.includes('.yarn')),
)
loadConfig.root =
process.env.OCLIF_TEST_ROOT ??
Object.values(require.cache).find((m) => m?.children.includes(module))?.filename ??
traverseFilePathUntil(
require.main?.path ?? module.path,
(p) => !(p.includes('node_modules') || p.includes('.pnpm') || p.includes('.yarn')),
)
/* eslint-enable unicorn/prefer-module */

// Using a named export to import fancy causes this issue: https://github.com/oclif/test/issues/516
Expand Down

0 comments on commit 40387ad

Please sign in to comment.