-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in lettertwo/update-self-published (pull request #74)
Add script to update self-published versions Approved-by: Agnieszka Gawrys Approved-by: Gora Kong Approved-by: Will Binns-Smith Approved-by: Brian Do
- Loading branch information
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
# Generated with husky-init and https://github.com/typicode/husky-4-to-6 | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
# ATLASSIAN: Update self-published (nightly) versions as part of a merge. | ||
# Ideally, this would be done as part of a `pre-merge-commit` hook, | ||
# but git does not support modifying the index during a merge commit. | ||
# So, we hack it in this `post-merge` hook by immediately ammending the commit. | ||
# Note that in cases where the merge commit did not automatically succeed, | ||
# this hook will not be run. See comments in `pre-commit` hook for more. | ||
yarn update-self-published -a | ||
if [[ -f .git/MERGE_HEAD ]]; then | ||
rm .git/MERGE_HEAD | ||
fi | ||
git log -n 1 --pretty=tformat:%s%n%n%b | git commit -F - --amend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#!/usr/bin/env node | ||
|
||
/* eslint-disable no-console */ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const exec = require('child_process').execSync; | ||
|
||
const UPSTREAM = /github.+parcel-bundler\/parcel/; | ||
const NIGHTLY = /.*-nightly\..*/; | ||
|
||
if (process.argv.includes('-h') || process.argv.includes('--help')) { | ||
console.log( | ||
[ | ||
` Usage: ${path.basename(process.argv[1])} [opts]`, | ||
'', | ||
' Options:', | ||
' -h, --help Show help', | ||
' -a Add updated files to git index', | ||
' (handy in a precommit hook)', | ||
'', | ||
' Looks for self-published packages (e.g., @parcel/transformer-js),', | ||
' and compares their nightly version numbers to the list', | ||
' of published nightly versions (via `yarn info`).', | ||
' If it finds a version that is newer, it updates the version', | ||
' in the package.json.', | ||
'', | ||
' It will use the oldest nightly version that is newer than', | ||
' the latest common commit between HEAD and the upstream default branch.', | ||
].join('\n'), | ||
); | ||
process.exit(); | ||
} | ||
|
||
const shouldStage = process.argv.includes('-a'); | ||
|
||
const fromVersionString = str => str.match(/npm:.+@(.+)$/)[1]; | ||
const toVersionString = (name, version) => `npm:${name}@${version}`; | ||
|
||
function run(cmd) { | ||
let result = exec(cmd, {stdio: [0, 'pipe', 2]}); | ||
try { | ||
return JSON.parse(result); | ||
} catch (e) { | ||
if (e instanceof SyntaxError) { | ||
return result.toString().trim(); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
function getUpstreamRemoteName() { | ||
for (let name of run(`git remote`).split(/\s+/)) { | ||
if (UPSTREAM.test(run(`git remote get-url ${name}`))) { | ||
return name; | ||
} | ||
} | ||
throw new Error('Could not determine an upstream remote name!'); | ||
} | ||
|
||
function getDefaultBranchName(remote) { | ||
return run(`git rev-parse --abbrev-ref ${remote}`).split(`${remote}/`).pop(); | ||
} | ||
|
||
/** | ||
* Check nightly version timestamps for the given `pkgName` | ||
* in reverse chronological order, returning the version | ||
* with the timestamp closest to `time` without being younger. | ||
*/ | ||
function getNearestNightlyVersion(pkgName, time) { | ||
let candidate = null; | ||
let info = run(`yarn info ${pkgName} --json`); | ||
let versions = [...Object.entries(info.data.time)].reverse(); | ||
for (let [version, timestamp] of versions) { | ||
if (NIGHTLY.test(version)) { | ||
let versionTime = new Date(timestamp); | ||
if (versionTime < time) break; | ||
candidate = version; | ||
} | ||
} | ||
return candidate; | ||
} | ||
|
||
console.log(`Updating self-published (nightly) versions...`); | ||
|
||
let packages = run( | ||
`${path.join(__dirname, '..', 'node_modules', '.bin', 'lerna')} ls --json`, | ||
); | ||
|
||
// Fetch the default upstream branch... | ||
let upstream = getUpstreamRemoteName(); | ||
let branch = getDefaultBranchName(upstream); | ||
run(`git fetch -q ${upstream} ${branch}`); | ||
// ...so we can determine the latest common ancestor commit. | ||
let baseRef = run(`git merge-base HEAD ${upstream}/${branch}`).split(/\s+/)[0]; | ||
// Get the commit time of the latest common ancestor between HEAD and upstream. | ||
let baseRefTime = new Date(run(`git show -s --format=%cI ${baseRef}`)); | ||
|
||
for (let {location, name} of packages) { | ||
let pkgPath = path.join(location, 'package.json'); | ||
let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); | ||
if (pkg.dependencies && 'self-published' in pkg.dependencies) { | ||
let current = fromVersionString(pkg.dependencies['self-published']); | ||
let nightly = getNearestNightlyVersion(name, baseRefTime); | ||
if (nightly && current !== nightly) { | ||
console.log(`updating ${name} to nearest nightly ${nightly}`); | ||
pkg.dependencies['self-published'] = toVersionString(name, nightly); | ||
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); | ||
if (shouldStage) run(`git add -u ${pkgPath}`); | ||
} | ||
} | ||
} |