summaryrefslogtreecommitdiff
path: root/deps/npm/lib/install/save.js
blob: acbe8c5bb3579f3b8962cb53391183faabf153cf (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
'use strict'
var fs = require('graceful-fs')
var path = require('path')
var url = require('url')
var writeFileAtomic = require('write-file-atomic')
var log = require('npmlog')
var semver = require('semver')
var iferr = require('iferr')
var validate = require('aproba')
var without = require('lodash.without')
var npm = require('../npm.js')
var deepSortObject = require('../utils/deep-sort-object.js')
var parseJSON = require('../utils/parse-json.js')
var moduleName = require('../utils/module-name.js')

// if the -S|--save option is specified, then write installed packages
// as dependencies to a package.json file.

exports.saveRequested = function (args, tree, andReturn) {
  validate('AOF', arguments)
  savePackageJson(args, tree, andWarnErrors(andSaveShrinkwrap(tree, andReturn)))
}

function andSaveShrinkwrap (tree, andReturn) {
  validate('OF', arguments)
  return function (er) {
    validate('E', arguments)
    saveShrinkwrap(tree, andWarnErrors(andReturn))
  }
}

function andWarnErrors (cb) {
  validate('F', arguments)
  return function (er) {
    if (er) log.warn('saveError', er.message)
    arguments[0] = null
    cb.apply(null, arguments)
  }
}

function saveShrinkwrap (tree, next) {
  validate('OF', arguments)
  var saveTarget = path.resolve(tree.path, 'npm-shrinkwrap.json')
  fs.stat(saveTarget, function (er, stat) {
    if (er) return next()
    var save = npm.config.get('save')
    var saveDev = npm.config.get('save-dev')
    var saveOptional = npm.config.get('save-optional')

    var shrinkwrap = tree.package._shrinkwrap || {dependencies: {}}
    var hasDevOnlyDeps = tree.requires.filter(function (dep) {
      var devReqs = dep.package._requiredBy.filter(function (name) { return name.substr(0, 4) === '#DEV' })
      return devReqs.length === dep.package._requiredBy.length
    }).some(function (dep) {
      return shrinkwrap.dependencies[dep.package.name] != null
    })

    if (!saveOptional && saveDev && !hasDevOnlyDeps) return next()
    if (saveOptional || !save) return next()

    if (hasDevOnlyDeps) {
      var dev = npm.config.get('dev')
      npm.config.set('dev', true)
      npm.commands.shrinkwrap([], true, function () {
        npm.config.set('dev', dev)
        next.apply(this, arguments)
      })
    } else {
      npm.commands.shrinkwrap([], true, next)
    }
  })
}

function savePackageJson (args, tree, next) {
  validate('AOF', arguments)
  var saveBundle = npm.config.get('save-bundle')

  // each item in the tree is a top-level thing that should be saved
  // to the package.json file.
  // The relevant tree shape is { <folder>: {what:<pkg>} }
  var saveTarget = path.resolve(tree.path, 'package.json')
  // don't use readJson, because we don't want to do all the other
  // tricky npm-specific stuff that's in there.
  fs.readFile(saveTarget, iferr(next, function (packagejson) {
    try {
      packagejson = parseJSON(packagejson)
    } catch (ex) {
      return next(ex)
    }

    // If we're saving bundled deps, normalize the key before we start
    if (saveBundle) {
      var bundle = packagejson.bundleDependencies || packagejson.bundledDependencies
      delete packagejson.bundledDependencies
      if (!Array.isArray(bundle)) bundle = []
    }

    var toSave = getThingsToSave(tree)
    var toRemove = getThingsToRemove(args, tree)
    var savingTo = {}
    toSave.forEach(function (pkg) { savingTo[pkg.save] = true })
    toRemove.forEach(function (pkg) { savingTo[pkg.save] = true })

    Object.keys(savingTo).forEach(function (save) {
      if (!packagejson[save]) packagejson[save] = {}
    })

    log.verbose('saving', toSave)
    toSave.forEach(function (pkg) {
      packagejson[pkg.save][pkg.name] = pkg.spec
      if (saveBundle) {
        var ii = bundle.indexOf(pkg.name)
        if (ii === -1) bundle.push(pkg.name)
      }
    })

    toRemove.forEach(function (pkg) {
      delete packagejson[pkg.save][pkg.name]
      if (saveBundle) {
        bundle = without(bundle, pkg.name)
      }
    })

    Object.keys(savingTo).forEach(function (key) {
      packagejson[key] = deepSortObject(packagejson[key])
    })
    if (saveBundle) {
      packagejson.bundledDependencies = deepSortObject(bundle)
    }

    var json = JSON.stringify(packagejson, null, 2) + '\n'
    writeFileAtomic(saveTarget, json, next)
  }))
}

var getSaveType = exports.getSaveType = function (args) {
  validate('A', arguments)
  var nothingToSave = !args.length
  var globalInstall = npm.config.get('global')
  var noSaveFlags = !npm.config.get('save') &&
                    !npm.config.get('save-dev') &&
                    !npm.config.get('save-optional')
  if (nothingToSave || globalInstall || noSaveFlags) return null

  if (npm.config.get('save-optional')) return 'optionalDependencies'
  else if (npm.config.get('save-dev')) return 'devDependencies'
  else return 'dependencies'
}

function computeVersionSpec (child) {
  validate('O', arguments)
  var requested = child.package._requested
  if (!requested || requested.type === 'tag') {
    requested = {
      type: 'version',
      spec: child.package.version
    }
  }
  if (requested.type === 'version' || requested.type === 'range') {
    var version = child.package.version
    var rangeDescriptor = ''
    if (semver.valid(version, true) &&
        semver.gte(version, '0.1.0', true) &&
        !npm.config.get('save-exact')) {
      rangeDescriptor = npm.config.get('save-prefix')
    }
    return rangeDescriptor + version
  } else if (requested.type === 'directory' || requested.type === 'local') {
    var relativePath = path.relative(child.parent.path, requested.spec)
    if (/^[.][.]/.test(relativePath)) {
      return url.format({
        protocol: 'file',
        slashes: true,
        pathname: requested.spec
      })
    } else {
      return url.format({
        protocol: 'file',
        slashes: false,
        pathname: relativePath
      })
    }
  } else if (requested.type === 'hosted') {
    return requested.spec
  } else {
    return requested.rawSpec
  }
}

function getThingsToSave (tree) {
  validate('O', arguments)
  var toSave = tree.children.filter(function (child) {
    return child.save
  }).map(function (child) {
    return {
      name: moduleName(child),
      spec: computeVersionSpec(child),
      save: child.save
    }
  })
  return toSave
}

function getThingsToRemove (args, tree) {
  validate('AO', arguments)
  if (!tree.removed) return []
  var toRemove = tree.removed.map(function (child) {
    return {
      name: moduleName(child),
      save: child.save
    }
  })
  var saveType = getSaveType(args)
  args.forEach(function (arg) {
    toRemove.push({
      name: arg,
      save: saveType
    })
  })
  return toRemove
}