2017-10-10 21:41:41 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
const execa = require('execa')
|
|
|
|
const fetch = require('isomorphic-fetch')
|
|
|
|
const fse = require('fs-extra')
|
|
|
|
const registryUrl = require('registry-url')
|
|
|
|
const semver = require('semver')
|
2017-07-21 00:33:14 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const bin = 'node_modules/.bin/'
|
|
|
|
const lernaBin = `${bin}lerna`
|
|
|
|
const getPackages = require('./get-packages')
|
|
|
|
const getReleaseVersion = require('./get-release-version')
|
|
|
|
const revertPackages = require('./revert-packages')
|
2017-07-21 00:33:14 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const PRERELEASE = 'prerelease'
|
|
|
|
const DIST_TAG = 'rc'
|
2017-11-09 20:41:18 +03:00
|
|
|
const PRIMER_CSS = 'primer'
|
2017-10-10 21:41:41 +03:00
|
|
|
const RELEASE_VERSION = getReleaseVersion(
|
|
|
|
PRIMER_CSS,
|
|
|
|
process.env.TRAVIS_BRANCH
|
2017-07-21 00:33:14 +03:00
|
|
|
)
|
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const depFields = [
|
|
|
|
'dependencies',
|
|
|
|
'devDependencies',
|
|
|
|
'optionalDependencies',
|
|
|
|
'peerDependencies',
|
|
|
|
]
|
|
|
|
|
|
|
|
const getUpdated = (args) => {
|
2017-10-25 01:33:35 +03:00
|
|
|
return execa(lernaBin, ['updated', '--json'], { reject: false })
|
|
|
|
.then(res => {
|
|
|
|
if (res.stdout == "") {
|
|
|
|
console.warn("No packages need updating")
|
|
|
|
return []
|
|
|
|
} else {
|
|
|
|
return JSON.parse(res.stdout)
|
|
|
|
}
|
|
|
|
})
|
2017-10-10 21:41:41 +03:00
|
|
|
.then(updated => updated.map(pkg => pkg.name))
|
|
|
|
}
|
2017-07-21 00:33:14 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const writePackage = (pkg) => {
|
|
|
|
const {dir} = pkg
|
|
|
|
delete pkg.dir
|
|
|
|
const json = JSON.stringify(pkg, null, ' ') + '\n'
|
|
|
|
pkg.dir = dir
|
|
|
|
return fse.writeFile(`${pkg.dir}/package.json`, json, 'utf8')
|
|
|
|
.then(() => pkg)
|
|
|
|
}
|
2017-07-21 00:33:14 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const bump = (pkg, by, preid) => {
|
|
|
|
if (pkg.name === PRIMER_CSS) {
|
|
|
|
pkg.version = RELEASE_VERSION
|
|
|
|
}
|
2017-07-21 00:33:14 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const original = pkg.version
|
|
|
|
let version = increment(pkg.version, by, preid)
|
|
|
|
return getPackageInfo(pkg.name)
|
|
|
|
.then(info => {
|
|
|
|
while (version in info.versions) {
|
|
|
|
version = increment(version, by, preid)
|
|
|
|
}
|
|
|
|
console.warn('%s %s -> %s', pkg.name, original, version)
|
|
|
|
pkg.version = version
|
|
|
|
return writePackage(pkg)
|
|
|
|
})
|
|
|
|
}
|
2017-07-21 00:33:14 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const getPackageInfo = (name) => {
|
|
|
|
const url = registryUrl() + name
|
|
|
|
return fetch(url).then(res => res.json())
|
|
|
|
}
|
2017-07-21 00:33:14 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const increment = (version, by, preid) => {
|
|
|
|
const {major, minor, patch} = semver(version)
|
|
|
|
const prev = [major, minor, patch].join('.')
|
|
|
|
const next = semver.inc(version, by, preid)
|
|
|
|
// if this is a prerelease, "revert" major.minor.patch
|
|
|
|
// so that only the prerelease id is incremented
|
|
|
|
return by === PRERELEASE
|
|
|
|
? next.replace(/^\d+\.\d+\.\d+/, prev)
|
|
|
|
: next
|
|
|
|
}
|
2017-07-27 23:26:21 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
const updateDependants = (pkg, pkgs) => {
|
|
|
|
return pkgs.filter(other => {
|
2018-06-19 22:26:03 +03:00
|
|
|
let changed = false
|
|
|
|
depFields.forEach(field => {
|
2017-10-10 21:41:41 +03:00
|
|
|
if (other[field] && (pkg.name in other[field])) {
|
|
|
|
other[field][pkg.name] = pkg.version
|
2018-06-19 22:26:03 +03:00
|
|
|
changed = true
|
2017-10-10 21:41:41 +03:00
|
|
|
}
|
|
|
|
})
|
2018-06-19 22:26:03 +03:00
|
|
|
return changed
|
2017-10-10 21:41:41 +03:00
|
|
|
})
|
|
|
|
}
|
2017-07-27 23:26:21 +03:00
|
|
|
|
2017-10-10 21:41:41 +03:00
|
|
|
revertPackages()
|
|
|
|
.then(() => getPackages())
|
|
|
|
.then(dirs => {
|
|
|
|
return dirs.map(dir => {
|
|
|
|
const pkg = require(`../${dir}/package.json`)
|
|
|
|
pkg.dir = dir
|
|
|
|
return pkg
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(pkgs => {
|
|
|
|
const by = PRERELEASE
|
|
|
|
const preid = DIST_TAG
|
|
|
|
return getUpdated()
|
|
|
|
.then(updated => {
|
|
|
|
console.warn('%d packages updated...', updated.length)
|
|
|
|
return pkgs.filter(pkg => updated.includes(pkg.name))
|
|
|
|
})
|
|
|
|
.then(updated => {
|
2017-10-12 07:20:50 +03:00
|
|
|
const changed = new Set(updated)
|
2017-10-10 21:41:41 +03:00
|
|
|
return Promise.all(updated.map(pkg => {
|
|
|
|
return bump(pkg, by, preid)
|
|
|
|
.then(pkg => updateDependants(pkg, pkgs))
|
|
|
|
.then(dependants => {
|
2017-10-12 07:20:50 +03:00
|
|
|
dependants.forEach(dep => changed.add(dep))
|
2017-10-10 21:41:41 +03:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
.then(() => {
|
|
|
|
const tasks = Array.from(changed)
|
|
|
|
.map(writePackage)
|
|
|
|
return Promise.all(tasks)
|
|
|
|
})
|
|
|
|
.then(updated => {
|
|
|
|
const tasks = updated.map(pkg => {
|
2017-10-12 07:14:23 +03:00
|
|
|
return execa('npm', ['publish', '--tag', DIST_TAG], {
|
2017-10-10 21:41:41 +03:00
|
|
|
cwd: pkg.dir,
|
|
|
|
stdio: 'inherit',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return Promise.all(tasks)
|
|
|
|
})
|
2017-11-10 20:01:00 +03:00
|
|
|
.then(() => {
|
|
|
|
console.warn("📓 Updated CHANGELOG...\n")
|
|
|
|
return execa(`${bin}lerna-changelog`, [], { env: process.env })
|
2017-11-10 20:10:38 +03:00
|
|
|
.then(result => {
|
|
|
|
console.warn(result.stdout)
|
|
|
|
})
|
2017-11-10 20:01:00 +03:00
|
|
|
.catch(error => {
|
|
|
|
console.error('lerna-changelog error:', error)
|
|
|
|
})
|
|
|
|
})
|
2017-10-10 21:41:41 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error('Error:', error)
|
2017-10-12 07:07:27 +03:00
|
|
|
process.exitCode = 1
|
2018-06-29 01:54:13 +03:00
|
|
|
return
|
2017-10-10 21:41:41 +03:00
|
|
|
})
|
2017-10-12 07:07:27 +03:00
|
|
|
.then(() => process.exit())
|