UNPKG

ring-websites-toolbelt

Version:

Ring Publishing Platform tool to work with Ring Websites

96 lines (80 loc) 3.51 kB
const fs = require('fs'); const fse = require('fs-extra'); const ThemeScriptAbstract = require(`./../../ThemeScriptAbstract`); const retryPromise = require(`./../../utils/retryPromise`); class Deploy extends ThemeScriptAbstract { constructor(options) { super(options); this.compressedThemeFileName = 'compressedTheme.zip'; } async execute() { this.validateThemeJson(); this.loadThemeVarriables(); this.setUpProviders(); await this.deployTheme(); } async deployTheme() { console.info('Deploying theme...'); try { await this.files.compressDir(this.paths.theme, 'theme', this.compressedThemeFileName, ['.git', 'node_modules']); const ocdnUrl = await retryPromise(this.uploadCompressedThemeToOcdn.bind(this), 3); console.info('Compressed theme uploaded to ocdn:', ocdnUrl); await fse.remove(this.compressedThemeFileName); const deploymentId = await retryPromise(this.triggerDeployment.bind(this, ocdnUrl), 3); await this.checkIfThemeDeployed(deploymentId); console.info('Theme has been deployed:', this.themeJson.theme, this.themeJson.version); } catch(err) { throw err; }; } async uploadCompressedThemeToOcdn() { console.info('Uploading compressed theme to ocdn...'); try { return (await this.s3.uploadFinalTheme(this.compressedThemeFileName)).Location; } catch (error) { console.warn('Error while uploading theme to ocdn...', (error.message || error)); throw error; } } async triggerDeployment(ocdnUrl, version) { console.info('Deploying...'); try { return await this.api.deployTheme(ocdnUrl, version); } catch (error) { console.warn('Error while getting deployment ID:', (error.message || error)); throw error; } } async checkIfThemeDeployed(deploymentId) { console.info('Checking if theme has been deployed...', deploymentId); return new Promise((resolve, reject) => { const maxAttempts = 120; let currentAttemptsCount = 0; let result; let intervalId = setInterval(async () => { ++currentAttemptsCount; try { result = await this.api.checkIfThemeDeployed(deploymentId); } catch (err) {} if (result && result.finished) { clearInterval(intervalId); if (!result.error) { resolve(); } else { const errorMessage = '\n==================== Error in theme\'s deployment ======================' + '\n' + result.error + '\n' + '=======================================================================' reject(new Error(errorMessage)); } } else if (currentAttemptsCount > maxAttempts) { console.info('Error while checking if theme deployed - max attempts count exceeded.'); clearInterval(intervalId); reject(new Error('Error while checking if theme deployed - max attempts count exceeded.')); } else { console.info('Theme not yet deployed... Waiting...'); } }, 5000); }); } } module.exports = Deploy;