Skip to content

Commit

Permalink
chore: Update publish script (#5559)
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins authored Jun 12, 2023
1 parent c72e1d4 commit 6875919
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 65 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@
"@testing-library/react": "^14.0.0",
"@testing-library/react-hooks": "^8.0.1",
"@testing-library/user-event": "^14.4.3",
"@types/current-git-branch": "^1.1.3",
"@types/eslint": "^8.34.0",
"@types/jsonfile": "^6.1.1",
"@types/luxon": "^3.3.0",
"@types/node": "^18.13.0",
"@types/react": "^18.2.4",
"@types/react-dom": "^18.2.4",
"@types/semver": "^7.5.0",
"@types/stream-to-array": "^2.3.0",
"@types/testing-library__jest-dom": "^5.14.5",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
Expand Down
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,22 @@ export const packages = [
},
]

export const latestBranch = 'main'

/** @type {Record<string, import('./types').BranchConfig>} */
/**
* Contains config for publishable branches.
* @type {Record<string, import('./types').BranchConfig>}
*/
export const branchConfigs = {
main: {
prerelease: false,
ghRelease: true,
},
next: {
prerelease: true,
ghRelease: true,
},
beta: {
prerelease: true,
ghRelease: true,
},
alpha: {
prerelease: true,
ghRelease: true,
},
}

Expand Down
100 changes: 43 additions & 57 deletions scripts/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import log from 'git-log-parser'
import streamToArray from 'stream-to-array'
import axios from 'axios'
import { DateTime } from 'luxon'
import { branchConfigs, latestBranch, packages, rootDir } from './config.js'
import { branchConfigs, packages, rootDir } from './config.js'

/** @param {string} version */
const releaseCommitMsg = (version) => `release: v${version}`
Expand All @@ -22,17 +22,8 @@ async function run() {
process.env.BRANCH ?? currentGitBranch()
)

/** @type {import('./types.js').BranchConfig | undefined} */
const branchConfig = branchConfigs[branchName]

if (!branchConfig) {
console.log(`No publish config found for branch: ${branchName}`)
console.log('Exiting...')
process.exit(0)
}

const isLatestBranch = branchName === latestBranch
const npmTag = isLatestBranch ? 'latest' : branchName
const isMainBranch = branchName === 'main'
const npmTag = isMainBranch ? 'latest' : branchName

// Get tags
/** @type {string[]} */
Expand All @@ -42,11 +33,11 @@ async function run() {
tags = tags
.filter((tag) => semver.valid(tag))
.filter((tag) => {
if (isLatestBranch) {
return semver.prerelease(tag) == null
if (semver.prerelease(tag) === null) {
return isMainBranch
} else {
return !isMainBranch
}

return tag.includes(`-${branchName}`)
})
// sort by latest
.sort(semver.compare)
Expand Down Expand Up @@ -95,6 +86,7 @@ async function run() {
*/
const commitsSinceLatestTag = (
await new Promise((resolve, reject) => {
/** @type {NodeJS.ReadableStream} */
const strm = log.parse({
_: range,
})
Expand Down Expand Up @@ -126,6 +118,10 @@ async function run() {

/**
* Parses the commit messsages, log them, and determine the type of release needed
* -1 means no release is necessary
* 0 means patch release is necessary
* 1 means minor release is necessary
* 2 means major release is necessary
* @type {number}
*/
let recommendedReleaseLevel = commitsSinceLatestTag.reduce(
Expand Down Expand Up @@ -304,6 +300,15 @@ async function run() {
recommendedReleaseLevel = 0
}

/** @type {import('./types.js').BranchConfig | undefined} */
const branchConfig = branchConfigs[branchName]

if (!branchConfig) {
console.log(`No publish config found for branch: ${branchName}`)
console.log('Exiting...')
process.exit(0)
}

const releaseType = branchConfig.prerelease
? 'prerelease'
: /** @type {const} */ ({ 0: 'patch', 1: 'minor', 2: 'major' })[
Expand Down Expand Up @@ -368,17 +373,6 @@ async function run() {
return
}

// Tag and commit
console.info(`Creating new git tag v${version}`)
execSync(`git tag -a -m "v${version}" v${version}`)

const taggedVersion = getTaggedVersion()
if (!taggedVersion) {
throw new Error(
'Missing the tagged release version. Something weird is afoot!',
)
}

console.info()
console.info(`Publishing all packages to npm with tag "${npmTag}"`)

Expand All @@ -396,36 +390,33 @@ async function run() {

console.info()

console.info(`Pushing new tags to branch.`)
execSync(`git push --tags`)
console.info(` Pushed tags to branch.`)

if (branchConfig.ghRelease) {
console.info(`Creating github release...`)
// Stringify the markdown to excape any quotes
execSync(
`gh release create v${version} ${
!isLatestBranch ? '--prerelease' : ''
} --notes '${changelogMd.replace(/'/g, '"')}'`,
)
console.info(` Github release created.`)

console.info(`Committing changes...`)
execSync(`git add -A && git commit -m "${releaseCommitMsg(version)}"`)
console.info()
console.info(` Committed Changes.`)
console.info(`Pushing changes...`)
execSync(`git push`)
console.info()
console.info(` Changes pushed.`)
} else {
console.info(`Skipping github release and change commit.`)
}
console.info(`Committing changes...`)
execSync(`git add -A && git commit -m "${releaseCommitMsg(version)}"`)
console.info()
console.info(` Committed Changes.`)

console.info(`Pushing changes...`)
execSync(`git push`)
console.info()
console.info(` Changes pushed.`)

console.info(`Creating new git tag v${version}`)
execSync(`git tag -a -m "v${version}" v${version}`)

console.info(`Pushing tags...`)
execSync(`git push --tags`)
console.info()
console.info(` Tags pushed.`)

console.info(`Creating github release...`)
// Stringify the markdown to excape any quotes
execSync(
`gh release create v${version} ${
!isMainBranch ? '--prerelease' : ''
} --notes '${changelogMd.replace(/'/g, '"')}'`,
)
console.info(` Github release created.`)

console.info(`All done!`)
}

Expand Down Expand Up @@ -459,11 +450,6 @@ async function updatePackageJson(pathName, transform) {
})
}

function getTaggedVersion() {
const output = execSync('git tag --list --points-at HEAD').toString()
return output.replace(/^v|\n+$/g, '')
}

/**
* @template TItem
* @param {((d: TItem) => any)[]} sorters
Expand Down
1 change: 0 additions & 1 deletion scripts/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ export type Package = {

export type BranchConfig = {
prerelease: boolean
ghRelease: boolean
}

0 comments on commit 6875919

Please sign in to comment.