-
Notifications
You must be signed in to change notification settings - Fork 73
/
update-lockfile.js
65 lines (54 loc) · 1.92 KB
/
update-lockfile.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
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
'use strict'
const exec = require('child_process').execSync
const semver = require('semver')
const flags = {
'dependencies': ' -S',
'devDependencies': ' -D',
'optionalDependencies': ' -O'
}
const yarnFlags = {
'dependencies': '',
'devDependencies': ' -D',
'optionalDependencies': ' -O'
}
const setPrefixYarn = prefix => prefix === '~'
? ' --tilde'
: prefix === ''
? ' --exact'
: ''
module.exports = function updateLockfile (dependency, options) {
if (!options.yarn && semver.lt(exec('npm --version').toString().trim(), '3.0.0')) {
exec('npm shrinkwrap')
} else {
// revert and unstage the changes done by greenkeeper
exec('git revert -n HEAD')
exec('git reset HEAD')
if (options.yarn) {
const flag = yarnFlags[dependency.type]
const prefix = setPrefixYarn(dependency.prefix)
const envArgs = process.env.GK_LOCK_YARN_OPTS ? ` ${process.env.GK_LOCK_YARN_OPTS.trim()}` : ''
const args = `${flag}${prefix}${envArgs} ${dependency.name}@${dependency.version}`
exec(`yarn add${args}`)
}
if (options.npm) {
const flag = flags[dependency.type]
const prefix = dependency.prefix ? ` --save-prefix="${dependency.prefix}"` : ''
const args = `${flag}${prefix} ${dependency.name}@${dependency.version}`
var npmBin = 'npm'
try {
exec('npm5 -v')
npmBin = 'npm5'
} catch (err) {}
exec(`${npmBin} install${args}`)
}
}
if (exec('git status --porcelain').toString() === '') return
// commit the updated lockfile
exec('git add npm-shrinkwrap.json 2>/dev/null || true')
exec('git add package-lock.json 2>/dev/null || true')
exec('git add yarn.lock 2>/dev/null || true')
exec('git config user.email "support@greenkeeper.io"')
exec('git config user.name "greenkeeperio-bot"')
const updateMessage = 'chore(package): update lockfile\n\nhttps://npm.im/greenkeeper-lockfile'
exec(`git commit -m "${updateMessage}"`)
}