-
Notifications
You must be signed in to change notification settings - Fork 72
/
update-lockfile.js
76 lines (66 loc) · 2.71 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
66
67
68
69
70
71
72
73
74
75
76
'use strict'
const exec = require('child_process').execSync
const _ = require('lodash')
const semver = require('semver')
const flags = {
'dependencies': ' -S',
'devDependencies': ' -D',
'optionalDependencies': ' -O'
}
const yarnFlags = {
'dependencies': '',
'devDependencies': ' -D',
'optionalDependencies': ' -O'
}
module.exports.updateLockfile = function updateLockfile (dependency, options) {
if (!options.yarn && semver.lt(exec('npm --version').toString().trim(), '3.0.0')) {
exec('npm shrinkwrap')
} else {
if (options.yarn) {
const flag = yarnFlags[dependency.type]
const envArgs = process.env.GK_LOCK_YARN_OPTS ? ` ${process.env.GK_LOCK_YARN_OPTS.trim()}` : ''
const args = `${flag}${envArgs} '${dependency.name}@${dependency.range}'`
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}`)
}
}
}
module.exports.stageLockfile = function stageLockfile (options) {
// make sure that we have changes to add
if (exec('git status --porcelain').toString() === '') return
// stage the updated lockfile
const ignoreOutput = (options !== undefined && options.ignoreOutput !== undefined) ? options.ignoreOutput : '2>/dev/null || true'
exec(`git add npm-shrinkwrap.json ${ignoreOutput}`)
exec(`git add package-lock.json ${ignoreOutput}`)
exec(`git add yarn.lock ${ignoreOutput}`)
}
module.exports.commitLockfiles = function commitLockfiles () {
const commitEmail = process.env.GK_LOCK_COMMIT_EMAIL ? process.env.GK_LOCK_COMMIT_EMAIL.trim() : 'support@greenkeeper.io'
const commitName = process.env.GK_LOCK_COMMIT_NAME ? process.env.GK_LOCK_COMMIT_NAME.trim() : 'greenkeeperio-bot'
const shouldAmend = !_.includes([undefined, `0`, 'false', 'null', 'undefined'], process.env.GK_LOCK_COMMIT_AMEND)
exec(`git config user.email "${commitEmail}"`)
exec(`git config user.name "${commitName}"`)
if (shouldAmend) {
exec(`git commit --amend --author="${commitName} <${commitEmail}>" --no-edit`)
} else {
let lockfileWording
// either say "lockfile" or "lockfiles" depending on how many files are changed
if (exec('git status --porcelain').toString().split('\n').length > 2) {
lockfileWording = 'lockfiles'
} else {
lockfileWording = 'lockfile'
}
const updateMessage = `chore(package): update ${lockfileWording}\n\nhttps://npm.im/greenkeeper-lockfile`
exec(`git commit -m "${updateMessage}"`)
}
}