Skip to content

Commit

Permalink
feat: align semantic version with manifest version
Browse files Browse the repository at this point in the history
  • Loading branch information
owlcode committed Feb 21, 2024
1 parent 45b4c15 commit f9e34d3
Show file tree
Hide file tree
Showing 5 changed files with 5,988 additions and 4,589 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: echo "node:" && node --version && echo "" && echo "npm:" && npm --version && echo "" && echo "yarn:" && yarn --version

- name: Install dependencies
run: yarn install
run: yarn install --frozen-lockfile

- name: Run CI script
run: yarn run ci
Expand All @@ -51,7 +51,7 @@ jobs:
run: echo "node:" && node --version && echo "" && echo "npm:" && npm --version && echo "" && echo "yarn:" && yarn --version

- name: Install dependencies
run: yarn install
run: yarn install --frozen-lockfile

- name: Run build
run: yarn run build
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"start": "yarn build --watch",
"build": "rimraf ./dist && tsc && babel src --out-dir dist --extensions .js,.jsx,.ts,.tsx",
"lint": "eslint src",
"ci": "yarn lint && yarn build",
"ci": "yarn lint && yarn build && yarn test",
"test": "jest",
"typecheck": "tsc --noEmit",
"release": "semantic-release"
},
Expand All @@ -42,6 +43,7 @@
"@typescript-eslint/parser": "5.30.5",
"eslint": "8.19.0",
"eslint-config-prettier": "8.5.0",
"jest": "29.7.0",
"prettier": "2.7.1",
"rimraf": "3.0.2",
"semantic-release": "19.0.3",
Expand Down
26 changes: 26 additions & 0 deletions src/prepare.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, test } from '@jest/globals'
import { formatExtensionVersion } from './prepare'
import SemanticReleaseError from '@semantic-release/error'

describe('parsePrereleaseVersion', () => {
test('success case', () => {
;[
['1.0.0', '1.0.0'],
['1.0.0-develop', '1.0.0'],
['1.0.0-develop123', '1.0.0'],
['1.0.0-develop.5', '1.0.0.5'],
['1.0.0-dev.with.dots.123', '1.0.0.123'],
['1.0.0-dev.with1445.1', '1.0.0.1'],
].forEach(([input, expected]) =>
expect(formatExtensionVersion(input)).toEqual(expected),
)
})

test('failure case', () => {
;['develop.1', '1.12-dev.1', ''].forEach((input) =>
expect(() => {
formatExtensionVersion(input)
}).toThrow(SemanticReleaseError),
)
})
})
38 changes: 35 additions & 3 deletions src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import { Context } from 'semantic-release'
const prepareManifest = (
manifestPath: string,
version: string,
versionName: string,
logger: Context['logger'],
) => {
const manifest = readJsonSync(manifestPath)

writeJsonSync(manifestPath, { ...manifest, version }, { spaces: 2 })
writeJsonSync(
manifestPath,
{ ...manifest, version, versionName },
{ spaces: 2 },
)

logger.log('Wrote version %s to %s', version, manifestPath)
logger.log('Wrote version_name %s to %s', versionName, manifestPath)
}

const zipFolder = (
Expand All @@ -40,6 +46,29 @@ const zipFolder = (
logger.log('Wrote zipped file to %s', zipPath)
}

const findLastNumericPart = (preRelease?: string) => {
const parts = preRelease?.split('.') || []
return parts.reverse().find((part) => part.match(/^[0-9]+$/))
}

export const formatExtensionVersion = (version?: string) => {
const semanticVersionRegExp =
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
const versionMatch = version?.match(semanticVersionRegExp)

if (!versionMatch) {
throw new SemanticReleaseError(
'Version does not fit semantic version format',
)
}

const [, major, minor, patch, preRelease] = versionMatch

return [major, minor, patch, findLastNumericPart(preRelease)]
.filter(Boolean)
.join('.')
}

const prepare = (
{ manifestPath, distFolder, asset }: PluginConfig,
{ nextRelease, logger, lastRelease, branch, commits }: Context,
Expand All @@ -51,8 +80,10 @@ const prepare = (
)
}

const version = nextRelease?.version
if (!version) {
const version = formatExtensionVersion(nextRelease?.version)
const versionName = nextRelease?.version

if (!version || !versionName) {
throw new SemanticReleaseError(
'Could not determine the version from semantic release.',
)
Expand All @@ -70,6 +101,7 @@ const prepare = (
prepareManifest(
manifestPath || `${normalizedDistFolder}/manifest.json`,
version,
versionName,
logger,
)
zipFolder(compiledAssetString, normalizedDistFolder, logger)
Expand Down
Loading

0 comments on commit f9e34d3

Please sign in to comment.