import { flags , SfdxCommand } from "@salesforce/command" ; import { Messages } from '@salesforce/core' ; import { SfdxError } from "@salesforce/core" ; import * as puppeteer from "puppeteer" ; Messages . importMessagesDirectory ( __dirname ) ; const messages = Messages . loadMessages ( "texei-sfdx-plugin" , "sharingcalc-resume" ) ; const mapSharingLabel = new Map ( [ [ 'sharingRule' , 'Sharing Rule' ] , [ 'groupMembership' , 'Group Membership' ] ] ) ; export default class Resume extends SfdxCommand { public static description = messages . getMessage ( "commandDescription" ) ; public static examples = [ `$ sfdx texei:sharingcalc:resume" \nSharing calculations resumed\n` ] ; protected static flagsConfig = { scope : flags . string ( { char : "s" , description : messages . getMessage ( "scopeFlagDescription" ) , required : false , options : [ "sharingRule" , "groupMembership" ] , default : "sharingRule" } ) , timeout : flags . number ( { char : "t" , description : messages . getMessage ( "commandTimeout" ) , required : false , default : 120000 } ) } ; protected static requiresUsername = true ; protected static requiresDevhubUsername = false ; protected static requiresProject = false ; private timeoutHandler = null ; public async run ( ) { let result = { } ; this . timeoutHandler = setTimeout ( ( ) => { if ( this . timeoutHandler ) { throw new SfdxError ( "There has been a puppeteer timeout while processing Sharing Calc Resume operation" ) ; } } , this . flags . timeout ) ; await this . resumeSharingCalc ( ) ; clearTimeout ( this . timeoutHandler ) ; this . timeoutHandler = null ; return result ; } private async resumeSharingCalc ( ) { const instanceUrl = this . org . getConnection ( ) . instanceUrl ; const SHARING_CALC_PATH = "/p/own/DeferSharingSetupPage" ; this . ux . startSpinner ( `Resuming ${ mapSharingLabel . get ( this . flags . scope ) } Calculations` , null , { stdout : true } ) ; this . debug ( `DEBUG Login to Org` ) ; const browser = await puppeteer . launch ( { args : [ "--no-sandbox" , "--disable-setuid-sandbox" ] , headless : ! ( process . env . BROWSER_DEBUG === "true" ) } ) ; const page = await browser . newPage ( ) ; await page . goto ( `${ instanceUrl }/secur/frontdoor.jsp?sid=${ this . org . getConnection ( ) . accessToken }` , { waitUntil : [ "domcontentloaded" , "networkidle0" ] } ) ; const navigationPromise = page . waitForNavigation ( ) ; this . debug ( `DEBUG Opening Defer Sharing Calculations page` ) ; await page . goto ( `${ instanceUrl + SHARING_CALC_PATH }` ) ; await navigationPromise ; this . debug ( `DEBUG Clicking 'Resume' button` ) ; try { if ( this . flags . scope === "groupMembership" ) { await page . click ( `#gmSect > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="group_resume"].btn` ) ; await page . click ( `div#group_resume_dialog_buttons > input[value=" Yes "]` ) ; } else { await page . click ( `#ep > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="rule_resume"].btn` ) ; } } catch ( ex ) { console . log ( 'Unable to resume sharing.' , ex . message ) ; } await navigationPromise ; this . debug ( `DEBUG Closing browser` ) ; await browser . close ( ) ; this . ux . stopSpinner ( "Done." ) ; return { message : `Resumed ${ mapSharingLabel . get ( this . flags . scope ) } Calculations` } ; } }