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
|
'use strict'
const BB = require('bluebird')
const createShrinkwrap = require('../shrinkwrap.js').createShrinkwrap
const deepSortObject = require('../utils/deep-sort-object.js')
const detectIndent = require('detect-indent')
const fs = BB.promisifyAll(require('graceful-fs'))
const iferr = require('iferr')
const log = require('npmlog')
const moduleName = require('../utils/module-name.js')
const npm = require('../npm.js')
const parseJSON = require('../utils/parse-json.js')
const path = require('path')
const validate = require('aproba')
const without = require('lodash.without')
const writeFileAtomic = require('write-file-atomic')
// if the -S|--save option is specified, then write installed packages
// as dependencies to a package.json file.
exports.saveRequested = function (tree, andReturn) {
validate('OF', arguments)
savePackageJson(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)
createShrinkwrap(tree, {silent: false}, next)
}
function savePackageJson (tree, next) {
validate('OF', 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, 'utf8', iferr(next, function (packagejson) {
const indent = detectIndent(packagejson).indent || ' '
try {
tree.package = parseJSON(packagejson)
} catch (ex) {
return next(ex)
}
// If we're saving bundled deps, normalize the key before we start
if (saveBundle) {
var bundle = tree.package.bundleDependencies || tree.package.bundledDependencies
delete tree.package.bundledDependencies
if (!Array.isArray(bundle)) bundle = []
}
var toSave = getThingsToSave(tree)
var toRemove = getThingsToRemove(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 (!tree.package[save]) tree.package[save] = {}
})
log.verbose('saving', toSave)
const types = ['dependencies', 'devDependencies', 'optionalDependencies']
toSave.forEach(function (pkg) {
tree.package[pkg.save][pkg.name] = pkg.spec
const movedFrom = []
for (let saveType of types) {
if (
pkg.save !== saveType &&
tree.package[saveType] &&
tree.package[saveType][pkg.name]
) {
movedFrom.push(saveType)
delete tree.package[saveType][pkg.name]
}
}
if (movedFrom.length) {
log.notice('save', `${pkg.name} is being moved from ${movedFrom.join(' and ')} to ${pkg.save}`)
}
if (saveBundle) {
var ii = bundle.indexOf(pkg.name)
if (ii === -1) bundle.push(pkg.name)
}
})
toRemove.forEach(function (pkg) {
delete tree.package[pkg.save][pkg.name]
if (saveBundle) {
bundle = without(bundle, pkg.name)
}
})
Object.keys(savingTo).forEach(function (key) {
tree.package[key] = deepSortObject(tree.package[key])
})
if (saveBundle) {
tree.package.bundleDependencies = deepSortObject(bundle)
}
var json = JSON.stringify(tree.package, null, indent) + '\n'
writeFileAtomic(saveTarget, json, next)
}))
}
exports.getSaveType = function (tree, arg) {
if (arguments.length) validate('OO', arguments)
var globalInstall = npm.config.get('global')
var noSaveFlags = !npm.config.get('save') &&
!npm.config.get('save-dev') &&
!npm.config.get('save-prod') &&
!npm.config.get('save-optional')
if (globalInstall || noSaveFlags) return null
if (npm.config.get('save-optional')) {
return 'optionalDependencies'
} else if (npm.config.get('save-dev')) {
return 'devDependencies'
} else if (npm.config.get('save-prod')) {
return 'dependencies'
} else {
if (arg) {
var name = moduleName(arg)
if (tree.package.optionalDependencies[name]) {
return 'optionalDependencies'
} else if (tree.package.devDependencies[name]) {
return 'devDependencies'
}
}
return 'dependencies'
}
}
function getThingsToSave (tree) {
validate('O', arguments)
var toSave = tree.children.filter(function (child) {
return child.save
}).map(function (child) {
return {
name: moduleName(child),
spec: child.saveSpec,
save: child.save
}
})
return toSave
}
function getThingsToRemove (tree) {
validate('O', arguments)
if (!tree.removedChildren) return []
var toRemove = tree.removedChildren.map(function (child) {
return {
name: moduleName(child),
save: child.save
}
})
return toRemove
}
|