Skip to content

Commit

Permalink
fix: STRF-10081 stencil pull overrides only variation config (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc authored May 24, 2023
1 parent 0832bfc commit c0e1097
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/stencil-pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function stencilPull(options = {}, callback) {
stencilPushUtils.promptUserForChannel,
stencilPullUtils.getChannelActiveTheme,
stencilPullUtils.getThemeConfiguration,
stencilPullUtils.getCurrentVariation,
stencilPullUtils.mergeThemeConfiguration,
],
callback,
Expand Down
36 changes: 31 additions & 5 deletions lib/stencil-pull.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ utils.getChannelActiveTheme = async (options) => {
return { ...options, activeTheme };
};

utils.getCurrentVariation = async (options) => {
const {
config: { accessToken },
storeHash,
activeTheme,
saved,
remoteThemeConfiguration,
} = options;
const apiHost = options.apiHost || options.config.apiHost;
const themeId = activeTheme.active_theme_uuid;
const variations = await themeApiClient.getVariationsByThemeId({
accessToken,
apiHost,
storeHash,
themeId,
});
const variation = variations.find((v) => v.uuid === remoteThemeConfiguration.variation_uuid);

console.log('ok'.green + ` -- Fetched ${saved ? 'saved' : 'active'} variation name`);
return { ...options, variation };
};

utils.getThemeConfiguration = async (options) => {
const {
config: { accessToken },
Expand Down Expand Up @@ -59,6 +81,7 @@ utils.mergeThemeConfiguration = async (options) => {
const { remoteThemeConfiguration } = options;

const localConfig = await parseJsonFile('config.json');
const variation = localConfig.variations.find((v) => v.name === options.variation.name);
let diffDetected = false;

// For any keys the remote configuration has in common with the local configuration,
Expand All @@ -67,19 +90,22 @@ utils.mergeThemeConfiguration = async (options) => {
if (!(key in localConfig.settings)) {
continue;
}
const localVal = localConfig.settings[key];
const defaultVal = localConfig.settings[key];

// Check for different types, and throw an error if they are found
if (typeof localVal !== typeof remoteVal) {
if (typeof defaultVal !== typeof remoteVal) {
throw new Error(
`Theme configuration key "${key}" cannot be merged because it is not of the same type. ` +
`Remote configuration is of type ${typeof remoteVal} while local configuration is of type ${typeof localVal}.`,
`Remote configuration is of type ${typeof remoteVal} while local configuration is of type ${typeof defaultVal}.`,
);
}

// If a different value is found, overwrite the local config
if (!_.isEqual(localVal, remoteVal)) {
localConfig.settings[key] = remoteVal;
if (!_.isEqual(defaultVal, remoteVal)) {
if (!variation.settings) {
variation.settings = {};
}
variation.settings[key] = remoteVal;
diffDetected = true;
}
}
Expand Down
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.

0 comments on commit c0e1097

Please sign in to comment.