Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,29 @@ function notaShim (path, er) {
return er
}

function isDir (path, er) {
er.code = 'EISDIR'
er.message = "'" + path + "' is a directory"
return er
}

var readCmdShim = module.exports = function (path, cb) {
var er = new Error()
Error.captureStackTrace(er, readCmdShim)
fs.readFile(path, function (readFileEr, contents) {
if (readFileEr) return cb(wrapError(readFileEr, er))
var destination = extractPath(path, contents.toString())
if (destination) return cb(null, destination)
return cb(notaShim(path, er))
if (readFileEr) {
// fs.readFile will return EINVAL rather than EISDIR
// if path is a directory.
if (readFileEr.code !== 'EINVAL') return cb(wrapError(readFileEr, er))
fs.stat(path, function (statEr, stats) {
if (!statEr && stats.isDirectory()) return cb(isDir(path, er))
return cb(wrapError(readFileEr, er))
})
} else {
var destination = extractPath(path, contents.toString())
if (destination) return cb(null, destination)
return cb(notaShim(path, er))
}
})
}

Expand Down