blob: 261ad663077f74ac8d9e563520098b1e7c78d874 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
"use strict"
var fs = require("fs")
var path = require("path")
var dz = require("dezalgo")
var npa = require("npm-package-arg")
module.exports = function (spec, where, cb) {
if (where instanceof Function) { cb = where; where = null }
if (where == null) where = "."
cb = dz(cb)
try {
var dep = npa(spec)
}
catch (e) {
return cb(e)
}
var specpath = dep.type == "local"
? path.resolve(where, dep.spec)
: path.resolve(dep.rawSpec? dep.rawSpec: dep.name)
fs.stat(specpath, function (er, s) {
if (er) return finalize()
if (!s.isDirectory()) return finalize("local")
fs.stat(path.join(specpath, "package.json"), function (er) {
finalize(er ? null : "directory")
})
})
function finalize(type) {
if (type != null && type != dep.type) {
dep.type = type
if (! dep.rawSpec) {
dep.rawSpec = dep.name
dep.name = null
}
}
if (dep.type == "local" || dep.type == "directory") dep.spec = specpath
cb(null, dep)
}
}
|