|
| 1 | +const proc = require('child_process'); |
| 2 | +const read = cmd => proc.execSync(cmd, { encoding: 'utf8' }).trim(); |
| 3 | +const run = cmd => { |
| 4 | + proc.execSync(cmd, { stdio: 'inherit' }); |
| 5 | +}; |
| 6 | +const tryRead = cmd => { |
| 7 | + try { |
| 8 | + return read(cmd); |
| 9 | + } catch { |
| 10 | + return undefined; |
| 11 | + } |
| 12 | +}; |
| 13 | + |
| 14 | +// The community contracts currently don't have a defined release process. |
| 15 | +// The master branch is used for development, and the docs are updated from there. |
| 16 | +// Use /^release-v(?<version>(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?)$/ for a release branch |
| 17 | +const masterBranch = /^master$/; |
| 18 | + |
| 19 | +const currentBranch = read('git rev-parse --abbrev-ref HEAD'); |
| 20 | +const match = currentBranch.match(masterBranch); |
| 21 | + |
| 22 | +if (!match) { |
| 23 | + console.error('Not currently on master branch'); |
| 24 | + process.exit(1); |
| 25 | +} |
| 26 | + |
| 27 | +const pkgVersion = require('../package.json').version; |
| 28 | + |
| 29 | +if (pkgVersion.includes('-') && !pkgVersion.includes('.0.0-')) { |
| 30 | + console.error('Refusing to update docs: non-major prerelease detected'); |
| 31 | + process.exit(0); |
| 32 | +} |
| 33 | + |
| 34 | +const current = match.groups; |
| 35 | +const docsBranch = `docs-v${current?.major ?? pkgVersion.split('.')[0]}.x`; |
| 36 | + |
| 37 | +// Fetch remotes and find the docs branch if it exists |
| 38 | +run('git fetch --all --no-tags'); |
| 39 | +const matchingDocsBranches = tryRead(`git rev-parse --glob='*/${docsBranch}'`); |
| 40 | + |
| 41 | +if (!matchingDocsBranches) { |
| 42 | + // Create the branch |
| 43 | + run(`git checkout --orphan ${docsBranch}`); |
| 44 | +} else { |
| 45 | + const [publishedRef, ...others] = new Set(matchingDocsBranches.split('\n')); |
| 46 | + if (others.length > 0) { |
| 47 | + console.error( |
| 48 | + `Found conflicting ${docsBranch} branches.\n` + |
| 49 | + 'Either local branch is outdated or there are multiple matching remote branches.', |
| 50 | + ); |
| 51 | + process.exit(1); |
| 52 | + } |
| 53 | + const publishedVersion = JSON.parse(read(`git show ${publishedRef}:package.json`)).version; |
| 54 | + const publishedMinor = publishedVersion.match(/\d+\.(?<minor>\d+)\.\d+/).groups.minor; |
| 55 | + if (current.minor < publishedMinor) { |
| 56 | + console.error('Refusing to update docs: newer version is published'); |
| 57 | + process.exit(0); |
| 58 | + } |
| 59 | + |
| 60 | + run('git checkout --quiet --detach'); |
| 61 | + run(`git reset --soft ${publishedRef}`); |
| 62 | + run(`git checkout ${docsBranch}`); |
| 63 | +} |
| 64 | + |
| 65 | +run('npm run prepare-docs'); |
| 66 | +run('git add -f docs'); // --force needed because generated docs files are gitignored |
| 67 | +run('git commit -m "Update docs"'); |
| 68 | +run(`git checkout ${currentBranch}`); |
0 commit comments