From dc4233b99ac5a6c8233b72c9b125bd032481a546 Mon Sep 17 00:00:00 2001 From: Rahul kishan M Date: Tue, 4 Jun 2024 14:27:29 +0530 Subject: [PATCH 1/2] Update omniscript.js --- lib/datapacktypes/omniscript.js | 229 +++++++++++++++++--------------- 1 file changed, 123 insertions(+), 106 deletions(-) diff --git a/lib/datapacktypes/omniscript.js b/lib/datapacktypes/omniscript.js index f4e105f8..7f970485 100644 --- a/lib/datapacktypes/omniscript.js +++ b/lib/datapacktypes/omniscript.js @@ -292,126 +292,143 @@ OmniScript.prototype.compileOmniScriptLwc = async function (jobInfo, omniScriptI } OmniScript.prototype.compileOSLWC = async function (jobInfo, omniScriptId, omniScriptKey) { - try { - - let puppeteerOptions = await utilityservice.prototype.getPuppeteerOptions(jobInfo); - - VlocityUtils.verbose('Deployed OmniScript ID', omniScriptKey + ' (' + omniScriptId + ')'); - - if (!puppeteerOptions.executablePath && !jobInfo.puppeteerInstalled) { - VlocityUtils.error('Chromium not installed. LWC activation disabled. Run "npm install puppeteer -g" or set puppeteerExecutablePath in your Job File'); - jobInfo.ignoreLWCActivationOS = true; - jobInfo.ignoreLWCActivationCards = true; - } else { - var package = this.vlocity.namespacePrefix; - - var siteUrl = this.vlocity.jsForceConnection.instanceUrl; - var sessionToken = this.vlocity.jsForceConnection.accessToken; - var loginURl = siteUrl + '/secur/frontdoor.jsp?sid=' + sessionToken; - VlocityUtils.verbose('LWC Activation Login URL', loginURl); - var browser; - + const _compileOSLWC = async (jobInfo, omniScriptId, omniScriptKey) => { + const promiseJob = new Promise(async (res,rej)=>{ try { - browser = await puppeteer.launch(puppeteerOptions); - } catch (error) { - VlocityUtils.error('Puppeteer initialization Failed, LWC Activation disabled - ' + error); - jobInfo.ignoreLWCActivationOS = true; - return; - } - - const page = await browser.newPage(); - const loginTimeout = jobInfo.loginTimeoutForLoginLWC; - - await Promise.all([ - page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'load' }), - page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'networkidle2'}), - page.goto(loginURl, {timeout: loginTimeout}) - ]); - - var omniScriptDisignerpageLink = siteUrl + '/apex/' + package + 'OmniLwcCompile?id=' + omniScriptId + '&activate=true'; - var omniScriptLogId = omniScriptKey + ' (' + omniScriptId + ')'; - - VlocityUtils.report('Starting OmniScript LWC Activation', omniScriptLogId); - VlocityUtils.verbose('LWC Activation URL', omniScriptDisignerpageLink); - - await page.goto(omniScriptDisignerpageLink); - await page.waitForTimeout(5000); - - - let tries = 0; - var errorMessage; - var maxNumOfTries = Math.ceil((60/jobInfo.defaultLWCPullTimeInSeconds)*jobInfo.defaultMinToWaitForLWCOmniScript); - while (tries < maxNumOfTries && !jobInfo.ignoreLWCActivationOS) { - try { - let message; + let puppeteerOptions = await utilityservice.prototype.getPuppeteerOptions(jobInfo); + + VlocityUtils.verbose('Deployed OmniScript ID', omniScriptKey + ' (' + omniScriptId + ')'); + + if (!puppeteerOptions.executablePath && !jobInfo.puppeteerInstalled) { + VlocityUtils.error('Chromium not installed. LWC activation disabled. Run "npm install puppeteer -g" or set puppeteerExecutablePath in your Job File'); + jobInfo.ignoreLWCActivationOS = true; + jobInfo.ignoreLWCActivationCards = true; + resolve(); + return; + } else { + var package = this.vlocity.namespacePrefix; + + var siteUrl = this.vlocity.jsForceConnection.instanceUrl; + var sessionToken = this.vlocity.jsForceConnection.accessToken; + var loginURl = siteUrl + '/secur/frontdoor.jsp?sid=' + sessionToken; + VlocityUtils.verbose('LWC Activation Login URL', loginURl); + var browser; + try { - message = await page.waitForSelector('#compiler-message'); - } catch (messageTimeout) { - VlocityUtils.verbose('Error', messageTimeout); - VlocityUtils.log(omniScriptKey, 'Loading Page taking too long - Retrying - Tries: ' + tries + ' of ' + maxNumOfTries); + locateChrome(function(l) { + console.log(l); + }); + browser = await puppeteer.launch({...puppeteerOptions, executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"}); + } catch (error) { + VlocityUtils.error('Puppeteer initialization Failed, LWC Activation disabled - ' + error); + jobInfo.ignoreLWCActivationOS = true; + resolve(); + return; } - - if (message) { - let currentStatus = await message.evaluate(node => node.innerText); - VlocityUtils.report('Activating LWC for OmniScript', omniScriptLogId, currentStatus); - jobInfo.elapsedTime = VlocityUtils.getTime(); - VlocityUtils.report('Elapsed Time', jobInfo.elapsedTime); - if (currentStatus === 'DONE') { - VlocityUtils.success('LWC Activated', omniScriptLogId); - break; - } else if (/^ERROR: No MODULE named markup/.test(currentStatus)) { - var missingLWCTrimedError = currentStatus.substring('ERROR: '.length, currentStatus.indexOf(' found :')); - errorMessage = ' Missing Custom LWC - ' + missingLWCTrimedError; - break; - } else if (/^ERROR/.test(currentStatus)) { - errorMessage = ' Error Activating LWC - ' + currentStatus; - break; + + const page = await browser.newPage(); + const loginTimeout = jobInfo.loginTimeoutForLoginLWC; + + await Promise.all([ + page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'load' }), + page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'networkidle2' }), + page.goto(loginURl, { timeout: loginTimeout }) + ]); + + var omniScriptDisignerpageLink = siteUrl + '/apex/' + package + 'OmniLwcCompile?id=' + omniScriptId + '&activate=true';//0jNTC0000000VGk2AM + var omniScriptLogId = omniScriptKey + ' (' + omniScriptId + ')'; + + VlocityUtils.report('Starting OmniScript LWC Activation', omniScriptLogId); + VlocityUtils.verbose('LWC Activation URL', omniScriptDisignerpageLink); + + await page.goto(omniScriptDisignerpageLink); + await page.waitForTimeout(5000); + + let tries = 0; + var errorMessage; + var maxNumOfTries = Math.ceil((60 / jobInfo.defaultLWCPullTimeInSeconds) * jobInfo.defaultMinToWaitForLWCOmniScript); + while (tries < maxNumOfTries && !jobInfo.ignoreLWCActivationOS) { + try { + let message; + try { + message = await page.waitForSelector('#compiler-message'); + } catch (messageTimeout) { + VlocityUtils.verbose('Error', messageTimeout); + VlocityUtils.log(omniScriptKey, 'Loading Page taking too long - Retrying - Tries: ' + tries + ' of ' + maxNumOfTries); + } + + if (message) { + let currentStatus = await message.evaluate(node => node.innerText); + VlocityUtils.report('Activating LWC for OmniScript', omniScriptLogId, currentStatus); + jobInfo.elapsedTime = VlocityUtils.getTime(); + VlocityUtils.report('Elapsed Time', jobInfo.elapsedTime); + if (currentStatus === 'DONE') { + VlocityUtils.success('LWC Activated', omniScriptLogId); + break; + } else if (/^ERROR: No MODULE named markup/.test(currentStatus)) { + var missingLWCTrimedError = currentStatus.substring('ERROR: '.length, currentStatus.indexOf(' found :')); + errorMessage = ' Missing Custom LWC - ' + missingLWCTrimedError; + break; + } else if (/^ERROR/.test(currentStatus)) { + errorMessage = ' Error Activating LWC - ' + currentStatus; + break; + } + } + } catch (e) { + VlocityUtils.error('Error Activating LWC', omniScriptLogId, e); + errorMessage = ' Error: ' + e; + } + tries++; + await page.waitForTimeout(jobInfo.defaultLWCPullTimeInSeconds * 1000); + } + + if (tries == maxNumOfTries) { + errorMessage = 'Activation took longer than ' + jobInfo.defaultMinToWaitForLWCOmniScript + ' minutes - Aborting'; + } + + if (errorMessage) { + if (!jobInfo.omniScriptLwcActivationSkip) { + jobInfo.omniScriptLwcActivationSkip = {}; } + jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage; + jobInfo.hasError = true; + jobInfo.currentStatus[omniScriptKey] = 'Error'; + jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage; + jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage); + VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + errorMessage); + await this.deactivateOmniScript(omniScriptId); } - } catch (e) { - VlocityUtils.error('Error Activating LWC', omniScriptLogId, e); - errorMessage = ' Error: ' + e; } - tries++; - await page.waitForTimeout(jobInfo.defaultLWCPullTimeInSeconds*1000); - } - - if (tries == maxNumOfTries) { - errorMessage = 'Activation took longer than ' + jobInfo.defaultMinToWaitForLWCOmniScript + ' minutes - Aborting'; - } - - if (errorMessage) { + } catch (e) { + VlocityUtils.error(e); + if (!jobInfo.omniScriptLwcActivationSkip) { jobInfo.omniScriptLwcActivationSkip = {}; } jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage; jobInfo.hasError = true; jobInfo.currentStatus[omniScriptKey] = 'Error'; - jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage; - jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage); - VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + errorMessage); - await this.deactivateOmniScript(omniScriptId); + jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + e; + jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + e); + VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + e); + await this.deactivateOmniScript(omniScriptId, jobInfo); + + } finally { + if (browser) { + await browser.close(); + } } - browser.close(); - } - } catch (e) { - VlocityUtils.error(e); - - if (!jobInfo.omniScriptLwcActivationSkip) { - jobInfo.omniScriptLwcActivationSkip = {}; - } - jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage; - jobInfo.hasError = true; - jobInfo.currentStatus[omniScriptKey] = 'Error'; - jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + e; - jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + e); - VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + e); - await this.deactivateOmniScript(omniScriptId, jobInfo); - - try { - browser.close(); - } catch (e2) {} + res() + }); + return new Promise(async(res,rej)=>{ + await promiseJob + await promiseJob //re-activating to make sure it gets activated + res() + }); } + lastPromise = lastPromise.then(()=>_compileOSLWC(jobInfo, omniScriptId, omniScriptKey)) + await lastPromise; + } OmniScript.prototype.onActivateError = async function (dataPackData) { From c0ec059501598a45e725c734c612c41ee47c4f99 Mon Sep 17 00:00:00 2001 From: Rahul kishan M Date: Wed, 19 Jun 2024 06:40:19 +0000 Subject: [PATCH 2/2] Puppeter additional hack check --- lib/datapacktypes/omniscript.js | 141 +----------------------- lib/puppeteerHelper/browserHelper.js | 92 ++++++++++++++++ lib/puppeteerHelper/compileOSLWC.js | 66 +++++++++++ lib/puppeteerHelper/omniScriptHelper.js | 69 ++++++++++++ lib/puppeteerHelper/puppeteerHelper.js | 36 ++++++ package.json | 2 +- 6 files changed, 268 insertions(+), 138 deletions(-) create mode 100644 lib/puppeteerHelper/browserHelper.js create mode 100644 lib/puppeteerHelper/compileOSLWC.js create mode 100644 lib/puppeteerHelper/omniScriptHelper.js create mode 100644 lib/puppeteerHelper/puppeteerHelper.js diff --git a/lib/datapacktypes/omniscript.js b/lib/datapacktypes/omniscript.js index 7f970485..5561d7c7 100644 --- a/lib/datapacktypes/omniscript.js +++ b/lib/datapacktypes/omniscript.js @@ -4,6 +4,7 @@ var puppeteer = require('puppeteer-core'); var fs = require('fs-extra'); var path = require('path'); var yaml = require('js-yaml'); +var compileOSLWCJob = require('../puppeteerHelper/compileOSLWC') var OmniScript = module.exports = function (vlocity) { this.vlocity = vlocity; @@ -291,145 +292,11 @@ OmniScript.prototype.compileOmniScriptLwc = async function (jobInfo, omniScriptI } } +lastPromise = Promise.resolve(); OmniScript.prototype.compileOSLWC = async function (jobInfo, omniScriptId, omniScriptKey) { - const _compileOSLWC = async (jobInfo, omniScriptId, omniScriptKey) => { - const promiseJob = new Promise(async (res,rej)=>{ - try { - let puppeteerOptions = await utilityservice.prototype.getPuppeteerOptions(jobInfo); - - VlocityUtils.verbose('Deployed OmniScript ID', omniScriptKey + ' (' + omniScriptId + ')'); - - if (!puppeteerOptions.executablePath && !jobInfo.puppeteerInstalled) { - VlocityUtils.error('Chromium not installed. LWC activation disabled. Run "npm install puppeteer -g" or set puppeteerExecutablePath in your Job File'); - jobInfo.ignoreLWCActivationOS = true; - jobInfo.ignoreLWCActivationCards = true; - resolve(); - return; - } else { - var package = this.vlocity.namespacePrefix; - - var siteUrl = this.vlocity.jsForceConnection.instanceUrl; - var sessionToken = this.vlocity.jsForceConnection.accessToken; - var loginURl = siteUrl + '/secur/frontdoor.jsp?sid=' + sessionToken; - VlocityUtils.verbose('LWC Activation Login URL', loginURl); - var browser; - - try { - locateChrome(function(l) { - console.log(l); - }); - browser = await puppeteer.launch({...puppeteerOptions, executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"}); - } catch (error) { - VlocityUtils.error('Puppeteer initialization Failed, LWC Activation disabled - ' + error); - jobInfo.ignoreLWCActivationOS = true; - resolve(); - return; - } - - const page = await browser.newPage(); - const loginTimeout = jobInfo.loginTimeoutForLoginLWC; - - await Promise.all([ - page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'load' }), - page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'networkidle2' }), - page.goto(loginURl, { timeout: loginTimeout }) - ]); - - var omniScriptDisignerpageLink = siteUrl + '/apex/' + package + 'OmniLwcCompile?id=' + omniScriptId + '&activate=true';//0jNTC0000000VGk2AM - var omniScriptLogId = omniScriptKey + ' (' + omniScriptId + ')'; - - VlocityUtils.report('Starting OmniScript LWC Activation', omniScriptLogId); - VlocityUtils.verbose('LWC Activation URL', omniScriptDisignerpageLink); - - await page.goto(omniScriptDisignerpageLink); - await page.waitForTimeout(5000); - - let tries = 0; - var errorMessage; - var maxNumOfTries = Math.ceil((60 / jobInfo.defaultLWCPullTimeInSeconds) * jobInfo.defaultMinToWaitForLWCOmniScript); - while (tries < maxNumOfTries && !jobInfo.ignoreLWCActivationOS) { - try { - let message; - try { - message = await page.waitForSelector('#compiler-message'); - } catch (messageTimeout) { - VlocityUtils.verbose('Error', messageTimeout); - VlocityUtils.log(omniScriptKey, 'Loading Page taking too long - Retrying - Tries: ' + tries + ' of ' + maxNumOfTries); - } - - if (message) { - let currentStatus = await message.evaluate(node => node.innerText); - VlocityUtils.report('Activating LWC for OmniScript', omniScriptLogId, currentStatus); - jobInfo.elapsedTime = VlocityUtils.getTime(); - VlocityUtils.report('Elapsed Time', jobInfo.elapsedTime); - if (currentStatus === 'DONE') { - VlocityUtils.success('LWC Activated', omniScriptLogId); - break; - } else if (/^ERROR: No MODULE named markup/.test(currentStatus)) { - var missingLWCTrimedError = currentStatus.substring('ERROR: '.length, currentStatus.indexOf(' found :')); - errorMessage = ' Missing Custom LWC - ' + missingLWCTrimedError; - break; - } else if (/^ERROR/.test(currentStatus)) { - errorMessage = ' Error Activating LWC - ' + currentStatus; - break; - } - } - } catch (e) { - VlocityUtils.error('Error Activating LWC', omniScriptLogId, e); - errorMessage = ' Error: ' + e; - } - tries++; - await page.waitForTimeout(jobInfo.defaultLWCPullTimeInSeconds * 1000); - } - - if (tries == maxNumOfTries) { - errorMessage = 'Activation took longer than ' + jobInfo.defaultMinToWaitForLWCOmniScript + ' minutes - Aborting'; - } - - if (errorMessage) { - if (!jobInfo.omniScriptLwcActivationSkip) { - jobInfo.omniScriptLwcActivationSkip = {}; - } - jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage; - jobInfo.hasError = true; - jobInfo.currentStatus[omniScriptKey] = 'Error'; - jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage; - jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage); - VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + errorMessage); - await this.deactivateOmniScript(omniScriptId); - } - } - } catch (e) { - VlocityUtils.error(e); - - if (!jobInfo.omniScriptLwcActivationSkip) { - jobInfo.omniScriptLwcActivationSkip = {}; - } - jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage; - jobInfo.hasError = true; - jobInfo.currentStatus[omniScriptKey] = 'Error'; - jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + e; - jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + e); - VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + e); - await this.deactivateOmniScript(omniScriptId, jobInfo); - - } finally { - if (browser) { - await browser.close(); - } - } - res() - }); - return new Promise(async(res,rej)=>{ - await promiseJob - await promiseJob //re-activating to make sure it gets activated - res() - }); - } - lastPromise = lastPromise.then(()=>_compileOSLWC(jobInfo, omniScriptId, omniScriptKey)) + lastPromise = lastPromise.then(() => compileOSLWCJob(jobInfo, omniScriptId, omniScriptKey, async (omniscript)=> await this.deactivateOmniScript(omniscript), this.vlocity)); await lastPromise; - -} +}; OmniScript.prototype.onActivateError = async function (dataPackData) { var onActivateErrorResult = {}, diff --git a/lib/puppeteerHelper/browserHelper.js b/lib/puppeteerHelper/browserHelper.js new file mode 100644 index 00000000..4bba54ab --- /dev/null +++ b/lib/puppeteerHelper/browserHelper.js @@ -0,0 +1,92 @@ +exports.goToUniquePage = async (page, siteUrl, packageNamespace, omniScriptId) => { + const omniScriptDesignerPageLink = `${siteUrl}/lightning/cmp/${packageNamespace}OmniDesignerAuraWrapper?c__recordId=${omniScriptId}`; + await page.goto(omniScriptDesignerPageLink, { timeout: 900000, waitUntil: 'networkidle2' }); +}; + +exports.clickPreviewButton = async (page, timeout) => { + await page.mouse.click(1127, 134); +}; + +exports.clickDeactivate = async (page) => { + let pageChanged = false; + page.on('framenavigated', frame => { + if (frame === page.mainFrame()) pageChanged = true; + }); + + await page.mouse.click(1358, 134); + await new Promise(res => setTimeout(res, 15000)); + await page.mouse.click(1042, 572); + await new Promise(res => setTimeout(res, 5000)); + + if (!pageChanged) { + await new Promise(res => setTimeout(res, 5000)); + await page.keyboard.down('Tab'); + await page.keyboard.up('Tab'); + } + await page.keyboard.down('Tab'); + await page.keyboard.up('Tab'); + await page.keyboard.down('Enter'); + await page.keyboard.up('Enter'); + + if (pageChanged) { + await new Promise(res => setTimeout(res, 10000)); + await page.keyboard.down('Tab'); + await page.keyboard.up('Tab'); + await page.keyboard.down('Enter'); + await page.keyboard.up('Enter'); + } +}; + +exports.clickActivate = async (page) => { + let pageChanged = false; + page.on('framenavigated', frame => { + if (frame === page.mainFrame()) pageChanged = true; + }); + + await page.mouse.click(1358, 134); + await new Promise(res => setTimeout(res, 5000)); + await page.mouse.click(1042, 572); + await new Promise(res => setTimeout(res, 25000)); + + if (!pageChanged) { + await new Promise(res => setTimeout(res, 5000)); + await page.keyboard.down('Tab'); + await page.keyboard.up('Tab'); + } + await page.keyboard.down('Tab'); + await page.keyboard.up('Tab'); + await page.keyboard.down('Enter'); + await page.keyboard.up('Enter'); + await page.mouse.click(1054, 670); + + if (pageChanged) { + await new Promise(res => setTimeout(res, 7000)); + await page.keyboard.down('Tab'); + await page.keyboard.up('Tab'); + await page.keyboard.down('Enter'); + await page.keyboard.up('Enter'); + } +}; + + +exports.verifyOmniscriptActivation = async (page) => { + const getShadowElement = async (element, selector) => { + return await element.evaluateHandle((el, sel) => el.shadowRoot.querySelector(sel), selector); + }; + + const canvasWebComponentHandle = await page.$('omnistudio-omni-designer-canvas'); + if (!canvasWebComponentHandle) throw new Error('omnistudio-omni-designer-canvas element not found'); + + const designerCanvasHandle = await getShadowElement(canvasWebComponentHandle, 'c-omni-designer-canvas-body'); + if (!designerCanvasHandle) throw new Error('c-omni-designer-canvas-body element not found'); + + const designerCanvasBodyHandle = await getShadowElement(designerCanvasHandle, 'c-omni-designer-preview'); + if (!designerCanvasBodyHandle) throw new Error('c-omni-designer-preview element not found'); + + const iframeHandle = await getShadowElement(designerCanvasBodyHandle, 'iframe'); + if (!iframeHandle) throw new Error('iframe element not found'); + + const contentFrame = await iframeHandle.contentFrame(); + const errorElement = await contentFrame.$('.slds-text-color_error'); + return !errorElement; +}; \ No newline at end of file diff --git a/lib/puppeteerHelper/compileOSLWC.js b/lib/puppeteerHelper/compileOSLWC.js new file mode 100644 index 00000000..38305ed9 --- /dev/null +++ b/lib/puppeteerHelper/compileOSLWC.js @@ -0,0 +1,66 @@ +const utilityservice = require('../utilityservice.js'); +const { launchBrowser, loginToBrowser,checkChromiumInstallation } = require('../puppeteerHelper/puppeteerHelper.js'); +const { goToUniquePage, clickPreviewButton, clickDeactivate, clickActivate, verifyOmniscriptActivation } = require('../puppeteerHelper/browserHelper.js'); +const { activateOmniScript, handleErrorAndDeactivate } = require('../puppeteerHelper/omniScriptHelper.js'); + +const compileOSLWCJob = async (jobInfo, omniScriptId, omniScriptKey, deactivateOmniScript, vlocity) => { + let browser; + + try { + const puppeteerOptions = await utilityservice.prototype.getPuppeteerOptions(jobInfo); + VlocityUtils.report('Deployed OmniScript ID', omniScriptKey + ' (' + omniScriptId + ')'); + + if (!checkChromiumInstallation(puppeteerOptions, jobInfo)) return; + const packageNamespace = vlocity.namespacePrefix; + const siteUrl = vlocity.jsForceConnection.instanceUrl; + const sessionToken = vlocity.jsForceConnection.accessToken; + const loginUrl = `${siteUrl}/secur/frontdoor.jsp?sid=${sessionToken}`; + VlocityUtils.verbose('LWC Activation Login URL', loginUrl); + + browser = await launchBrowser(puppeteerOptions); + if (!browser) { + jobInfo.ignoreLWCActivationOS = true; + return; + } + + const page = await loginToBrowser(browser, loginUrl, jobInfo); + await page.setViewport({ width: 1520, height: 1000 }); + + const errorMessage = await activateOmniScript(page, siteUrl, packageNamespace, omniScriptId, omniScriptKey, jobInfo); + if (errorMessage) { + await handleErrorAndDeactivate(errorMessage, jobInfo, omniScriptId, omniScriptKey,deactivateOmniScript); + return; + } + + await new Promise(resolve => setTimeout(resolve, 5000)); + await goToUniquePage(page, siteUrl, packageNamespace, omniScriptId); + await new Promise(resolve => setTimeout(resolve, 20000)); + await clickPreviewButton(page, 15000); + await new Promise(resolve => setTimeout(resolve, 10000)); + const omniScriptLogId = `${omniScriptKey} (${omniScriptId})`; + const enableReactivate = await verifyOmniscriptActivation(page); + if (enableReactivate) { + await clickDeactivate(page); + await new Promise(resolve => setTimeout(resolve, 10000)); + await clickActivate(page); + await new Promise(resolve => setTimeout(resolve, 10000)); + + const omniscriptIsDeployedProperly = await verifyOmniscriptActivation(page); + if (!omniscriptIsDeployedProperly) { + throw new Error(`Even after reactivating we found ${omniScriptKey} has a text Error after click preview`); + } + VlocityUtils.success('LWC Activated_Manually', omniScriptLogId); + return + } + VlocityUtils.success('LWC Activated_Manually wasnt required', omniScriptLogId); + } catch (e) { + const omniScriptLogId = `${omniScriptKey} (${omniScriptId})`; + VlocityUtils.error(e); + VlocityUtils.error('LWC Activated_Manually Error', omniScriptLogId + ' - ' + e); + await handleErrorAndDeactivate(e, jobInfo, omniScriptId, omniScriptKey,deactivateOmniScript); + } finally { + if (browser) await browser.close(); + } +}; + +module.exports = compileOSLWCJob; \ No newline at end of file diff --git a/lib/puppeteerHelper/omniScriptHelper.js b/lib/puppeteerHelper/omniScriptHelper.js new file mode 100644 index 00000000..2857f393 --- /dev/null +++ b/lib/puppeteerHelper/omniScriptHelper.js @@ -0,0 +1,69 @@ + +exports.activateOmniScript = async (page, siteUrl, packageNamespace, omniScriptId, omniScriptKey, jobInfo) => { + const omniScriptDesignerPageLink = `${siteUrl}/apex/${packageNamespace}OmniLwcCompile?id=${omniScriptId}&activate=true`; + const omniScriptLogId = `${omniScriptKey} (${omniScriptId})`; + + VlocityUtils.report('Starting OmniScript LWC Activation', omniScriptLogId); + VlocityUtils.verbose('LWC Activation URL', omniScriptDesignerPageLink); + + await page.goto(omniScriptDesignerPageLink, { timeout: 900000, waitUntil: 'networkidle0' }); + await new Promise(resolve => setTimeout(resolve, 5000)); + + let tries = 0; + let errorMessage; + const maxNumOfTries = Math.ceil((60 / jobInfo.defaultLWCPullTimeInSeconds) * jobInfo.defaultMinToWaitForLWCOmniScript); + + while (tries < maxNumOfTries && !jobInfo.ignoreLWCActivationOS) { + try { + let message; + try { + message = await page.waitForSelector('#compiler-message'); + } catch (messageTimeout) { + VlocityUtils.verbose('Error', messageTimeout); + console.log(omniScriptKey, 'Loading Page taking too long - Retrying - Tries: ' + tries + ' of ' + maxNumOfTries); + } + + if (message) { + const currentStatus = await message.evaluate(node => node.innerText); + VlocityUtils.report('Activating LWC for OmniScript', omniScriptLogId, currentStatus); + jobInfo.elapsedTime = VlocityUtils.report('Elapsed Time', jobInfo.elapsedTime); + + if (currentStatus === 'DONE') { + VlocityUtils.success('LWC Activated', omniScriptLogId); + break; + } else if (/^ERROR: No MODULE named markup/.test(currentStatus)) { + errorMessage = 'Missing Custom LWC - ' + currentStatus.substring('ERROR: '.length, currentStatus.indexOf(' found :')); + break; + } else if (/^ERROR/.test(currentStatus)) { + errorMessage = 'Error Activating LWC - ' + currentStatus; + break; + } + } + } catch (e) { + VlocityUtils.error('Error Activating LWC', omniScriptLogId, e); + errorMessage = 'Error: ' + e; + } + tries++; + await new Promise(resolve => setTimeout(resolve, jobInfo.defaultLWCPullTimeInSeconds * 1000)); + } + + if (tries === maxNumOfTries) { + errorMessage = 'Activation took longer than ' + jobInfo.defaultMinToWaitForLWCOmniScript + ' minutes - Aborting'; + } + + return errorMessage; +}; + +exports.handleErrorAndDeactivate = async (errorMessage, jobInfo, omniScriptId, omniScriptKey, deactivateOmniScript) => { + if (!jobInfo.omniScriptLwcActivationSkip) { + jobInfo.omniScriptLwcActivationSkip = {}; + } + jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage; + jobInfo.hasError = true; + jobInfo.currentStatus[omniScriptKey] = 'Error'; + jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage; + jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage); + VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + errorMessage); + await deactivateOmniScript(omniScriptId); +}; + diff --git a/lib/puppeteerHelper/puppeteerHelper.js b/lib/puppeteerHelper/puppeteerHelper.js new file mode 100644 index 00000000..81175f65 --- /dev/null +++ b/lib/puppeteerHelper/puppeteerHelper.js @@ -0,0 +1,36 @@ +const puppeteer = require('puppeteer-core'); + +exports.getPuppeteerOptions = async (jobInfo) => { + return await jobInfo.getPuppeteerOptions(); +}; + +exports.launchBrowser = async (puppeteerOptions) => { + try { + // return await puppeteer.launch({ ...puppeteerOptions, executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" }); + return await puppeteer.launch(puppeteerOptions); + } catch (error) { + VlocityUtils.error('Puppeteer initialization Failed, LWC Activation disabled - ' + error); + return null; + } +}; + +exports.loginToBrowser = async (browser, loginUrl, jobInfo) => { + const page = await browser.newPage(); + const loginTimeout = jobInfo.loginTimeoutForLoginLWC; + await Promise.all([ + page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'load' }), + page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'networkidle2' }), + page.goto(loginUrl, { timeout: loginTimeout }) + ]); + return page; +}; + +exports.checkChromiumInstallation = (puppeteerOptions, jobInfo) => { + if (!puppeteerOptions.executablePath && !jobInfo.puppeteerInstalled) { + VlocityUtils.error('Chromium not installed. LWC activation disabled. Run "npm install puppeteer -g" or set puppeteerExecutablePath in your Job File'); + jobInfo.ignoreLWCActivationOS = true; + jobInfo.ignoreLWCActivationCards = true; + return false; + } + return true; +} \ No newline at end of file diff --git a/package.json b/package.json index c5456ff1..d65076a6 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "nopt": "4.0.1", "opn": "6.0.0", "properties": "1.2.1", - "puppeteer-core": "^5.3.1", + "puppeteer-core": "^22.10.0", "runtime-plugin-manager-clone": "0.1.0", "salesforce-alm": "49.5.0", "sass.js": "0.11.1",