Skip to content

Commit

Permalink
feat: handle monorepos
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This now works with monorepos, it’s a big refactor,
that’s why we make it a new semver-major release when it lands, just
so that existing users won’t get tripped up by little changes.

However, the design goal here is to be as backwards compatible as
possible.
  • Loading branch information
janl committed Apr 11, 2018
1 parent 3e8d2d1 commit a9b3e65
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
4 changes: 3 additions & 1 deletion lib/update-lockfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const yarnFlags = {
'optionalDependencies': ' -O'
}

module.exports = function updateLockfile (dependency, options) {
module.exports.updateLockfile = function updateLockfile (dependency, options) {
if (!options.yarn && semver.lt(exec('npm --version').toString().trim(), '3.0.0')) {
exec('npm shrinkwrap')
} else {
Expand Down Expand Up @@ -44,7 +44,9 @@ module.exports = function updateLockfile (dependency, options) {
exec(`${npmBin} install${args}`)
}
}
}

module.exports.commitLockfile = function commitLockfile () {
const commitEmail = process.env.GK_LOCK_COMMIT_EMAIL ? process.env.GK_LOCK_COMMIT_EMAIL.trim() : '[email protected]'
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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"url": "https://github.com/greenkeeperio/greenkeeper-lockfile/issues"
},
"dependencies": {
"fast-glob": "^2.2.0",
"lodash": "^4.17.4",
"require-relative": "^0.8.7",
"semver": "^5.3.0"
Expand Down
79 changes: 51 additions & 28 deletions update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,19 @@
'use strict'

const fs = require('fs')
const path = require('path')
const process = require('process')

const relative = require('require-relative')
const fg = require('fast-glob')

const config = require('./lib/config')
const extractDependency = require('./lib/extract-dependency')
const updateLockfile = require('./lib/update-lockfile')
const { updateLockfile, commitLockfile } = require('./lib/update-lockfile')
const ci = require('./ci-services')

module.exports = function update () {
const info = ci()
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)) {
return console.error(
'Without either an "npm-shrinkwrap.json", "package-lock.json" or "yarn.lock" file present there is no need to run this script'
)
}

if (!info.correctBuild) {
return console.error('This build should not update the lockfile. It could be a PR, not a branch build.')
}

if (!info.branchName) {
return console.error('No branch details set, so assuming not a Greenkeeper branch')
}

// legacy support
if (config.branchPrefix === 'greenkeeper/' && info.branchName.startsWith('greenkeeper-')) {
Expand All @@ -46,17 +30,56 @@ module.exports = function update () {
return console.error('Only running on first push of a new branch')
}

const dependency = extractDependency(pkg, config.branchPrefix, info.branchName)
if (!info.correctBuild) {
return console.error('This build should not update the lockfile. It could be a PR, not a branch build.')
}

if (!dependency) {
return console.error('No dependency changed')
if (!info.branchName) {
return console.error('No branch details set, so assuming not a Greenkeeper branch')
}

updateLockfile(dependency, {
yarn: yarnLockExists,
npm: packageLockExists || shrinkwrapExists
})
const allPackageFiles = fg.sync('./**/package.json')
const doCommit = allPackageFiles.reduce((didChange, pkgJson) => {
const lockfilePath = path.dirname(pkgJson)
const previousDir = process.cwd()
process.chdir(lockfilePath)

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) {
console.error(`${pkgJson}: No dependency changed`)
process.chdir(previousDir)
return didChange
}

updateLockfile(dependency, {
yarn: yarnLockExists,
npm: packageLockExists || shrinkwrapExists
})
process.chdir(previousDir)
return true
}, false)

if (doCommit) {
commitLockfile()
}

console.log('Lockfile updated')
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'test') {
console.log('Lockfile updated')
}
}
if (require.main === module) module.exports()

0 comments on commit a9b3e65

Please sign in to comment.