diff --git a/lib/stencil-push.js b/lib/stencil-push.js index 97df0c55..63071eb4 100644 --- a/lib/stencil-push.js +++ b/lib/stencil-push.js @@ -13,6 +13,7 @@ function stencilPush(options = {}, callback) { utils.notifyUserOfThemeLimitReachedIfNecessary, utils.promptUserToDeleteThemesIfNecessary, utils.deleteThemesIfNecessary, + utils.checkIfDeletionIsComplete(), utils.uploadBundleAgainIfNecessary, utils.notifyUserOfThemeUploadCompletion, utils.pollForJobCompletion((data) => ({ themeId: data.theme_id })), diff --git a/lib/stencil-push.utils.js b/lib/stencil-push.utils.js index 68316bcb..b5bbdc43 100644 --- a/lib/stencil-push.utils.js +++ b/lib/stencil-push.utils.js @@ -190,6 +190,48 @@ utils.deleteThemesIfNecessary = async (options) => { return options; }; +utils.checkIfDeletionIsComplete = () => { + return async.retryable( + { + interval: 1000, + errorFilter: (err) => { + if (err.message === 'ThemeStillExists') { + console.log(`${'warning'.yellow} -- Theme still exists;Retrying ...`); + return true; + } + return false; + }, + times: 5, + }, + utils.checkIfThemeIsDeleted(), + ); +}; + +utils.checkIfThemeIsDeleted = () => async (options) => { + const { + themeLimitReached, + config: { accessToken }, + storeHash, + themeIdsToDelete, + } = options; + + if (!themeLimitReached) { + return options; + } + + const apiHost = options.apiHost || options.config.apiHost; + + const result = await themeApiClient.getThemes({ accessToken, apiHost, storeHash }); + + const themeStillExists = result.some((theme) => themeIdsToDelete.includes(theme.uuid)); + + if (themeStillExists) { + throw new Error('ThemeStillExists'); + } + + return { ...options, ...result }; +}; + utils.uploadBundleAgainIfNecessary = async (options) => { if (!options.themeLimitReached) { return options;