-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsaver.js
32 lines (31 loc) · 894 Bytes
/
saver.js
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
const fs = require('fs')
const path = require('path')
const npmrc = require('../utils/npmrc')
module.exports = (pkgs, save) => {
const pkgJSON = require(path.join(process.cwd(), 'package.json'))
var saveDeps = {}
pkgs.forEach((pkg) => {
const key = pkg.split('@')[0]
var value = pkg.split('@')[1]
const dep = global.dependenciesTree[pkg]
if (!value) {
switch (dep.type) {
case 'registry':
value = npmrc['save-prefix'] + dep.version
break
default:
value = dep.url
break
}
}
saveDeps[key] = value
})
const field = save === 'dev' ? 'devDependencies' : 'dependencies'
const oldDeps = pkgJSON[field] || {}
const newDeps = Object.assign({}, oldDeps, saveDeps)
pkgJSON[field] = newDeps
fs.writeFileSync(
path.join(process.cwd(), 'package.json'),
JSON.stringify(pkgJSON, 2, 2)
)
}