-
Notifications
You must be signed in to change notification settings - Fork 72
/
update.js
executable file
·116 lines (91 loc) · 3.43 KB
/
update.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
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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const process = require('process')
const exec = require('child_process').execSync
const relative = require('require-relative')
const fg = require('fast-glob')
const config = require('./lib/config')
const getIgnores = require('./lib/ignores')
const extractDependency = require('./lib/extract-dependency')
const hasLockfileCommit = require('./lib/git-helpers').hasLockfileCommit
const lockfile = require('./lib/update-lockfile')
const updateLockfile = lockfile.updateLockfile
const stageLockfile = lockfile.stageLockfile
const commitLockfiles = lockfile.commitLockfiles
const ci = require('./ci-services')
module.exports = function update () {
const info = ci()
// legacy support
if (config.branchPrefix === 'greenkeeper/' && info.branchName.startsWith('greenkeeper-')) {
config.branchPrefix = 'greenkeeper-'
}
if (!info.branchName) {
return console.error('No branch details set, so assuming not a Greenkeeper branch')
}
if (!info.branchName.startsWith(config.branchPrefix)) {
return console.error(`'${info.branchName}' is not a Greenkeeper branch`)
}
if (info.branchName === `${config.branchPrefix}initial`) {
// This should be possible to do, Contributions are welcome!
return console.error(`'${info.branchName}' is the initial Greenkeeper branch, please update the lockfile manualy`)
}
if (!info.correctBuild) {
return console.error('This build should not update the lockfile. It could be a PR, not a branch build.')
}
if (hasLockfileCommit(info)) {
return console.error('greenkeeper-lockfile already has a commit on this branch')
}
const ignores = getIgnores()
const allPackageFiles = fg.sync('./**/package.json', {ignore: ignores})
// make sure that we have a clean working tree
exec('git stash')
// exec('git revert -n HEAD')
// exec('git reset HEAD')
const doCommit = allPackageFiles.reduce((didChange, pkgJson) => {
const lockfilePath = path.dirname(pkgJson)
const previousDir = process.cwd()
try {
process.chdir(lockfilePath)
} catch (error) {
console.error(`can't chdir into lockfile path ${lockfilePath}`)
return
}
const pkg = relative('./package.json')
const shrinkwrapExists = fs.existsSync('./npm-shrinkwrap.json')
const packageLockExists = fs.existsSync('./package-lock.json')
const yarnLockExists = fs.existsSync('./yarn.lock')
if (!(shrinkwrapExists || packageLockExists || yarnLockExists)) {
console.info(
`${pkgJson}: Without either an "npm-shrinkwrap.json", "package-lock.json" or "yarn.lock" file present there is no need to run this script`
)
process.chdir(previousDir)
return didChange
}
const dependency = extractDependency(pkg, config.branchPrefix, info.branchName)
if (!dependency || dependency.length === 0) {
console.error(`${pkgJson}: No dependency changed`)
process.chdir(previousDir)
return didChange
}
dependency.forEach(dep => {
updateLockfile(dep, {
yarn: yarnLockExists,
npm: packageLockExists || shrinkwrapExists
})
})
stageLockfile({
ignoreOutput: info.ignoreOutput
})
process.chdir(previousDir)
return true
}, false)
if (doCommit) {
commitLockfiles()
}
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'test') {
console.log('Lockfiles updated')
}
}
if (require.main === module) module.exports()