Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
fix(pkglock): switch to default fs when resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 6, 2019
1 parent 83e7070 commit fa05062
Showing 1 changed file with 49 additions and 43 deletions.
92 changes: 49 additions & 43 deletions lib/pkglock.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,67 @@ const pkgLockName = 'package-lock.json'
const pkgLockCache = new Map()

const envNoPkgLock = process.env.TINK_NO_PKG_LOCK
const isPkgLockDisabled = () => !process.tink || process.tink.noPkgLock || envNoPkgLock
let inResolve = false
const isPkgLockDisabled = () => inResolve || !process.tink || process.tink.noPkgLock || envNoPkgLock

module.exports.resolve = resolve
module.exports._clearCache = () => pkgLockCache.clear()

function resolve (...p) {
if (isPkgLockDisabled()) { return null }
const resolved = path.resolve(...p)
if (resolved.match(path.dirname(__dirname)) || resolved.match(process.tink.cache)) {
// Don't be a smartass about our own sources and cache...
return null
}
const result = readPkgLock(resolved)
if (!result) { return result }
let { pkgLock, subPath } = result
if (!pkgLock) { return false }
let pkgName, filePath
let scope = pkgLock
while (subPath) {
if (subPath.startsWith('/node_modules')) {
subPath = subPath.substr('/node_modules'.length)
if (!subPath) {
try {
inResolve = true
const resolved = path.resolve(...p)
if (resolved.match(path.dirname(__dirname)) || resolved.match(process.tink.cache)) {
// Don't be a smartass about our own sources and cache...
return null
}
const result = readPkgLock(resolved)
if (!result) { return result }
let { pkgLock, subPath } = result
if (!pkgLock) { return false }
let pkgName, filePath
let scope = pkgLock
while (subPath) {
if (subPath.startsWith('/node_modules')) {
subPath = subPath.substr('/node_modules'.length)
if (!subPath) {
return {
cache: process.tink.cache,
scope,
dir: scope.dependencies,
resolvedPath: resolved,
isDir: true,
isFile: false
}
}
}
[, pkgName, subPath, filePath] = subPath.match(/^[/\\]((?:@[^/\\]+[/\\])?[^/\\]+)([/\\]?(.*))/)
let res = resolveEntity(process.tink.cache, scope, pkgName, filePath)
if (res) {
const pkg = scope.dependencies[pkgName]
pkg.name = pkgName
return {
cache: process.tink.cache,
scope,
dir: scope.dependencies,
pkg,
hash: res.hash,
dir: res.dir,
resolvedPath: resolved,
isDir: true,
isFile: false
isDir: res.isDir,
isFile: res.isFile
}
} else if (scope && scope.dependencies && scope.dependencies[pkgName]) {
const { dependencies: { [pkgName]: newScope } } = scope
scope = newScope
} else {
// ENOENT
return false
}
}
[, pkgName, subPath, filePath] = subPath.match(/^[/\\]((?:@[^/\\]+[/\\])?[^/\\]+)([/\\]?(.*))/)
let res = resolveEntity(process.tink.cache, scope, pkgName, filePath)
if (res) {
const pkg = scope.dependencies[pkgName]
pkg.name = pkgName
return {
cache: process.tink.cache,
pkg,
hash: res.hash,
dir: res.dir,
resolvedPath: resolved,
isDir: res.isDir,
isFile: res.isFile
}
} else if (scope && scope.dependencies && scope.dependencies[pkgName]) {
const { dependencies: { [pkgName]: newScope } } = scope
scope = newScope
} else {
// ENOENT
return false
}
return false
} finally {
inResolve = false
}
return false
}

module.exports.depKey = depKey
Expand Down Expand Up @@ -144,7 +150,7 @@ function readPkgLock (resolved) {
} else {
const p = path.toNamespacedPath(pkgLockPath)
try {
pkgLock = JSON.parse((fs.readFileSync.orig || fs.readFileSync)(p))
pkgLock = JSON.parse(fs.readFileSync(p))
pkgLockCache.set(pkgLockPath, pkgLock)
} catch (e) {
if (e.code !== 'ENOENT') {
Expand Down

0 comments on commit fa05062

Please sign in to comment.