-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update "stencil pull" to use configurations API, improving perf…
…ormance
- Loading branch information
1 parent
741641a
commit 2b142fc
Showing
10 changed files
with
185 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,79 @@ | ||
const themeApiClient = require('./theme-api-client'); | ||
const tmp = require('tmp-promise'); | ||
const { extractZipFiles } = require('./archiveManager'); | ||
const { fetchFile } = require('./utils/networkUtils'); | ||
const fs = require('fs'); | ||
const _ = require('lodash'); | ||
|
||
const utils = {}; | ||
|
||
module.exports = utils; | ||
|
||
utils.selectActiveTheme = (options, callback) => { | ||
const [activeTheme] = options.themes.filter(theme => theme.is_active).map(theme => theme.uuid); | ||
utils.getChannelActiveTheme = async options => { | ||
const { config: { accessToken }, apiHost, storeHash, channelId } = options; | ||
|
||
callback(null, Object.assign({}, options, { activeTheme })); | ||
const activeTheme = await themeApiClient.getChannelActiveTheme({ accessToken, apiHost, storeHash, channelId }); | ||
|
||
console.log('ok'.green + ` -- Fetched theme details for channel ${channelId}`); | ||
|
||
return { ...options, activeTheme }; | ||
}; | ||
|
||
utils.startThemeDownloadJob = async options => { | ||
const { config: { accessToken }, apiHost, activeTheme, storeHash } = options; | ||
utils.getThemeConfiguration = async options => { | ||
const { config: { accessToken }, apiHost, storeHash, activeTheme, saved } = options; | ||
|
||
const themeId = activeTheme.active_theme_uuid; | ||
|
||
const configurationId = saved ? activeTheme.saved_theme_configuration_uuid | ||
: activeTheme.active_theme_configuration_uuid; | ||
|
||
const { jobId } = await themeApiClient.downloadTheme({ | ||
accessToken: accessToken, | ||
themeId: activeTheme, | ||
apiHost, | ||
storeHash, | ||
}); | ||
const remoteThemeConfiguration = await themeApiClient.getThemeConfiguration({ accessToken, apiHost, storeHash, | ||
themeId, configurationId }); | ||
|
||
return { | ||
...options, | ||
jobId, | ||
}; | ||
console.log('ok'.green + ` -- Fetched ${saved ? 'saved' : 'active'} configuration`); | ||
|
||
return { ...options, remoteThemeConfiguration }; | ||
}; | ||
|
||
utils.downloadThemeConfig = async options => { | ||
const { path: tempThemePath, cleanup } = await tmp.file(); | ||
utils.mergeThemeConfiguration = async options => { | ||
const { remoteThemeConfiguration } = options; | ||
|
||
try { | ||
await fetchFile(options.downloadUrl, tempThemePath); | ||
} catch (err) { | ||
throw new Error(`Unable to download theme config from ${options.downloadUrl}: ${err.message}`); | ||
} | ||
let rawConfig = fs.readFileSync('config.json'); | ||
let parsedConfig = JSON.parse(rawConfig); | ||
let diffDetected = false; | ||
|
||
console.log('ok'.green + ' -- Theme files downloaded'); | ||
console.log('ok'.green + ' -- Extracting theme config'); | ||
// For any keys the remote configuration has in common with the local configuration, | ||
// overwrite the local configuration if the remote configuration differs | ||
for (const [key, value] of Object.entries(remoteThemeConfiguration.settings)) { | ||
if (key in parsedConfig.settings) { | ||
// Check for different types, and throw an error if they are found | ||
if (typeof parsedConfig.settings[key] !== typeof value) { | ||
throw new Error(`Theme configuration key ${key} cannot be merged because it is not of the same type. Remote configuration is of type ${typeof value} while local configuration is of type ${typeof parsedConfig.settings[key]}.`); | ||
} | ||
|
||
const outputNames = { | ||
'config.json': options.saveConfigName, | ||
}; | ||
await extractZipFiles({ zipPath: tempThemePath, fileToExtract: 'config.json', outputNames }); | ||
// If a different value is found, overwrite the local config | ||
if (!_.isEqual(parsedConfig.settings[key], value)) { | ||
parsedConfig.settings[key] = value; | ||
diffDetected = true; | ||
} | ||
} | ||
} | ||
|
||
console.log('ok'.green + ' -- Theme config extracted'); | ||
// Does a file need to be written? | ||
if (diffDetected || options.saveConfigName !== 'config.json') { | ||
if (diffDetected) { | ||
console.log('ok'.green + ' -- Remote configuration merged with local configuration'); | ||
} else { | ||
console.log('ok'.green + ' -- Remote and local configurations are in sync for all common keys'); | ||
} | ||
|
||
await cleanup(); | ||
fs.writeFile(options.saveConfigName, JSON.stringify(parsedConfig, null, 2), function(err) { | ||
if(err) { | ||
console.error(err); | ||
} else { | ||
console.log('ok'.green + ` -- Configuration written to ${options.saveConfigName}`); | ||
} | ||
}); | ||
} else { | ||
console.log('ok'.green + ` -- Remote and local configurations are in sync for all common keys, no action taken`); | ||
} | ||
|
||
return options; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters