1 var assert = require("assert")
2 , path = require("path")
3 , mkdir = require("mkdirp")
4 , chownr = require("chownr")
5 , pathIsInside = require("path-is-inside")
6 , readJson = require("read-package-json")
7 , log = require("npmlog")
8 , npm = require("../npm.js")
9 , tar = require("../utils/tar.js")
10 , deprCheck = require("../utils/depr-check.js")
11 , getCacheStat = require("./get-stat.js")
12 , cachedPackageRoot = require("./cached-package-root.js")
13 , addLocalTarball = require("./add-local-tarball.js")
14 , sha = require("sha")
15 , inflight = require("inflight")
17 module.exports = addLocal
19 function addLocal (p, pkgData, cb_) {
20 assert(typeof p === "object", "must have spec info")
21 assert(typeof cb_ === "function", "must have callback")
23 pkgData = pkgData || {}
25 function cb (er, data) {
27 log.error("addLocal", "Could not install %s", p.spec)
30 if (data && !data._fromGithub) {
31 data._from = path.relative(npm.prefix, p.spec) || "."
32 var resolved = path.relative(npm.prefix, p.spec)
33 if (resolved) data._resolved = "file:"+resolved
38 if (p.type === "directory") {
39 addLocalDirectory(p.spec, pkgData, null, cb)
42 addLocalTarball(p.spec, pkgData, null, cb)
46 // At this point, if shasum is set, it's something that we've already
47 // read and checked. Just stashing it in the data at this point.
48 function addLocalDirectory (p, pkgData, shasum, cb) {
49 assert(pkgData, "must pass package data")
50 assert(typeof cb === "function", "must have callback")
52 // if it's a folder, then read the package.json,
53 // tar it to the proper place, and add the cache tar
54 if (pathIsInside(p, npm.cache)) return cb(new Error(
55 "Adding a cache directory to the cache will make the world implode."))
57 readJson(path.join(p, "package.json"), false, function (er, data) {
61 return cb(new Error("No name provided in package.json"))
63 else if (pkgData.name && pkgData.name !== data.name) {
65 "Invalid package: expected " + pkgData.name + " but found " + data.name
70 return cb(new Error("No version provided in package.json"))
72 else if (pkgData.version && pkgData.version !== data.version) {
74 "Invalid package: expected " + pkgData.name + "@" + pkgData.version +
75 " but found " + data.name + "@" + data.version
81 // pack to {cache}/name/ver/package.tgz
82 var root = cachedPackageRoot(data)
83 var tgz = path.resolve(root, "package.tgz")
84 var pj = path.resolve(root, "package/package.json")
86 var wrapped = inflight(tgz, next)
87 if (!wrapped) return log.verbose("addLocalDirectory", tgz, "already in flight; waiting")
88 log.verbose("addLocalDirectory", tgz, "not in flight; packing")
90 getCacheStat(function (er, cs) {
91 mkdir(path.dirname(pj), function (er, made) {
92 if (er) return wrapped(er)
93 var fancy = !pathIsInside(p, npm.tmp)
94 tar.pack(tgz, p, data, fancy, function (er) {
96 log.error("addLocalDirectory", "Could not pack", p, "to", tgz)
100 if (!cs || isNaN(cs.uid) || isNaN(cs.gid)) return wrapped()
102 chownr(made || tgz, cs.uid, cs.gid, function (er) {
103 if (er && er.code === 'ENOENT') return wrapped()
111 if (er) return cb(er)
112 // if we have the shasum already, just add it
114 return addLocalTarball(tgz, data, shasum, cb)
116 sha.get(tgz, function (er, shasum) {
120 data._shasum = shasum
121 return addLocalTarball(tgz, data, shasum, cb)