Skip to content

Commit

Permalink
Merge pull request #754 from jmwiese/STRF-9345
Browse files Browse the repository at this point in the history
feat: strf-9345 Infer apiHost from storeUrl
  • Loading branch information
jmwiese authored Sep 9, 2021
2 parents 7283549 + 5b132e9 commit 403d51c
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 23 deletions.
6 changes: 3 additions & 3 deletions bin/stencil-download.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ require('colors');
const inquirer = require('inquirer');
const program = require('../lib/commander');

const { API_HOST, PACKAGE_INFO } = require('../constants');
const { PACKAGE_INFO } = require('../constants');
const stencilDownload = require('../lib/stencil-download');
const { checkNodeVersion } = require('../lib/cliCommon');
const { printCliResultErrorAndExit } = require('../lib/cliCommon');

program
.version(PACKAGE_INFO.version)
.option('-h, --host [hostname]', 'specify the api host', API_HOST)
.option('-h, --host [hostname]', 'specify the api host')
.option('-f, --file [filename]', 'specify the filename to download only')
.option('-e, --exclude [exclude]', 'specify a directory to exclude from download')
.option('-c, --channel_id [channelId]', 'specify the channel ID of the storefront', parseInt)
Expand All @@ -23,7 +23,7 @@ const cliOptions = program.opts();
const extraExclude = cliOptions.exclude ? [cliOptions.exclude] : [];
const options = {
exclude: ['parsed', 'manifest.json', ...extraExclude],
apiHost: cliOptions.host || API_HOST,
apiHost: cliOptions.host,
channelId: cliOptions.channel_id,
applyTheme: true, // fix to be compatible with stencil-push.utils
file: cliOptions.file,
Expand Down
7 changes: 4 additions & 3 deletions bin/stencil-pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require('colors');

const { PACKAGE_INFO, API_HOST } = require('../constants');
const { PACKAGE_INFO } = require('../constants');
const program = require('../lib/commander');
const stencilPull = require('../lib/stencil-pull');
const { checkNodeVersion } = require('../lib/cliCommon');
Expand All @@ -11,7 +11,7 @@ const { printCliResultErrorAndExit } = require('../lib/cliCommon');
program
.version(PACKAGE_INFO.version)
.option('-s, --saved', 'get the saved configuration instead of the active one')
.option('-h, --host [hostname]', 'specify the api host', API_HOST)
.option('-h, --host [hostname]', 'specify the api host')
.option(
'-f, --filename [filename]',
'specify the filename to save the config as',
Expand All @@ -27,8 +27,9 @@ program
checkNodeVersion();

const cliOptions = program.opts();

const options = {
apiHost: cliOptions.host || API_HOST,
apiHost: cliOptions.host,
saveConfigName: cliOptions.filename,
channelId: cliOptions.channel_id,
saved: cliOptions.saved || false,
Expand Down
4 changes: 2 additions & 2 deletions bin/stencil-push.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

require('colors');
const { PACKAGE_INFO, API_HOST } = require('../constants');
const { PACKAGE_INFO } = require('../constants');
const program = require('../lib/commander');
const stencilPush = require('../lib/stencil-push');
const { checkNodeVersion } = require('../lib/cliCommon');
Expand All @@ -25,7 +25,7 @@ checkNodeVersion();

const cliOptions = program.opts();
const options = {
apiHost: cliOptions.host || API_HOST,
apiHost: cliOptions.host,
channelId: cliOptions.channel_id,
bundleZipPath: cliOptions.file,
activate: cliOptions.activate,
Expand Down
4 changes: 2 additions & 2 deletions bin/stencil-start.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

require('colors');
const { PACKAGE_INFO, API_HOST } = require('../constants');
const { PACKAGE_INFO } = require('../constants');
const program = require('../lib/commander');
const StencilStart = require('../lib/stencil-start');
const { printCliResultErrorAndExit } = require('../lib/cliCommon');
Expand All @@ -28,7 +28,7 @@ const options = {
open: cliOptions.open,
variation: cliOptions.variation,
channelId: cliOptions.channelId,
apiHost: cliOptions.host || API_HOST,
apiHost: cliOptions.host,
tunnel: cliOptions.tunnel,
cache: cliOptions.cache,
};
Expand Down
6 changes: 6 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ const DEFAULT_CUSTOM_LAYOUTS_CONFIG = {

/// ///////////////////////////////////////// Other //////////////////////////////////////// ///

const DEV_API_HOST = 'https://api.service.bcdev';
const INTG_API_HOST = 'https://api.integration.zone';
const STG_API_HOST = 'https://api.staging.zone';
const API_HOST = 'https://api.bigcommerce.com';

module.exports = {
PACKAGE_INFO,
THEME_PATH,
DEFAULT_CUSTOM_LAYOUTS_CONFIG,
DEV_API_HOST,
INTG_API_HOST,
STG_API_HOST,
API_HOST,
};
3 changes: 2 additions & 1 deletion lib/stencil-download.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ utils.downloadThemeFiles = async (options) => {
utils.startThemeDownloadJob = async (options) => {
const {
config: { accessToken },
apiHost,
activeTheme,
storeHash,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

const { jobId } = await themeApiClient.downloadTheme({
accessToken,
themeId: activeTheme.active_theme_uuid,
Expand Down
28 changes: 27 additions & 1 deletion lib/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ const inquirerModule = require('inquirer');

const serverConfigModule = require('../server/config');
const StencilConfigManager = require('./StencilConfigManager');
const { DEFAULT_CUSTOM_LAYOUTS_CONFIG } = require('../constants');
const {
DEFAULT_CUSTOM_LAYOUTS_CONFIG,
API_HOST,
INTG_API_HOST,
STG_API_HOST,
DEV_API_HOST,
} = require('../constants');

class StencilInit {
/**
Expand Down Expand Up @@ -133,15 +139,35 @@ class StencilInit {
return questions.length ? this._inquirer.prompt(questions) : {};
}

apiHostFromStoreUrl(storeUrl) {
if (storeUrl !== undefined) {
if (storeUrl.includes('service.bcdev')) {
return DEV_API_HOST;
}
if (storeUrl.includes('my-integration.zone')) {
return INTG_API_HOST;
}
if (storeUrl.includes('my-staging.zone')) {
return STG_API_HOST;
}
}

return API_HOST;
}

/**
* @param {object} stencilConfig
* @param {object} answers
* @param {object} cliOptions
* @returns {object}
*/
applyAnswers(stencilConfig, answers, cliOptions) {
const storeUrl =
answers.normalStoreUrl || cliOptions.normalStoreUrl || stencilConfig.normalStoreUrl;

return {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
apiHost: this.apiHostFromStoreUrl(storeUrl),
...stencilConfig,
...cliOptions,
...answers,
Expand Down
36 changes: 35 additions & 1 deletion lib/stencil-init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const inquirerModule = require('inquirer');

const StencilInit = require('./stencil-init');
const StencilConfigManager = require('./StencilConfigManager');
const { DEFAULT_CUSTOM_LAYOUTS_CONFIG } = require('../constants');
const { DEFAULT_CUSTOM_LAYOUTS_CONFIG, API_HOST } = require('../constants');
const { assertNoMutations } = require('../test/assertions/assertNoMutations');

const getStencilConfig = () => ({
Expand All @@ -21,6 +21,7 @@ const getStencilConfig = () => ({
port: 3001,
accessToken: 'accessToken_from_stencilConfig',
githubToken: 'githubToken_1234567890',
apiHost: API_HOST,
});
const getAnswers = () => ({
normalStoreUrl: 'https://url-from-answers.mybigcommerce.com',
Expand Down Expand Up @@ -73,6 +74,7 @@ describe('StencilInit integration tests', () => {
const expectedResult = {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
...answers,
apiHost: API_HOST,
};
const stencilConfigManager = new StencilConfigManager({
themePath: './test/_mocks/themes/valid/',
Expand Down Expand Up @@ -110,6 +112,7 @@ describe('StencilInit integration tests', () => {
const expectedResult = {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
...cliOptions,
apiHost: API_HOST,
};
const stencilConfigManager = new StencilConfigManager({
themePath: './test/_mocks/themes/valid/',
Expand Down Expand Up @@ -364,4 +367,35 @@ describe('StencilInit unit tests', () => {
expect(res.githubToken).toEqual(stencilConfig.githubToken);
});
});

describe('apiHostFromStoreUrl', () => {
it('should return the integration api host for integration stores', async () => {
const storeUrl = 'https://store-url.my-integration.zone';
const expected = 'https://api.integration.zone';

const { instance } = createStencilInitInstance();
const res = instance.apiHostFromStoreUrl(storeUrl);

expect(res).toEqual(expected);
});

it('should return the staging api host for staging stores', async () => {
const storeUrl = 'https://store-url.my-staging.zone';
const expected = 'https://api.staging.zone';

const { instance } = createStencilInitInstance();
const res = instance.apiHostFromStoreUrl(storeUrl);

expect(res).toEqual(expected);
});

it('should return the API_HOST constant for all ohter stores', async () => {
const storeUrl = 'https://store-url.mystore.com';

const { instance } = createStencilInitInstance();
const res = instance.apiHostFromStoreUrl(storeUrl);

expect(res).toEqual(API_HOST);
});
});
});
6 changes: 4 additions & 2 deletions lib/stencil-pull.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const utils = {};
utils.getChannelActiveTheme = async (options) => {
const {
config: { accessToken },
apiHost,
storeHash,
channelId,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

const activeTheme = await themeApiClient.getChannelActiveTheme({
accessToken,
apiHost,
Expand All @@ -28,12 +29,13 @@ utils.getChannelActiveTheme = async (options) => {
utils.getThemeConfiguration = async (options) => {
const {
config: { accessToken },
apiHost,
storeHash,
activeTheme,
saved,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

const themeId = activeTheme.active_theme_uuid;

const configurationId = saved
Expand Down
21 changes: 14 additions & 7 deletions lib/stencil-push.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ utils.getStoreHash = async (options) => {
utils.getThemes = async (options) => {
const {
config: { accessToken },
apiHost,
storeHash,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

const themes = await themeApiClient.getThemes({ accessToken, apiHost, storeHash });

return { ...options, themes };
Expand Down Expand Up @@ -84,11 +85,12 @@ utils.generateBundle = async (options) => {
utils.uploadBundle = async (options) => {
const {
config: { accessToken },
apiHost,
bundleZipPath,
storeHash,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

try {
const result = await themeApiClient.postTheme({
accessToken,
Expand Down Expand Up @@ -162,12 +164,13 @@ utils.promptUserToDeleteThemesIfNecessary = async (options) => {
utils.deleteThemesIfNecessary = async (options) => {
const {
config: { accessToken },
apiHost,
storeHash,
themeLimitReached,
themeIdsToDelete,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

if (!themeLimitReached) {
return options;
}
Expand Down Expand Up @@ -229,12 +232,13 @@ utils.pollForJobCompletion = (resultFilter) => {
utils.checkIfJobIsComplete = (resultFilter) => async (options) => {
const {
config: { accessToken },
apiHost,
storeHash,
bundleZipPath,
jobId,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

const result = await themeApiClient.getJob({
accessToken,
apiHost,
Expand Down Expand Up @@ -271,11 +275,12 @@ utils.getChannels = async (options) => {
const {
config: { accessToken },
channelId,
apiHost,
storeHash,
applyTheme,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

if (!applyTheme || channelId) {
return options;
}
Expand All @@ -292,13 +297,14 @@ utils.getChannels = async (options) => {
utils.getVariations = async (options) => {
const {
config: { accessToken },
apiHost,
storeHash,
themeId,
applyTheme,
activate,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

if (!applyTheme) {
return options;
}
Expand Down Expand Up @@ -406,12 +412,13 @@ utils.requestToApplyVariationWithRetrys = () => {
utils.requestToApplyVariation = async (options) => {
const {
config: { accessToken },
apiHost,
storeHash,
variationId,
channelId,
} = options;

const apiHost = options.apiHost || options.config.apiHost;

if (options.applyTheme) {
await themeApiClient.activateThemeByVariationId({
accessToken,
Expand Down
2 changes: 1 addition & 1 deletion lib/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class StencilStart {

async getChannelUrl(stencilConfig, cliOptions) {
const { accessToken } = stencilConfig;
const { apiHost } = cliOptions;
const apiHost = cliOptions.apiHost || stencilConfig.apiHost;
const storeHash = await this._themeApiClient.getStoreHash({
storeUrl: stencilConfig.normalStoreUrl,
});
Expand Down

0 comments on commit 403d51c

Please sign in to comment.