Welcome to mirror list, hosted at ThFree Co, Russian Federation.

add-local.js « cache « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dbe55ddc276d55ffdfa85ce2bd8e4ffd5d22ba6c (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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var assert = require("assert")
  , path = require("path")
  , mkdir = require("mkdirp")
  , chownr = require("chownr")
  , pathIsInside = require("path-is-inside")
  , readJson = require("read-package-json")
  , log = require("npmlog")
  , npm = require("../npm.js")
  , tar = require("../utils/tar.js")
  , deprCheck = require("../utils/depr-check.js")
  , getCacheStat = require("./get-stat.js")
  , cachedPackageRoot = require("./cached-package-root.js")
  , addLocalTarball = require("./add-local-tarball.js")
  , sha = require("sha")
  , inflight = require("inflight")
  , lifecycle = require("../utils/lifecycle.js")
  , iferr = require("iferr")

module.exports = addLocal

function addLocal (p, pkgData, cb_) {
  assert(typeof p === "object", "must have spec info")
  assert(typeof cb === "function", "must have callback")

  pkgData = pkgData || {}

  function cb (er, data) {
    if (er) {
      log.error("addLocal", "Could not install %s", p.spec)
      return cb_(er)
    }
    if (data && !data._fromHosted) {
      data._from = path.relative(npm.prefix, p.spec) || "."
      var resolved = path.relative(npm.prefix, p.spec)
      if (resolved) data._resolved = "file:"+resolved
    }
    return cb_(er, data)
  }

  if (p.type === "directory") {
    addLocalDirectory(p.spec, pkgData, null, cb)
  }
  else {
    addLocalTarball(p.spec, pkgData, null, cb)
  }
}

// At this point, if shasum is set, it's something that we've already
// read and checked.  Just stashing it in the data at this point.
function addLocalDirectory (p, pkgData, shasum, cb) {
  assert(pkgData, "must pass package data")
  assert(typeof cb === "function", "must have callback")

  // if it's a folder, then read the package.json,
  // tar it to the proper place, and add the cache tar
  if (pathIsInside(p, npm.cache)) return cb(new Error(
    "Adding a cache directory to the cache will make the world implode."))

  readJson(path.join(p, "package.json"), false, function (er, data) {
    if (er) return cb(er)

    if (!data.name) {
      return cb(new Error("No name provided in package.json"))
    }
    else if (pkgData.name && pkgData.name !== data.name) {
      return cb(new Error(
        "Invalid package: expected " + pkgData.name + " but found " + data.name
      ))
    }

    if (!data.version) {
      return cb(new Error("No version provided in package.json"))
    }
    else if (pkgData.version && pkgData.version !== data.version) {
      return cb(new Error(
        "Invalid package: expected " + pkgData.name + "@" + pkgData.version +
          " but found " + data.name + "@" + data.version
      ))
    }

    deprCheck(data)

    // pack to {cache}/name/ver/package.tgz
    var root = cachedPackageRoot(data)
    var tgz = path.resolve(root, "package.tgz")
    var pj = path.resolve(root, "package/package.json")

    var wrapped = inflight(tgz, next)
    if (!wrapped) return log.verbose("addLocalDirectory", tgz, "already in flight; waiting")
    log.verbose("addLocalDirectory", tgz, "not in flight; packing")

    getCacheStat(function (er, cs) {
      mkdir(path.dirname(pj), function (er, made) {
        if (er) return cb(er)
        var doPrePublish = !pathIsInside(p, npm.tmp)
        if (doPrePublish) {
          lifecycle(data, "prepublish", p, iferr(cb, thenPack))
        }
        else {
          thenPack()
        }
        function thenPack () {
          tar.pack(tgz, p, data, function (er) {
            if (er) {
              log.error("addLocalDirectory", "Could not pack", p, "to", tgz)
              return cb(er)
            }

            if (!cs || isNaN(cs.uid) || isNaN(cs.gid)) wrapped()

            chownr(made || tgz, cs.uid, cs.gid, wrapped)
          })
        }
      })
    })

    function next (er) {
      if (er) return cb(er)
      // if we have the shasum already, just add it
      if (shasum) {
        return addLocalTarball(tgz, data, shasum, cb)
      } else {
        sha.get(tgz, function (er, shasum) {
          if (er) {
            return cb(er)
          }
          data._shasum = shasum
          return addLocalTarball(tgz, data, shasum, cb)
        })
      }
    }
  })
}