Skip to content

Commit

Permalink
Inject cloud region into build (#1818)
Browse files Browse the repository at this point in the history
Add support for a CLOUD_REGION env variable
which allows either US or EU to be set as the
cloud region. Defaults to US.

J=BACK-2269
TEST=manual

Manually ran a build with the CLOUD_REGION
env variable set to US and EU and confirmed
the CLOUD_REGION variable was set correctly
in dist/answers.js
  • Loading branch information
cea2aj committed May 4, 2023
1 parent 96cc51e commit 4240dd3
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 29 deletions.
10 changes: 10 additions & 0 deletions conf/cloud-regions/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const CLOUD_REGION = {
US: 'us',
EU: 'eu'
};

exports.CLOUD_REGION = CLOUD_REGION;

exports.DEFAULT_CLOUD_REGION = CLOUD_REGION.US;

exports.ALL_CLOUD_REGIONS = Object.values(CLOUD_REGION);
27 changes: 22 additions & 5 deletions conf/gulp-tasks/bundle/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const TranslateCallParser = require('../../i18n/translatecallparser');
* @param {string} locale The current locale
* @param {string} libVersion The current JS library version
* @param {TranslationResolver} translationResolver
* @param {string} cloudRegion The current cloud region
*
* @returns {stream.Writable} A {@link Writable} stream containing the modern
* SDK bundle.
Expand All @@ -37,7 +38,8 @@ exports.modernBundle = function (
bundleName,
locale,
libVersion,
translationResolver) {
translationResolver,
cloudRegion) {
const rollupConfig = {
input: entryPoint,
output: outputConfig,
Expand All @@ -55,7 +57,8 @@ exports.modernBundle = function (
rollupJson()
]
};
return _buildBundle(callback, rollupConfig, bundleName, locale, libVersion, translationResolver);
return _buildBundle(
callback, rollupConfig, bundleName, locale, libVersion, translationResolver, cloudRegion);
};

/**
Expand All @@ -69,6 +72,7 @@ exports.modernBundle = function (
* @param {string} locale The current locale
* @param {string} libVersion The current JS library version
* @param {TranslationResolver} translationResolver
* @param {string} cloudRegion The cloud region
* @returns {stream.Writable} A {@link Writable} stream containing the legacy
* SDK bundle.
*/
Expand All @@ -79,7 +83,9 @@ exports.legacyBundle = function (
bundleName,
locale,
libVersion,
translationResolver) {
translationResolver,
cloudRegion
) {
const rollupConfig = {
input: entryPoint,
output: outputConfig,
Expand Down Expand Up @@ -115,7 +121,8 @@ exports.legacyBundle = function (
rollupJson()
]
};
return _buildBundle(callback, rollupConfig, bundleName, locale, libVersion, translationResolver);
return _buildBundle(
callback, rollupConfig, bundleName, locale, libVersion, translationResolver, cloudRegion);
};

/**
Expand All @@ -127,9 +134,18 @@ exports.legacyBundle = function (
* @param {string} locale The current locale
* @param {string} libVersion The current JS library version
* @param {TranslationResolver} translationResolver for the given locale
* @param {string} cloudRegion The cloud region
* @returns {stream.Writable} A {@link Writable} stream containing the SDK bundle.
*/
function _buildBundle (callback, rollupConfig, bundleName, locale, libVersion, translationResolver) {
function _buildBundle (
callback,
rollupConfig,
bundleName,
locale,
libVersion,
translationResolver,
cloudRegion
) {
return rollup(rollupConfig)
.pipe(source(`${bundleName}.js`))
.pipe(replace('@@LIB_VERSION', libVersion))
Expand All @@ -144,6 +160,7 @@ function _buildBundle (callback, rollupConfig, bundleName, locale, libVersion, t
placeholder.hasNoInterpolation();
return canBeTranslatedStatically ? `"${translationResult}"` : translationResult;
}))
.pipe(replace('@@CLOUD_REGION', cloudRegion))
.pipe(dest('dist'))
.on('end', callback);
}
12 changes: 8 additions & 4 deletions conf/gulp-tasks/bundle/bundletaskfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ Object.freeze(BundleType);
* A factory class that provides Gulp tasks for the different kinds of SDK bundles.
*/
class BundleTaskFactory {
constructor (libVersion, translationResolver, locale) {
constructor (libVersion, translationResolver, locale, cloudRegion) {
this._libVersion = libVersion;
this._locale = locale;
this._translationResolver = translationResolver;
this._namespace = 'ANSWERS';
this._cloudRegion = cloudRegion;
}

/**
Expand Down Expand Up @@ -70,7 +71,8 @@ class BundleTaskFactory {
getBundleName(BundleType.MODERN, this._locale),
this._locale,
this._libVersion,
this._translationResolver
this._translationResolver,
this._cloudRegion
);
}

Expand All @@ -95,7 +97,8 @@ class BundleTaskFactory {
getBundleName(BundleType.LEGACY_IIFE, this._locale),
this._locale,
this._libVersion,
this._translationResolver
this._translationResolver,
this._cloudRegion
);
}

Expand All @@ -121,7 +124,8 @@ class BundleTaskFactory {
getBundleName(BundleType.LEGACY_UMD, this._locale),
this._locale,
this._libVersion,
this._translationResolver
this._translationResolver,
this._cloudRegion
);
}
}
Expand Down
32 changes: 18 additions & 14 deletions conf/gulp-tasks/library.gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const rename = require('gulp-rename');
const getLibraryVersion = require('./utils/libversion');
const { BundleType, BundleTaskFactory } = require('./bundle/bundletaskfactory');
const { DEFAULT_LOCALE, ALL_LANGUAGES } = require('../i18n/constants');
const { DEFAULT_CLOUD_REGION } = require('../cloud-regions/constants');
const { copyAssetsForLocales } = require('../i18n/localebuildutils');
const LocalFileParser = require('../i18n/localfileparser');
const MinifyTaskFactory = require('./bundle/minifytaskfactory');
Expand All @@ -20,7 +21,7 @@ const { generateProcessTranslationJsCall } = require('../i18n/runtimecallgenerat
* @returns {Promise<Function>}
*/
exports.dev = function devJSBundle () {
return createBundleTaskFactory(DEFAULT_LOCALE).then(devTaskFactory => {
return createBundleTaskFactory(DEFAULT_LOCALE, DEFAULT_CLOUD_REGION).then(devTaskFactory => {
return new Promise(resolve => {
return parallel(
series(devTaskFactory.create(BundleType.LEGACY_IIFE), getWatchJSTask(devTaskFactory)),
Expand All @@ -35,7 +36,7 @@ exports.dev = function devJSBundle () {
* @returns {Promise<Function>}
*/
exports.unminifiedLegacy = function unminifiedLegacyJSBundle () {
return createBundleTaskFactory(DEFAULT_LOCALE).then(unminifiedLegacyTaskFactory => {
return createBundleTaskFactory(DEFAULT_LOCALE, DEFAULT_CLOUD_REGION).then(unminifiedLegacyTaskFactory => {
return new Promise(resolve => {
return parallel(
unminifiedLegacyTaskFactory.create(BundleType.LEGACY_IIFE),
Expand All @@ -51,13 +52,14 @@ exports.unminifiedLegacy = function unminifiedLegacyJSBundle () {
* of creating bundles for just languages.
*
* @param {Array<string>} languages
* @param {string} cloudRegion
* @param {boolean} isSearchBarOnly If the build task is for the SearchBar-only assets.
* @returns {Promise<Function>}
*/
function createJSBundlesForLanguages (languages, isSearchBarOnly = false) {
function createJSBundlesForLanguages (languages, cloudRegion, isSearchBarOnly = false) {
const localizedTaskPromises = languages.map(language => {
const minifyTaskFactory = new MinifyTaskFactory(language);
return createBundleTaskFactory(language)
return createBundleTaskFactory(language, cloudRegion)
.then(bundleTaskFactory => createBundles(bundleTaskFactory, minifyTaskFactory, isSearchBarOnly));
});
return Promise.all(localizedTaskPromises).then(localizedTasks => {
Expand All @@ -66,14 +68,15 @@ function createJSBundlesForLanguages (languages, isSearchBarOnly = false) {
}

/**
* Creates a task for building all of the localized SDK assets.
* Creates a task for building all the localized SDK assets.
*
* @param {boolean} isSearchBarOnly If the task is for the SearchBar-only assets.
* @param {string[]} languages a list of languages to build js bundles for,
* with the corresponding language-locale pairs.
* @param {string} cloudRegion the cloud region for the build.
* @returns {Promise<Function>}
*/
function allLocaleJSBundles (isSearchBarOnly = false, languages) {
function allLocaleJSBundles (isSearchBarOnly = false, languages, cloudRegion) {
const assetNames = [
'answers.js',
'answers.min.js',
Expand All @@ -82,8 +85,8 @@ function allLocaleJSBundles (isSearchBarOnly = false, languages) {
'answers-umd.js',
'answers-umd.min.js'];

return createJSBundlesForLanguages(languages, isSearchBarOnly).then(() => {
copyAssetsForLocales(assetNames, languages);
return createJSBundlesForLanguages(languages, cloudRegion, isSearchBarOnly).then(() => {
copyAssetsForLocales(assetNames, languages, cloudRegion);
});
}

Expand All @@ -95,12 +98,12 @@ exports.buildLanguages = function allLanguageJSBundles () {
return createJSBundlesForLanguages(ALL_LANGUAGES);
};

exports.buildLocales = function (languages = ALL_LANGUAGES) {
return allLocaleJSBundles(false, languages);
exports.buildLocales = function (languages = ALL_LANGUAGES, cloudRegion = DEFAULT_CLOUD_REGION) {
return allLocaleJSBundles(false, languages, cloudRegion);
};

exports.buildSearchBarOnlyAssets = function (languages = ALL_LANGUAGES) {
return allLocaleJSBundles(true, languages);
exports.buildSearchBarOnlyAssets = function (languages = ALL_LANGUAGES, cloudRegion) {
return allLocaleJSBundles(true, languages, cloudRegion);
};

/**
Expand Down Expand Up @@ -132,9 +135,10 @@ function createBundles (bundleTaskFactory, minifyTaskFactory, isSearchBarOnly) {
* Creates a BundleTaskFactory configured with translations for the given locale.
*
* @param {string} locale
* @param {string} cloudRegion
* @returns {BundleTaskFactory} bundleTaskFactory
*/
async function createBundleTaskFactory (locale) {
async function createBundleTaskFactory (locale, cloudRegion) {
const localFileParser = new LocalFileParser(path.join(__dirname, '../i18n/translations'));
const translation = await localFileParser.fetch(locale);

Expand All @@ -143,7 +147,7 @@ async function createBundleTaskFactory (locale) {
translator,
generateProcessTranslationJsCall);

return new BundleTaskFactory(getLibraryVersion(), translationResolver, locale);
return new BundleTaskFactory(getLibraryVersion(), translationResolver, locale, cloudRegion);
}

function compileCSS () {
Expand Down
12 changes: 9 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ if (languageEnv && typeof languageEnv === 'string') {
languages = languageEnv.split(',');
}

const cloudRegionEnv = process.env.CLOUD_REGION;
let cloudRegion;
if (cloudRegionEnv && typeof cloudRegionEnv === 'string') {
cloudRegion = cloudRegionEnv;
}

exports.default = exports.build = parallel(
templates.default,
library.default
Expand All @@ -27,14 +33,14 @@ exports.buildLanguages = parallel(
library.buildLanguages
);
exports.buildLocales = parallel(
library.buildLocales.bind(null, languages),
library.buildLocales.bind(null, languages, cloudRegion),
templates.buildLocales.bind(null, languages)
);

exports.extractTranslations = extractTranslations;
exports.templates = templates.default;

exports.buildSearchBarOnlyAssets = parallel(
templates.buildSearchBarOnlyAssets.bind(null, languages),
library.buildSearchBarOnlyAssets.bind(null, languages)
library.buildSearchBarOnlyAssets.bind(null, languages, cloudRegion),
templates.buildSearchBarOnlyAssets.bind(null, languages)
);
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions src/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const LOCALE = '@@LOCALE';
/** The speech recognition locales supported by Microsoft Edge */
export const SPEECH_RECOGNITION_LOCALES_SUPPORTED_BY_EDGE = '@@SPEECH_RECOGNITION_LOCALES_SUPPORTED_BY_EDGE';

export const CLOUD_REGION = '@@CLOUD_REGION';

/** The identifier of the production environment */
export const PRODUCTION = 'production';

Expand Down
4 changes: 3 additions & 1 deletion src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import FilterRegistry from './filters/filterregistry';
import DirectAnswer from './models/directanswer';
import AutoCompleteResponseTransformer from './search/autocompleteresponsetransformer';

import { PRODUCTION, ENDPOINTS, LIB_VERSION } from './constants';
import { PRODUCTION, ENDPOINTS, LIB_VERSION, CLOUD_REGION } from './constants';
import { getCachedLiveApiUrl, getLiveApiUrl } from './utils/urlutils';
import { SearchParams } from '../ui';
import SearchStates from './storage/searchstates';
Expand Down Expand Up @@ -113,6 +113,8 @@ export default class Core {
*/
this._environment = config.environment || PRODUCTION;

this._cloudRegion = CLOUD_REGION;

/** @type {string} */
this._verticalKey = config.verticalKey;

Expand Down

0 comments on commit 4240dd3

Please sign in to comment.