/
resume.ts
133 lines (104 loc) · 4.09 KB
/
resume.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { flags, SfdxCommand } from "@salesforce/command";
import { Messages } from '@salesforce/core';
import { SfdxError } from "@salesforce/core";
import * as puppeteer from "puppeteer";
// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
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
})
};
// Comment this out if your command does not require an org username
protected static requiresUsername = true;
// Comment this out if your command does not support a hub org username
protected static requiresDevhubUsername = false;
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = false;
private timeoutHandler = null;
public async run(): Promise<any> {
let result = {};
// Start timeout handler
this.timeoutHandler = setTimeout(() => {
if (this.timeoutHandler) {
throw new SfdxError("There has been a puppeteer timeout while processing Sharing Calc Resume operation");
}
} , this.flags.timeout);
// Process operation
await this.resumeSharingCalc();
// Clear timeout handler
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 {
// Resume either Group Membership or Sharing Rules
if (this.flags.scope === "groupMembership") {
await page.click(
`#gmSect > .pbBody > .pbSubsection > .detailList > tbody > .detailRow > td > input[name="group_resume"].btn`
);
// click the yes button to recaulcate group memberships immediately
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` };
}
}