-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
112 additions
and
46 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,35 @@ | ||
const RELEASE_BRANCH_REGEX = /^release\/v[0-9.]+$/; | ||
const HOTFIX_BRANCH_REGEX = /^hotfix\/v[0-9.]+$/; | ||
const SEM_VER_REGEX = /^[1-9]+$|^[1-9]+\.[0-9]+$|^[1-9]+\.[0-9]+\.[0-9]+$/; | ||
|
||
/** | ||
* Given a branch (or release) of the SDK and a locale, this helper provides the correct | ||
* URL in the CDN for the asset. | ||
* | ||
* @param {string} branch The branch (or release) of the SDK. | ||
* @param {string} locale The locale to use. | ||
* @param {string} assetName The name of the desired asset. | ||
* @returns {string} The CDN URL of the localized asset. | ||
*/ | ||
module.exports = function sdkAssetUrl(branch, locale, assetName) { | ||
const isReleasedBranch = SEM_VER_REGEX.test(branch); | ||
|
||
let parsedBranch; | ||
if (isReleasedBranch) { | ||
parsedBranch = `v${branch}`; | ||
} else { | ||
parsedBranch = `dev/${branch.replace(/\//g, '-')}`; | ||
} | ||
|
||
const isPreReleaseBranch = | ||
RELEASE_BRANCH_REGEX.test(branch) || HOTFIX_BRANCH_REGEX.test(branch); | ||
const isLocalizationSupported = | ||
(isReleasedBranch || isPreReleaseBranch) && | ||
!(locale.startsWith('en') || assetName === 'answers.css') ; | ||
|
||
const parsedAssetName = isLocalizationSupported ? | ||
`${locale}-${assetName}` : | ||
assetName; | ||
|
||
return `https://assets.sitescdn.net/answers/${parsedBranch}/${parsedAssetName}`; | ||
}; |
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
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 was deleted.
Oops, something went wrong.
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,73 @@ | ||
import sdkAssetUrl from '../../hbshelpers/sdkAssetUrl'; | ||
|
||
describe('URLs are computed properly for released versions', () => { | ||
it('works correctly when locale is "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.css' | ||
|
||
expect(sdkAssetUrl('1.8', 'en', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('1.8', 'en', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
|
||
it('works correctly when locale is not "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/v1.8/fr-answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.css' | ||
|
||
expect(sdkAssetUrl('1.8', 'fr', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('1.8', 'fr', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
}); | ||
|
||
describe('URLs are computed properly for release branches', () => { | ||
it('works correctly when locale is "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.css' | ||
|
||
expect(sdkAssetUrl('release/v1.8', 'en', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('release/v1.8', 'en', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
|
||
it('works correctly when locale is not "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/fr-answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.css' | ||
|
||
expect(sdkAssetUrl('release/v1.8', 'fr', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('release/v1.8', 'fr', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
}); | ||
|
||
describe('URLs are computed properly for hotfix branches', () => { | ||
it('works correctly when locale is "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.css' | ||
|
||
expect(sdkAssetUrl('hotfix/v1.8.1', 'en', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('hotfix/v1.8.1', 'en', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
|
||
it('works correctly when locale is not "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/fr-answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.css' | ||
|
||
expect(sdkAssetUrl('hotfix/v1.8.1', 'fr', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('hotfix/v1.8.1', 'fr', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
}); | ||
|
||
describe('URLs are computed properly for all other branches', () => { | ||
it('works correctly when locale is "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.css' | ||
|
||
expect(sdkAssetUrl('feature/foo', 'en', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('feature/foo', 'en', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
|
||
it('works correctly when locale is not "en"', () => { | ||
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.min.js'; | ||
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.css' | ||
|
||
expect(sdkAssetUrl('feature/foo', 'fr', 'answers.min.js')).toEqual(expectedJSUrl); | ||
expect(sdkAssetUrl('feature/foo', 'fr', 'answers.css')).toEqual(expectedCSSUrl); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.