Skip to content

Commit

Permalink
feat: strf-7651 - update push command to take into account multiple c…
Browse files Browse the repository at this point in the history
…hannels
  • Loading branch information
MaxGenash committed Mar 12, 2021
1 parent b7261e4 commit 7582e7c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
8 changes: 7 additions & 1 deletion bin/stencil-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ const { printCliResultErrorAndExit } = require('../lib/cliCommon');

program
.version(PACKAGE_INFO.version)
.option('--host [hostname]', 'specify the api host', API_HOST)
.option('--host [hostname]', 'specify the api host')
.option('-f, --file [filename]', 'specify the filename of the bundle to upload')
.option('-s, --save [filename]', 'specify the filename to save the bundle as')
.option('-a, --activate [variationname]', 'specify the variation of the theme to activate')
.option('-d, --delete', 'delete oldest private theme if upload limit reached')
.option(
'-c, --channel_id [channelId]',
'specify the channel ID of the storefront to push the theme to',
parseInt,
)
.parse(process.argv);

checkNodeVersion();

const cliOptions = program.opts();
const options = {
apiHost: cliOptions.host || API_HOST,
channelId: cliOptions.channel_id,
bundleZipPath: cliOptions.file,
activate: cliOptions.activate,
saveBundleName: cliOptions.save,
Expand Down
2 changes: 2 additions & 0 deletions lib/stencil-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function stencilPush(options = {}, callback) {
utils.notifyUserOfThemeUploadCompletion,
utils.pollForJobCompletion((data) => ({ themeId: data.theme_id })),
utils.promptUserWhetherToApplyTheme,
utils.getChannels,
utils.promptUserForChannel,
utils.getVariations,
utils.promptUserForVariation,
utils.requestToApplyVariationWithRetrys(),
Expand Down
48 changes: 47 additions & 1 deletion lib/stencil-push.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ utils.promptUserWhetherToApplyTheme = async (options) => {
{
type: 'confirm',
name: 'applyTheme',
message: `Would you like to apply your theme to your store at ${options.config.normalStoreUrl}?`,
message: `Would you like to apply your theme to your store?`,
default: false,
},
];
Expand All @@ -277,6 +277,27 @@ utils.promptUserWhetherToApplyTheme = async (options) => {
return { ...options, ...answers };
};

utils.getChannels = async (options) => {
const {
config: { accessToken },
apiHost,
storeHash,
applyTheme,
} = options;

if (!applyTheme) {
return options;
}

const channels = await themeApiClient.getStoreChannels({
accessToken,
apiHost,
storeHash,
});

return { ...options, channels };
};

utils.getVariations = async (options) => {
const {
config: { accessToken },
Expand Down Expand Up @@ -320,6 +341,29 @@ utils.getVariations = async (options) => {
return { ...options, variations };
};

utils.promptUserForChannel = async (options) => {
const { applyTheme, channelId, channels } = options;

if (!applyTheme || channelId || channels.length < 2) {
return options;
}

const questions = [
{
type: 'list',
name: 'channelId',
message: 'Which channel would you like to apply the theme to?',
choices: channels.map((channel) => ({
name: channel.name,
value: channel.id,
})),
},
];
const answers = await Inquirer.prompt(questions);

return { ...options, channelId: answers.channelId };
};

utils.promptUserForVariation = async (options) => {
if (!options.applyTheme || options.variationId) {
return options;
Expand Down Expand Up @@ -365,6 +409,7 @@ utils.requestToApplyVariation = async (options) => {
apiHost,
storeHash,
variationId,
channelId,
} = options;

if (options.applyTheme) {
Expand All @@ -373,6 +418,7 @@ utils.requestToApplyVariation = async (options) => {
apiHost,
storeHash,
variationId,
channelId,
});
}

Expand Down
29 changes: 28 additions & 1 deletion lib/theme-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ const networkUtils = new NetworkUtils();
/**
* @param {object} options
* @param {string} options.variationId
* @param {string} options.channelId
* @param {string} options.apiHost
* @param {string} options.storeHash
* @param {string} options.accessToken
* @returns {Promise<any>}
*/
async function activateThemeByVariationId({ variationId, apiHost, storeHash, accessToken }) {
async function activateThemeByVariationId({
variationId,
channelId,
apiHost,
storeHash,
accessToken,
}) {
try {
return await networkUtils.sendApiRequest({
url: `${apiHost}/stores/${storeHash}/v3/themes/actions/activate`,
Expand All @@ -25,6 +32,7 @@ async function activateThemeByVariationId({ variationId, apiHost, storeHash, acc
accessToken,
data: {
variation_id: variationId,
channel_id: channelId,
which: 'original',
},
});
Expand Down Expand Up @@ -131,6 +139,24 @@ async function getChannelActiveTheme({ accessToken, apiHost, storeHash, channelI
);
}
}
/**
* @param {object} options
* @param {string} options.accessToken
* @param {string} options.apiHost
* @param {string} options.storeHash
* @returns {Promise<object[]>}
*/
async function getStoreChannels({ accessToken, apiHost, storeHash }) {
try {
const response = await networkUtils.sendApiRequest({
url: `${apiHost}/stores/${storeHash}/v3/channels?platform=bigcommerce&type=storefront&available=true`,
accessToken,
});
return response.data.data;
} catch (err) {
throw new Error(`Could not fetch a list of the store channels: ${err.message}`);
}
}

/**
* @param {object} options
Expand Down Expand Up @@ -273,6 +299,7 @@ async function downloadTheme({ accessToken, apiHost, storeHash, themeId }) {
module.exports = {
activateThemeByVariationId,
deleteThemeById,
getStoreChannels,
getChannelActiveTheme,
getJob,
getVariationsByThemeId,
Expand Down

0 comments on commit 7582e7c

Please sign in to comment.