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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
module.exports = loadPackageDefaults
var path = require("path")
, log = require("./log")
, find = require("./find")
, asyncMap = require("./async-map")
, npm = require("../../npm")
function loadPackageDefaults (pkg, cb) {
asyncMap([pkg], function (pkg, cb) {
log.verbose(pkg._id, "loadDefaults")
readDefaultModules(pkg, cb)
readDefaultBins(pkg, cb)
readDefaultMans(pkg, cb)
}, 3, function (er) { cb(er, pkg) })
}
function readDefaultMans (pkg, cb) {
var man = pkg.directories && pkg.directories.man
, pkgDir = path.join(npm.dir, pkg.name, pkg.version, "package")
, manDir = path.join(pkgDir, man)
if (pkg.man && !Array.isArray(pkg.man)) pkg.man = [pkg.man]
if (pkg.man || !man) return cb(null, pkg)
find(manDir, /\.[0-9]+(\.gz)?$/, function (er, filenames) {
if (er) return cb(er)
pkg.man = filenames.filter(function(_){return _})
.map(function (filename) {
var f = path.basename(filename)
return filename.substr(pkgDir.length + 1)
})
cb(null,pkg)
})
}
// shim ROOT/{name}-{version}/**/*.js to ROOT/.npm/{name}/{version}/{lib}/**/*.js
function readDefaultModules (pkg, cb) {
var lib = pkg.directories && pkg.directories.lib || pkg.lib
, pkgDir = path.join(npm.dir, pkg.name, pkg.version, "package")
, libDir = path.join(pkgDir, lib)
if (pkg.modules || !lib) return cb(null, pkg)
// create a modules hash from the lib folder.
pkg.modules = {}
find(libDir, /\.(js|node)$/, function (er, filenames) {
if (er) return cb(er)
filenames.filter(function(_){return _}).forEach(function (filename) {
filename = path.basename(filename, path.extname(filename))
var key = filename.substr(libDir.length + 1)
, val = filename.substr(pkgDir.length + 1)
if (key.length && val.length) pkg.modules[key] = val
})
return cb(null, pkg)
})
}
function readDefaultBins (pkg, cb) {
var binDir = pkg.directories && pkg.directories.bin
log.verbose(binDir, pkg._id)
if (pkg.bin || !binDir) return cb(null, pkg)
log.verbose("linking default bins", pkg._id)
var pkgDir = path.join(npm.dir, pkg.name, pkg.version, "package")
binDir = path.join(pkgDir, binDir)
pkg.bin = {}
find(binDir, function (er, filenames) {
log.silly(filenames, "default bins")
if (er) return cb(er)
filenames.forEach(function (filename) {
var key = filename.substr(binDir.length + 1)
.replace(/\.(js|node)$/, '')
, val = filename.substr(pkgDir.length + 1)
if (key.length && val.length) pkg.bin[key] = val
})
log.silly(pkg.bin, pkg._id+".bin")
cb(null, pkg)
})
}
|