diff --git a/package.json b/package.json index a5f33bc0c4..1bc714082d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ef8888284e..43f204a067 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -51,9 +51,15 @@ importers: '@testing-library/user-event': specifier: ^14.4.3 version: 14.4.3(@testing-library/dom@9.3.0) + '@types/current-git-branch': + specifier: ^1.1.3 + version: 1.1.3 '@types/eslint': specifier: ^8.34.0 version: 8.37.0 + '@types/jsonfile': + specifier: ^6.1.1 + version: 6.1.1 '@types/luxon': specifier: ^3.3.0 version: 3.3.0 @@ -69,6 +75,9 @@ importers: '@types/semver': specifier: ^7.5.0 version: 7.5.0 + '@types/stream-to-array': + specifier: ^2.3.0 + version: 2.3.0 '@types/testing-library__jest-dom': specifier: ^5.14.5 version: 5.14.5(patch_hash=d573maxasnl5kxwdyzebcnmhpm) @@ -5514,6 +5523,10 @@ packages: /@types/cookie@0.5.1: resolution: {integrity: sha512-COUnqfB2+ckwXXSFInsFdOAWQzCCx+a5hq2ruyj+Vjund94RJQd4LG2u9hnvJrTgunKAaax7ancBYlDrNYxA0g==} + /@types/current-git-branch@1.1.3: + resolution: {integrity: sha512-4/q4F/2T3HZP/sqIiATlnOSYAWCXjQUHcUzIy63SWi0KOJd+kjCiL3NO/JgyeKqP/98N4e9vF017v2HPyKirrQ==} + dev: true + /@types/eslint@8.37.0: resolution: {integrity: sha512-Piet7dG2JBuDIfohBngQ3rCt7MgO9xCO4xIMKxBThCq5PNRB91IjlJ10eJVwfoNtvTErmxLzwBZ7rHZtbOMmFQ==} dependencies: @@ -5584,6 +5597,12 @@ packages: resolution: {integrity: sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==} dev: true + /@types/jsonfile@6.1.1: + resolution: {integrity: sha512-GSgiRCVeapDN+3pqA35IkQwasaCh/0YFH5dEF6S88iDvEn901DjOeH3/QPY+XYP1DFzDZPvIvfeEgk+7br5png==} + dependencies: + '@types/node': 18.13.0 + dev: true + /@types/luxon@3.3.0: resolution: {integrity: sha512-uKRI5QORDnrGFYgcdAVnHvEIvEZ8noTpP/Bg+HeUzZghwinDlIS87DEenV5r1YoOF9G4x600YsUXLWZ19rmTmg==} dev: true @@ -5667,6 +5686,12 @@ packages: resolution: {integrity: sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==} dev: false + /@types/stream-to-array@2.3.0: + resolution: {integrity: sha512-s8Y6/EV1LPn9fDlJKSlD8r+14hWXVDn+wPAGwWRzTUq/4MqdoQNEzQxP6Jq91qvFYxR3OlFAPXcv6ToplnAgFQ==} + dependencies: + '@types/node': 18.13.0 + dev: true + /@types/testing-library__jest-dom@5.14.5(patch_hash=d573maxasnl5kxwdyzebcnmhpm): resolution: {integrity: sha512-SBwbxYoyPIvxHbeHxTZX2Pe/74F/tX2/D3mMvzabdeJ25bBojfW0TyB8BHrbq/9zaaKICJZjLP+8r6AeZMFCuQ==} dependencies: diff --git a/scripts/config.js b/scripts/config.js index a2595ec57e..b9f8dfd995 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -80,25 +80,22 @@ export const packages = [ }, ] -export const latestBranch = 'main' - -/** @type {Record} */ +/** + * Contains config for publishable branches. + * @type {Record} + */ export const branchConfigs = { main: { prerelease: false, - ghRelease: true, }, next: { prerelease: true, - ghRelease: true, }, beta: { prerelease: true, - ghRelease: true, }, alpha: { prerelease: true, - ghRelease: true, }, } diff --git a/scripts/publish.js b/scripts/publish.js index 336888266e..609d08648e 100644 --- a/scripts/publish.js +++ b/scripts/publish.js @@ -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}` @@ -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[]} */ @@ -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) @@ -95,6 +86,7 @@ async function run() { */ const commitsSinceLatestTag = ( await new Promise((resolve, reject) => { + /** @type {NodeJS.ReadableStream} */ const strm = log.parse({ _: range, }) @@ -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( @@ -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' })[ @@ -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}"`) @@ -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!`) } @@ -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 diff --git a/scripts/types.d.ts b/scripts/types.d.ts index 185a5fce99..1018585a46 100644 --- a/scripts/types.d.ts +++ b/scripts/types.d.ts @@ -44,5 +44,4 @@ export type Package = { export type BranchConfig = { prerelease: boolean - ghRelease: boolean }