Skip to content

Commit

Permalink
Clean up release code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
panzarino committed Oct 28, 2020
1 parent bc48963 commit fdca3aa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
25 changes: 14 additions & 11 deletions scripts/check-halt.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
/* eslint-disable no-console */
const execa = require('execa')

const { getCurrentBranch, readPackageJson } = require('./utils')
const { getChangedPackagesAndDependents, getLernaPackages } = require('./changed-packages')
const { readPackageJson } = require('./npm-release')

const runTests = () => {
const runTestsAndExit = () => {
process.exit(0)
}

const skipTests = () => {
const skipTestsAndExit = () => {
process.exit(1)
}

const main = async (ciJob) => {
if (!ciJob) {
console.log(`Could not get current CI job`)
skipTests()

return skipTestsAndExit()
}

const { stdout: currentBranch } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'])
const currentBranch = await getCurrentBranch()

if (currentBranch === 'develop' || currentBranch === 'master') {
console.log(`Currently on ${currentBranch} - all tests run`)
runTests()

return runTestsAndExit()
}

const packages = await getLernaPackages()
Expand All @@ -43,7 +43,8 @@ const main = async (ciJob) => {

if (Object.keys(changed).includes(pack)) {
console.log(`${pack} was directly changed, so tests run.`)
runTests()

return runTestsAndExit()
}

const dependenciesChanged = []
Expand All @@ -56,11 +57,13 @@ const main = async (ciJob) => {

if (dependenciesChanged.length) {
console.log(`${pack} is listed as a dependant of ${dependenciesChanged.join(', ')}, so tests run.`)
runTests()

return runTestsAndExit()
}

console.log(`${pack} is unchanged and not dependent on any changed packages, so tests do not run.`)
skipTests()

return skipTestsAndExit()
}

main(process.env.CIRCLE_JOB)
41 changes: 17 additions & 24 deletions scripts/npm-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
const execa = require('execa')
const fs = require('fs')
const path = require('path')
const semverRcompare = require('semver/functions/rcompare')
const semverSortNewestFirst = require('semver/functions/rcompare')

const { getCurrentBranch, getPackagePath, readPackageJson, minutes } = require('./utils')
const { getLernaPackages, getPackageDependents } = require('./changed-packages')
const { minutes, waitForJobToPass } = require('./wait-on-circle-jobs')
const { waitForJobToPass } = require('./wait-on-circle-jobs')

const error = (message) => {
if (require.main === module) {
Expand All @@ -18,22 +19,12 @@ const error = (message) => {
}
}

const getCurrentBranch = async () => {
const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'])

return stdout
}

const getTags = async () => {
const { stdout } = await execa('git', ['tag', '--merged', await getCurrentBranch()])

return stdout.split('\n')
}

const getPackagePath = ({ location }) => path.join(location, 'package.json')

const readPackageJson = (pack) => JSON.parse(fs.readFileSync(getPackagePath(pack)))

const getBinaryVersion = async () => {
const { stdout: root } = await execa('git', ['rev-parse', '--show-toplevel'])
const rootPath = path.join(root, 'package.json')
Expand Down Expand Up @@ -68,9 +59,9 @@ const getCurrentVersion = async (name) => {
const versions = tags
.map((tag) => (tag.match(new RegExp(`^${name}-v(.+)`)) || [])[1])
.filter((tag) => tag)
.sort(semverRcompare)
.sort(semverSortNewestFirst)

return versions[0] || null
return versions[0]
}

const getPackageVersions = async (packages) => {
Expand All @@ -93,7 +84,7 @@ const getPackageVersions = async (packages) => {
const currentVersion = await getCurrentVersion(pack)

if (!currentVersion) {
error(`Couldn't find a current version for ${pack}`)
return error(`Couldn't find a current version for ${pack}`)
}

console.log(`Current version: ${currentVersion}`)
Expand Down Expand Up @@ -125,19 +116,21 @@ const injectVersions = (packagesToRelease, versions, packages) => {
const info = packages.find((p) => p.name === name)
const packageJson = readPackageJson(info)

for (const dependency in packageJson.dependencies) {
if (packageJson.dependencies[dependency] === '*') {
const version = versions[dependency].nextVersion || versions[dependency].currentVersion
if (packageJson.dependencies) {
for (const dependency in packageJson.dependencies) {
if (packageJson.dependencies[dependency] === '*') {
const version = versions[dependency].nextVersion || versions[dependency].currentVersion

if (version) {
packageJson.dependencies[dependency] = version
if (version) {
packageJson.dependencies[dependency] = version

console.log(`\t${dependency}: ${version}`)
console.log(`\t${dependency}: ${version}`)
}
}
}
}

fs.writeFileSync(getPackagePath(info), JSON.stringify(packageJson, null, 2))
fs.writeFileSync(getPackagePath(info), JSON.stringify(packageJson, null, 2))
}
}
}

Expand Down Expand Up @@ -189,7 +182,7 @@ const releasePackages = async (packages) => {
// goes through the release process for all of our independent npm projects
const main = async () => {
if (!process.env.CIRCLECI) {
error(`Cannot run release process outside of Circle CI`)
return error(`Cannot run release process outside of Circle CI`)
}

if (process.env.CIRCLE_PULL_REQUEST) {
Expand Down
19 changes: 19 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const la = require('lazy-ass')
const is = require('check-more-types')
const path = require('path')
const fs = require('fs')
const execa = require('execa')

/* eslint-disable no-console */

Expand Down Expand Up @@ -113,10 +114,28 @@ const getCIBuildUrl = () => {
}
}

const seconds = (s) => s * 1000
const minutes = (m) => m * 60 * 1000

const getCurrentBranch = async () => {
const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'])

return stdout
}

const getPackagePath = ({ location }) => path.join(location, 'package.json')

const readPackageJson = (pack) => JSON.parse(fs.readFileSync(getPackagePath(pack)))

module.exports = {
getNameAndBinary,
getJustVersion,
getShortCommit,
getCIName,
getCIBuildUrl,
getCurrentBranch,
getPackagePath,
readPackageJson,
seconds,
minutes,
}
5 changes: 2 additions & 3 deletions scripts/wait-on-circle-jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ const got = require('got')
// always print the debug logs
const debug = require('debug')('*')

const { seconds, minutes } = require('./utils')

// we expect CircleCI to set the current polling job name
const jobName = process.env.CIRCLE_JOB || 'wait-on-circle-jobs'

const workflowId = process.env.CIRCLE_WORKFLOW_ID

const getAuth = () => `${process.env.CIRCLE_TOKEN}:`

const seconds = (s) => s * 1000
const minutes = (m) => m * 60 * 1000

const verifyCI = () => {
if (!process.env.CIRCLE_TOKEN) {
console.error('Cannot find CIRCLE_TOKEN')
Expand Down

0 comments on commit fdca3aa

Please sign in to comment.