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

update-dependents.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7499ae1341e797738a89c0a1348a257bdeba124f (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*

This command is plumbing.

npm update-deps <pkg>

For each other version of pkg, for each dependent in other
version's dependents folder, if the new version would satisfy the
dependency as well, update other version's dependent's dependency
links to point at the new version

If no dependents are left, then remove old version

*/

module.exports = updateDependents

var readInstalled = require("./utils/read-installed")
  , path = require("path")
  , npm = require("../npm")
  , chain = require("./utils/chain")
  , semver = require("./utils/semver")
  , link = require("./utils/link")
  , linkIfExists = link.ifExists
  , shim = require("./utils/write-shim")
  , shimIfExists = shim.ifExists
  , readJson = require("./utils/read-json")
  , log = require("./utils/log")
  , fs = require("./utils/graceful-fs")
  , rm = require("./utils/rm-rf")
  , lifecycle = require("./utils/lifecycle")
  , asyncMap = require("./utils/async-map")

function updateDependents (args, cb) {
  // replace args with the installed data
  if (!args.length) return cb() // nothing to do
  readArgs(args, function (er, args) {
    if (er) return log.er(cb, "Error reading args")(er)

    cb = (function (cb) { return function (er) {
      var a = args.map(function (a) { return a._id })
      log( a, (er) ? "failed to update dependents" : "updated dependents")
      return cb(er)
    }})(cb)
    // now this is an array of package descriptors
    // and each one is installed, and has siblings.
    // update any dependents on any other versions to this one,
    // if it satisfies them, and then remove them if they have
    // no more dependents
    if (!args.length) return log(
      "Nothing to update", "update dependents", cb)
    asyncMap(args, function (arg, cb) {
      lifecycle(arg, "preupdatedependents", function (er) {
        if (er) return cb(er)
        updateDepsTo(arg, function (er, rmList) {
          if (er) return cb(er)
          chain
            ( [lifecycle, arg, "updatedependents"]
            , [lifecycle, arg, "postupdatedependents"]
            , function (er) { cb(er, rmList) }
            )
          })
      })
    }, function (er, rmList) {
      if (er) return cb(er)
      // now they've all been updated, so remove the others.
      log.verbose(rmList.join("\n"), "updateDependents: rm")
      npm.commands.rm(rmList, cb)
    })
  })
}

// update the _others to this one.
function updateDepsTo (arg, cb) {
  asyncMap(arg._others, function (o, cb) {
    updateOtherVersionDeps(o, arg, cb)
  }, cb)
}

function updateOtherVersionDeps (other, pkg, cb) {
  var depdir = path.join( npm.dir
                        , pkg.name
                        , other
                        , "dependents"
                        )
  fs.readdir(depdir, function (er, deps) {
    // if the package didn't have any deps, then this folder
    // would not be created.
    if (er) return log.verbose("no dependents", "update", cb)
    // for each of these, update the dependency on
    // other to pkg
    if (!deps.length) return cb()
    asyncMap(deps, function (d, cb) {
      // todo: make this a @ instead of a -
      d = d.split("-")
      var name = d.shift()
        , ver = d.join("-")
      return updateDepToNew(name, ver, pkg, other, cb)
    }, cb)
  })
}
function updateDepToNew (depName, depVer, pkg, other, cb) {
  var depdir = path.join(npm.dir, depName, depVer)
    , jsonFile = path.join(depdir, "package", "package.json")
  readJson(jsonFile, function (er, data) {
    if (er) return log.er(cb, "failed to read "+jsonFile)(er)
    // check if pkg is ok
    var dependencies = data.dependencies
    if (!dependencies) return log
      ( "Weird, "+depName+"@"+depVer+" doesn't have any dependencies"
      , "wtf?"
      , cb
      )
    if (Array.isArray(dependencies)) {
      var deps = {}
      dependencies.forEach(function (d) { deps[d] = "*" })
      dependencies = deps
    }
    var dependency = data.dependencies[pkg.name]
    if (!dependency) return log.error
      ( "Weird, "+depName+"@"+depVer+" doesn't depend on "+pkg.name
      , "wtf?"
      , cb
      )
    if (!semver.satisfies(pkg.version, dependency)) return log
      ( pkg._id + " doesn't satisfy "+depName+"@"+depVer
      , "not updating"
      , cb
      )

    chain
      ( [ removeDependencyLinks, data, pkg, other ]
      , [ createDependencyLinks, data, pkg ]
      , function (er) { cb(er, pkg.name+"@"+other) }
      )
  })
}

function removeDependencyLinks (dep, pkg, other, cb) {
  var depdir = path.join(npm.dir, dep.name, dep.version)
    , depsOn = path.join(depdir, "dependson", pkg.name+"-"+other)
    , deps = path.join(depdir, "dependencies", pkg.name)
    , dependentLink = path.join( npm.dir
                               , pkg.name
                               , other
                               , "dependents"
                               , dep.name + "-" + dep.version
                               )
  asyncMap([deps+"js", deps, depsOn, dependentLink], rm, cb)
}
function createDependencyLinks (dep, pkg, cb) {
  var depdir = path.join(npm.dir, dep.name, dep.version)
    , depsOn = path.join( depdir
                        , "dependson"
                        , pkg.name+"-"+pkg.version
                        )
    , deps = path.join(depdir, "dependencies", pkg.name)
    , targetRoot = path.join(npm.dir, pkg.name, pkg.version)
    , targetMain = path.join(targetRoot, "main.js")
    , targetLib = path.join(targetRoot, "lib")
    , dependentLink = path.join( npm.dir
                               , pkg.name
                               , pkg.version
                               , "dependents"
                               , dep.name + "-" + dep.version
                               )
  asyncMap
    ( [ [ link, targetRoot, depsOn ]
      , [ link, depdir, dependentLink ]
      , [ linkIfExists, targetLib, deps ]
      , [ shimIfExists, targetMain, deps + ".js" ]
      ]
    , function (c, cb) {
        c.shift().apply(null, c.concat(cb))
      }
    , cb
    )
}

function readArgs (args, cb) {
  var p = args.length
  function r () { if (--p === 0) {
    cb(null, args.filter(function (a) { return a }))
  }}
  function readOthers (arg, i) {
    readInstalled([arg.name], function (er, inst) {
      if (er) {
        args[i] = null
        return log(er, "Error reading installed", r)
      }
      var have = Object.keys(inst[arg.name])
      if (have.length < 2) {
        args[i] = null
        return log(
          "Only one version installed", "update-dependents "+arg.name, r)
      }
      arg._others = have.filter(function (v) { return v !== arg.version })
      r()
    })
  }

  args.forEach(function (arg, i) {
    if (typeof arg === "object") return readOthers(arg, i)
    arg = arg.split(/@/)
    var name = arg.shift()
      , ver = arg.join("@")
      , jsonFile = path.join( npm.dir
                            , name
                            , ver
                            , "package"
                            , "package.json"
                            )
    readJson(jsonFile, function (er, arg) {
      if (er) {
        args[i] = null
        return log(er, "Error reading "+jsonFile, r)
      }
      args[i] = arg
      readOthers(arg, i)
    })
  })
}