-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-react): Add
versionSelector
helper (#1780)
* feat(clerk-react): Add versionSelector helper * Create neat-needles-poke.md * fix(clerk-react): Only care about snapshot
- Loading branch information
Showing
6 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@clerk/clerk-react": patch | ||
--- | ||
|
||
Refactor our script loading logic to use a `versionSelector` helper function. No change in behavior should occur. This internal change allows versions tagged with `snapshot` and `staging` to use the exact corresponding NPM version of `@clerk/clerk-js`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/react/src/utils/__tests__/versionSelector.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { versionSelector } from '../versionSelector'; | ||
|
||
describe('versionSelector', () => { | ||
it('should return the clerkJSVersion if it is provided', () => { | ||
expect(versionSelector('1.0.0')).toEqual('1.0.0'); | ||
}); | ||
it('should use the major version if there is no prerelease tag', () => { | ||
// @ts-ignore | ||
global.PACKAGE_VERSION = '1.0.0'; | ||
// @ts-ignore | ||
global.JS_PACKAGE_VERSION = '2.0.0'; | ||
|
||
expect(versionSelector(undefined)).toEqual('1'); | ||
}); | ||
it('should use the prerelease tag when it is not snapshot', () => { | ||
// @ts-ignore | ||
global.PACKAGE_VERSION = '1.0.0-next.0'; | ||
// @ts-ignore | ||
global.JS_PACKAGE_VERSION = '2.0.0-next.0'; | ||
|
||
expect(versionSelector(undefined)).toEqual('next'); | ||
}); | ||
it('should use the exact JS version if tag is snapshot', () => { | ||
// @ts-ignore | ||
global.PACKAGE_VERSION = '1.0.0-snapshot.0'; | ||
// @ts-ignore | ||
global.JS_PACKAGE_VERSION = '2.0.0-snapshot.0'; | ||
|
||
expect(versionSelector(undefined)).toEqual('2.0.0-snapshot.0'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* This version selector is a bit complicated, so here is the flow: | ||
* 1. Use the clerkJSVersion prop on the provider | ||
* 2. Use the exact `@clerk/clerk-js` version if it is a `@snapshot` prerelease for `@clerk/clerk-react` | ||
* 3. Use the prerelease tag of `@clerk/clerk-react` | ||
* 4. Fallback to the major version of `@clerk/clerk-react` | ||
* @param clerkJSVersion - The optional clerkJSVersion prop on the provider | ||
* @returns The npm tag, version or major version to use | ||
*/ | ||
export const versionSelector = (clerkJSVersion: string | undefined) => { | ||
if (clerkJSVersion) { | ||
return clerkJSVersion; | ||
} | ||
|
||
const prereleaseTag = getPrereleaseTag(PACKAGE_VERSION); | ||
if (prereleaseTag) { | ||
if (prereleaseTag === 'snapshot') { | ||
return JS_PACKAGE_VERSION; | ||
} | ||
|
||
return prereleaseTag; | ||
} | ||
|
||
return getMajorVersion(PACKAGE_VERSION); | ||
}; | ||
|
||
// TODO: Replace these with "semver" package | ||
const getPrereleaseTag = (packageVersion: string) => packageVersion.match(/-(.*)\./)?.[1]; | ||
const getMajorVersion = (packageVersion: string) => packageVersion.split('.')[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters