/
country.ts
171 lines (148 loc) · 5.84 KB
/
country.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (c) 2021, jayree
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
Flags,
SfCommand,
requiredOrgFlagWithDeprecations,
orgApiVersionFlagWithDeprecations,
} from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { ux } from '@oclif/core';
import playwright from 'playwright-chromium';
import { tabletojson } from 'tabletojson';
import { configSelectors } from '../../../../utils/puppeteer/countrystateconfig.js';
import { readLaunchOptionsFromProject } from '../../../../utils/puppeteer/utils.js';
// eslint-disable-next-line no-underscore-dangle
const __filename = fileURLToPath(import.meta.url);
// eslint-disable-next-line no-underscore-dangle
const __dirname = dirname(__filename);
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@jayree/sfdx-plugin-org', 'createstatecountry');
// eslint-disable-next-line sf-plugin/command-example
export default class UpdateCountry extends SfCommand<void> {
public static readonly summary = messages.getMessage('commandCountryDescription');
// public static readonly description = messages.getMessage('commandCountryDescription');
public static readonly flags = {
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
silent: Flags.boolean({
summary: messages.getMessage('flags.silent.summary'),
required: false,
default: false,
hidden: true,
}),
};
public async run(): Promise<void> {
const { flags } = await this.parse(UpdateCountry);
let spinnermessage = '';
const browser = await playwright['chromium'].launch(await readLaunchOptionsFromProject());
const context = await browser.newContext();
const page = await context.newPage();
const setHTMLInputElementValue = async (newvalue: string, element: string): Promise<void> => {
element = element.replace(/:/g, '\\:');
const elementDisabled = await page.evaluate<boolean, string>((s) => {
const result = document.querySelector<HTMLInputElement>(s);
if (result != null) {
return result.disabled;
} else {
return true;
}
}, element);
if (!elementDisabled) {
return page.fill(element, newvalue);
}
};
const bar = ux.progress({
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
format: 'State and Country/Territory Picklist | [{bar}] {percentage}% | ETA: {eta}s | {value}/{total} | {text}',
stream: process.stdout,
});
try {
// eslint-disable-next-line no-unused-expressions
!flags.silent
? this.spinner.start('State and Country/Territory Picklist')
: process.stdout.write('State and Country/Territory Picklist');
await flags['target-org'].getConnection(flags['api-version']).refreshAuth();
const conn = flags['target-org'].getConnection(flags['api-version']);
spinnermessage = `login to ${conn.instanceUrl}`;
// eslint-disable-next-line no-unused-expressions
!flags.silent ? this.spinner.start(spinnermessage) : process.stdout.write('.');
await page.goto(`${conn.instanceUrl}/secur/frontdoor.jsp?sid=${conn.accessToken}`, {
waitUntil: 'networkidle',
});
spinnermessage = 'retrieve list of countries';
// eslint-disable-next-line no-unused-expressions
!flags.silent ? this.spinner.start(spinnermessage) : process.stdout.write('.');
try {
await page.goto(conn.instanceUrl + '/i18n/ConfigStateCountry.apexp?setupid=AddressCleanerOverview', {
waitUntil: 'networkidle',
});
await page.waitForSelector('.list', { state: 'visible' });
} catch (error) {
throw Error("list of countries couldn't be loaded");
}
this.spinner.stop();
const table = await page.evaluate<string | undefined>(() => {
const list = document.querySelector('.list');
if (list) {
return list.outerHTML;
}
});
const list = tabletojson.convert(table as string)[0] as Array<{
Action: string;
Active: string;
Visible: string;
Country: string;
'Country/Territory Code': string;
'Available States/Provinces': string;
}>;
let curr = 0;
if (!flags.silent) {
bar.start(list.length, 0, {
text: '',
});
}
for await (const value of list) {
const countryCode = value['Country/Territory Code'];
const countryName = value.Country;
curr = curr + 1;
// eslint-disable-next-line no-unused-expressions
!flags.silent
? bar.update(curr, {
text: 'update ' + countryName + '/' + countryCode,
})
: process.stdout.write('.');
await page.goto(
conn.instanceUrl + `/i18n/ConfigureCountry.apexp?countryIso=${countryCode}&setupid=AddressCleanerOverview`,
{
waitUntil: 'networkidle',
},
);
const setCountrySelector = configSelectors.setCountry;
await setHTMLInputElementValue(countryCode, setCountrySelector.editIntVal);
await page.click(setCountrySelector.save.replace(/:/g, '\\:'));
await page.waitForSelector('.message.confirmM3', { state: 'visible' });
}
} catch (error) {
throw new Error((error as Error).message);
} finally {
// eslint-disable-next-line no-unused-expressions
!flags.silent ? bar.update(bar.getTotal(), { text: '' }) : process.stdout.write('.');
this.spinner.stop();
bar.stop();
if (page) {
await page.close();
if (browser) {
await browser.close();
}
}
}
}
}