Skip to content

Commit

Permalink
fix: always ensure isDir for filename
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 19, 2020
1 parent 14dd2e7 commit 6b343a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createHash } from 'crypto'
import mkdirp from 'mkdirp'
import createRequire from 'create-require'
import resolve from 'resolve'
import { isDir } from './utils'
import { TransformOptions } from './types'

export type JITIOptions = {
Expand All @@ -25,9 +26,14 @@ function md5 (content: string, len = 8) {
return createHash('md5').update(content).digest('hex').substr(0, len)
}

export default function createJITI (_filename: string = process.cwd() + '/index.js', opts: JITIOptions = {}): NodeRequire {
export default function createJITI (_filename: string = process.cwd(), opts: JITIOptions = {}): NodeRequire {
opts = { ...defaults, ...opts }

// If filename is dir, createRequire goes with parent directory, so we need fakepath
if (isDir(_filename)) {
_filename = join(_filename, 'index.js')
}

if (opts.cache && !opts.cacheDir) {
const nodeModulesDir = join(process.cwd(), 'node_modules')
if (existsSync(nodeModulesDir)) {
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { lstatSync } from 'fs'

export function isDir (filename: string): boolean {
try {
const stat = lstatSync(filename)
return stat.isDirectory()
} catch (e) {
// lstatSync throws an error if path doesn't exist
return false
}
}

0 comments on commit 6b343a4

Please sign in to comment.