tldraw/scripts/publish-new.ts

98 lines
2.8 KiB
TypeScript
Raw Normal View History

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'
import { assert } from 'node:console'
2023-04-25 11:01:25 +00:00
import { parse } from 'semver'
import { exec } from './lib/exec'
import { REPO_ROOT } from './lib/file'
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
async function main() {
2023-05-15 13:17:19 +00:00
const huppyToken = process.env.HUPPY_TOKEN
assert(huppyToken && typeof huppyToken === 'string', 'HUPPY_ACCESS_KEY env var must be set')
const auto = new Auto({
plugins: ['npm'],
baseBranch: 'main',
owner: 'tldraw',
repo: 'tldraw',
verbose: true,
})
2023-04-25 11:01:25 +00:00
// module was called directly
const currentBranch = (await exec('git', ['rev-parse', '--abbrev-ref', 'HEAD'])).toString().trim()
2023-04-25 11:01:25 +00:00
if (currentBranch !== 'main') {
throw new Error('Must be on main branch to publish')
}
await auto.loadConfig()
const bump = await auto.getVersion()
if (!bump) {
nicelog('nothing to do')
return
}
2023-04-25 11:01:25 +00:00
const latestVersion = parse(getLatestVersion())!
2023-04-25 11:01:25 +00:00
nicelog('latestVersion', latestVersion)
2023-04-25 11:01:25 +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
// 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
}`
: latestVersion.inc(bump).format()
2023-04-25 11:01:25 +00:00
setAllVersions(nextVersion)
2023-04-25 11:01:25 +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`)
}
}
await exec('git', [
'add',
'lerna.json',
...packageJsonFilesToAdd,
REPO_ROOT + '/packages/*/src/**/version.ts',
])
2023-04-25 11:01:25 +00:00
// this creates a new commit
await auto.changelog({
useVersion: nextVersion,
title: `v${nextVersion}`,
})
2023-04-25 11:01:25 +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
// create a release on github
await auto.runRelease({ useVersion: nextVersion })
2023-04-25 11:01:25 +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
nicelog('Notifying huppy of release...')
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 }),
})
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
}
main()