2023-04-25 11:01:25 +00:00
|
|
|
import { Auto } from '@auto-it/core'
|
2023-05-15 13:17:19 +00:00
|
|
|
import fetch from 'cross-fetch'
|
2024-02-29 17:55:36 +00:00
|
|
|
import glob from 'glob'
|
2024-02-29 12:21:11 +00:00
|
|
|
import minimist from 'minimist'
|
2023-05-15 13:17:19 +00:00
|
|
|
import { assert } from 'node:console'
|
2024-02-29 12:21:11 +00:00
|
|
|
import { SemVer, parse } from 'semver'
|
2023-05-09 13:25:56 +00:00
|
|
|
import { exec } from './lib/exec'
|
2023-06-01 18:01:49 +00:00
|
|
|
import { nicelog } from './lib/nicelog'
|
2023-04-25 11:01:25 +00:00
|
|
|
import { getLatestVersion, publish, setAllVersions } from './lib/publishing'
|
2023-06-26 15:46:59 +00:00
|
|
|
import { getAllWorkspacePackages } from './lib/workspace'
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-02-29 12:21:11 +00:00
|
|
|
type ReleaseType =
|
|
|
|
| {
|
|
|
|
bump: 'major' | 'minor'
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
bump: 'override'
|
|
|
|
version: SemVer
|
|
|
|
}
|
|
|
|
|
|
|
|
function getReleaseType(): ReleaseType {
|
|
|
|
const arg = minimist(process.argv.slice(2))['bump']
|
|
|
|
if (!arg) {
|
|
|
|
throw new Error('Must provide a --bump argument')
|
|
|
|
}
|
|
|
|
if (arg === 'major' || arg === 'minor') {
|
|
|
|
return { bump: arg }
|
|
|
|
}
|
|
|
|
const parsed = parse(arg)
|
|
|
|
if (parsed) {
|
|
|
|
return { bump: 'override', version: parsed }
|
2023-04-25 11:01:25 +00:00
|
|
|
}
|
2024-02-29 12:21:11 +00:00
|
|
|
throw new Error('Invalid bump argument ' + JSON.stringify(arg))
|
|
|
|
}
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-02-29 16:28:11 +00:00
|
|
|
async function getNextVersion(releaseType: ReleaseType): Promise<string> {
|
2024-02-29 12:21:11 +00:00
|
|
|
if (releaseType.bump === 'override') {
|
|
|
|
return releaseType.version.format()
|
2023-05-09 13:25:56 +00:00
|
|
|
}
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-02-29 16:28:11 +00:00
|
|
|
const latestVersion = parse(await getLatestVersion())!
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2023-06-01 18:01:49 +00:00
|
|
|
nicelog('latestVersion', latestVersion)
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2023-05-09 13:25:56 +00:00
|
|
|
const [prereleaseTag, prereleaseNumber] = latestVersion.prerelease
|
|
|
|
if (prereleaseTag && typeof prereleaseNumber !== 'number') {
|
|
|
|
throw new Error(`Invalid prerelease format in version ${latestVersion}, expected e.g. -alpha.1`)
|
|
|
|
}
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2023-05-09 13:25:56 +00:00
|
|
|
// if we're in prerelease mode, don't bump the version, only the prerelease number
|
|
|
|
const nextVersion = prereleaseTag
|
|
|
|
? `${latestVersion.major}.${latestVersion.minor}.${latestVersion.patch}-${prereleaseTag}.${
|
|
|
|
Number(prereleaseNumber) + 1
|
2024-02-07 16:02:22 +00:00
|
|
|
}`
|
2024-02-29 12:21:11 +00:00
|
|
|
: latestVersion.inc(releaseType.bump).format()
|
|
|
|
|
|
|
|
return nextVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const huppyToken = process.env.HUPPY_TOKEN
|
|
|
|
assert(huppyToken && typeof huppyToken === 'string', 'HUPPY_ACCESS_KEY env var must be set')
|
|
|
|
|
|
|
|
// check we're on the main branch on HEAD
|
|
|
|
const currentBranch = (await exec('git', ['rev-parse', '--abbrev-ref', 'HEAD'])).toString().trim()
|
|
|
|
if (currentBranch !== 'main') {
|
|
|
|
throw new Error('Must be on main branch to publish')
|
|
|
|
}
|
|
|
|
|
|
|
|
const releaseType = getReleaseType()
|
2024-02-29 16:28:11 +00:00
|
|
|
const nextVersion = await getNextVersion(releaseType)
|
2024-02-29 12:21:11 +00:00
|
|
|
|
|
|
|
console.log('Releasing version', nextVersion)
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-02-29 17:38:19 +00:00
|
|
|
await setAllVersions(nextVersion)
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2023-05-09 13:25:56 +00:00
|
|
|
// stage the changes
|
2023-06-26 15:46:59 +00:00
|
|
|
const packageJsonFilesToAdd = []
|
|
|
|
for (const workspace of await getAllWorkspacePackages()) {
|
|
|
|
if (workspace.relativePath.startsWith('packages/')) {
|
|
|
|
packageJsonFilesToAdd.push(`${workspace.relativePath}/package.json`)
|
|
|
|
}
|
|
|
|
}
|
2024-02-29 17:55:36 +00:00
|
|
|
const versionFilesToAdd = glob.sync('**/*/version.ts', {
|
|
|
|
ignore: ['node_modules/**'],
|
|
|
|
follow: false,
|
|
|
|
})
|
|
|
|
console.log('versionFilesToAdd', versionFilesToAdd)
|
2023-07-07 15:32:08 +00:00
|
|
|
await exec('git', [
|
|
|
|
'add',
|
2024-02-29 17:38:19 +00:00
|
|
|
'--update',
|
2023-07-07 15:32:08 +00:00
|
|
|
'lerna.json',
|
|
|
|
...packageJsonFilesToAdd,
|
2024-02-29 17:55:36 +00:00
|
|
|
...versionFilesToAdd,
|
2023-07-07 15:32:08 +00:00
|
|
|
])
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-02-29 12:21:11 +00:00
|
|
|
const auto = new Auto({
|
|
|
|
plugins: ['npm'],
|
|
|
|
baseBranch: 'main',
|
|
|
|
owner: 'tldraw',
|
|
|
|
repo: 'tldraw',
|
|
|
|
verbose: true,
|
|
|
|
disableTsNode: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
await auto.loadConfig()
|
|
|
|
|
2023-05-09 13:25:56 +00:00
|
|
|
// this creates a new commit
|
|
|
|
await auto.changelog({
|
|
|
|
useVersion: nextVersion,
|
|
|
|
title: `v${nextVersion}`,
|
|
|
|
})
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2023-05-09 13:25:56 +00:00
|
|
|
// create and push a new tag
|
|
|
|
await exec('git', ['tag', '-f', `v${nextVersion}`])
|
|
|
|
await exec('git', ['push', '--follow-tags'])
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2023-05-09 13:25:56 +00:00
|
|
|
// create a release on github
|
|
|
|
await auto.runRelease({ useVersion: nextVersion })
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2023-05-09 13:25:56 +00:00
|
|
|
// finally, publish the packages [IF THIS STEP FAILS, RUN THE `publish-manual.ts` script locally]
|
|
|
|
await publish()
|
2023-05-15 13:17:19 +00:00
|
|
|
|
2023-06-01 18:01:49 +00:00
|
|
|
nicelog('Notifying huppy of release...')
|
2023-10-09 10:55:16 +00:00
|
|
|
const huppyResponse = await fetch('https://tldraw-repo-sync.fly.dev/api/on-release', {
|
2023-05-15 13:17:19 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ apiKey: huppyToken, tagToRelease: `v${nextVersion}`, canary: false }),
|
|
|
|
})
|
2023-06-01 18:01:49 +00:00
|
|
|
nicelog(
|
2023-05-15 13:17:19 +00:00
|
|
|
`huppy: [${huppyResponse.status} ${huppyResponse.statusText}] ${await huppyResponse.text()}`
|
|
|
|
)
|
2023-04-25 11:01:25 +00:00
|
|
|
}
|
2023-05-09 13:25:56 +00:00
|
|
|
|
|
|
|
main()
|