import { flags , SfdxCommand } from "@salesforce/command" ; import { Messages } from '@salesforce/core' ; import * as puppeteer from "puppeteer" ; Messages . importMessagesDirectory ( __dirname ) ; const messages = Messages . loadMessages ( "texei-sfdx-plugin" , "sharingcalc-recalculate" ) ; const mapSharingLabel = new Map ( [ [ 'sharingRule' , 'Sharing Rule' ] ] ) ; export default class Recalculate extends SfdxCommand { public static description = messages . getMessage ( "commandDescription" ) ; public static examples = [ `$ sfdx texei:sharingcalc:recalculate" \nRecalculated Sharing Rules\n` ] ; protected static flagsConfig = { scope : flags . string ( { char : "s" , description : messages . getMessage ( "scopeFlagDescription" ) , required : false , options : [ "sharingRule" ] , default : "sharingRule" } ) } ; protected static requiresUsername = true ; protected static requiresDevhubUsername = false ; protected static requiresProject = false ; public async run ( ) { let result = { } ; await this . reclaculateSharing ( ) ; return result ; } private async reclaculateSharing ( ) { 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 'Recalculate' button` ) ; try { await page . click ( `#ep > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="rule_recalc"].btn` ) ; } catch ( ex ) { console . log ( 'Unable to recalculate sharing.' , ex . message ) ; } await navigationPromise ; this . debug ( `DEBUG Closing browser` ) ; await browser . close ( ) ; this . ux . stopSpinner ( "Done." ) ; return { message : `Recalculated ${ mapSharingLabel . get ( this . flags . scope ) }s` } ; } }