sfdx-hardis 6.0.6-beta202508141313.0 → 6.0.7-beta202508191748.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/defaults/refresh-sandbox/package-certificates-to-save.xml +8 -0
  3. package/defaults/refresh-sandbox/package-metadatas-to-save.xml +44 -0
  4. package/lib/commands/hardis/auth/login.js +3 -1
  5. package/lib/commands/hardis/auth/login.js.map +1 -1
  6. package/lib/commands/hardis/doc/fieldusage.js +3 -1
  7. package/lib/commands/hardis/doc/fieldusage.js.map +1 -1
  8. package/lib/commands/hardis/doc/flow2markdown.js +3 -1
  9. package/lib/commands/hardis/doc/flow2markdown.js.map +1 -1
  10. package/lib/commands/hardis/doc/mkdocs-to-cf.js +3 -1
  11. package/lib/commands/hardis/doc/mkdocs-to-cf.js.map +1 -1
  12. package/lib/commands/hardis/doc/mkdocs-to-salesforce.js +40 -38
  13. package/lib/commands/hardis/doc/mkdocs-to-salesforce.js.map +1 -1
  14. package/lib/commands/hardis/doc/override-prompts.js +3 -1
  15. package/lib/commands/hardis/doc/override-prompts.js.map +1 -1
  16. package/lib/commands/hardis/doc/packagexml2markdown.js +28 -26
  17. package/lib/commands/hardis/doc/packagexml2markdown.js.map +1 -1
  18. package/lib/commands/hardis/doc/plugin/generate.js +3 -1
  19. package/lib/commands/hardis/doc/plugin/generate.js.map +1 -1
  20. package/lib/commands/hardis/git/pull-requests/extract.js +3 -1
  21. package/lib/commands/hardis/git/pull-requests/extract.js.map +1 -1
  22. package/lib/commands/hardis/lint/access.js +3 -1
  23. package/lib/commands/hardis/lint/access.js.map +1 -1
  24. package/lib/commands/hardis/lint/metadatastatus.js +3 -1
  25. package/lib/commands/hardis/lint/metadatastatus.js.map +1 -1
  26. package/lib/commands/hardis/lint/missingattributes.js +3 -1
  27. package/lib/commands/hardis/lint/missingattributes.js.map +1 -1
  28. package/lib/commands/hardis/lint/unusedmetadatas.js +3 -1
  29. package/lib/commands/hardis/lint/unusedmetadatas.js.map +1 -1
  30. package/lib/commands/hardis/mdapi/deploy.d.ts +1 -1
  31. package/lib/commands/hardis/mdapi/deploy.js +3 -1
  32. package/lib/commands/hardis/mdapi/deploy.js.map +1 -1
  33. package/lib/commands/hardis/misc/custom-label-translations.js +3 -1
  34. package/lib/commands/hardis/misc/custom-label-translations.js.map +1 -1
  35. package/lib/commands/hardis/misc/purge-references.js +3 -1
  36. package/lib/commands/hardis/misc/purge-references.js.map +1 -1
  37. package/lib/commands/hardis/misc/toml2csv.js +3 -1
  38. package/lib/commands/hardis/misc/toml2csv.js.map +1 -1
  39. package/lib/commands/hardis/org/community/update.d.ts +1 -1
  40. package/lib/commands/hardis/org/community/update.js +3 -1
  41. package/lib/commands/hardis/org/community/update.js.map +1 -1
  42. package/lib/commands/hardis/org/configure/data.js +3 -1
  43. package/lib/commands/hardis/org/configure/data.js.map +1 -1
  44. package/lib/commands/hardis/org/configure/files.js +3 -1
  45. package/lib/commands/hardis/org/configure/files.js.map +1 -1
  46. package/lib/commands/hardis/org/configure/monitoring.js +3 -1
  47. package/lib/commands/hardis/org/configure/monitoring.js.map +1 -1
  48. package/lib/commands/hardis/org/connect.js +3 -1
  49. package/lib/commands/hardis/org/connect.js.map +1 -1
  50. package/lib/commands/hardis/org/create.js +3 -1
  51. package/lib/commands/hardis/org/create.js.map +1 -1
  52. package/lib/commands/hardis/org/data/delete.js +3 -1
  53. package/lib/commands/hardis/org/data/delete.js.map +1 -1
  54. package/lib/commands/hardis/org/data/export.js +3 -1
  55. package/lib/commands/hardis/org/data/export.js.map +1 -1
  56. package/lib/commands/hardis/org/diagnose/instanceupgrade.js +3 -1
  57. package/lib/commands/hardis/org/diagnose/instanceupgrade.js.map +1 -1
  58. package/lib/commands/hardis/org/diagnose/licenses.js +3 -1
  59. package/lib/commands/hardis/org/diagnose/licenses.js.map +1 -1
  60. package/lib/commands/hardis/org/diagnose/unused-connected-apps.js +3 -1
  61. package/lib/commands/hardis/org/diagnose/unused-connected-apps.js.map +1 -1
  62. package/lib/commands/hardis/org/diagnose/unusedlicenses.js +15 -3
  63. package/lib/commands/hardis/org/diagnose/unusedlicenses.js.map +1 -1
  64. package/lib/commands/hardis/org/diagnose/unusedusers.js +6 -4
  65. package/lib/commands/hardis/org/diagnose/unusedusers.js.map +1 -1
  66. package/lib/commands/hardis/org/files/export.js +3 -1
  67. package/lib/commands/hardis/org/files/export.js.map +1 -1
  68. package/lib/commands/hardis/org/files/import.js +3 -1
  69. package/lib/commands/hardis/org/files/import.js.map +1 -1
  70. package/lib/commands/hardis/org/generate/packagexmlfull.js +3 -1
  71. package/lib/commands/hardis/org/generate/packagexmlfull.js.map +1 -1
  72. package/lib/commands/hardis/org/monitor/limits.js +3 -1
  73. package/lib/commands/hardis/org/monitor/limits.js.map +1 -1
  74. package/lib/commands/hardis/org/multi-org-query.js +3 -1
  75. package/lib/commands/hardis/org/multi-org-query.js.map +1 -1
  76. package/lib/commands/hardis/org/purge/apexlog.js +3 -1
  77. package/lib/commands/hardis/org/purge/apexlog.js.map +1 -1
  78. package/lib/commands/hardis/org/purge/flow.js +3 -1
  79. package/lib/commands/hardis/org/purge/flow.js.map +1 -1
  80. package/lib/commands/hardis/org/refresh/after-refresh.d.ts +22 -0
  81. package/lib/commands/hardis/org/refresh/after-refresh.js +272 -0
  82. package/lib/commands/hardis/org/refresh/after-refresh.js.map +1 -0
  83. package/lib/commands/hardis/org/refresh/before-refresh.d.ts +42 -0
  84. package/lib/commands/hardis/org/refresh/before-refresh.js +725 -0
  85. package/lib/commands/hardis/org/refresh/before-refresh.js.map +1 -0
  86. package/lib/commands/hardis/org/retrieve/packageconfig.js +3 -1
  87. package/lib/commands/hardis/org/retrieve/packageconfig.js.map +1 -1
  88. package/lib/commands/hardis/org/retrieve/sources/analytics.js +3 -1
  89. package/lib/commands/hardis/org/retrieve/sources/analytics.js.map +1 -1
  90. package/lib/commands/hardis/org/retrieve/sources/dx.js +3 -1
  91. package/lib/commands/hardis/org/retrieve/sources/dx.js.map +1 -1
  92. package/lib/commands/hardis/org/retrieve/sources/dx2.js +3 -1
  93. package/lib/commands/hardis/org/retrieve/sources/dx2.js.map +1 -1
  94. package/lib/commands/hardis/org/retrieve/sources/metadata.js +3 -1
  95. package/lib/commands/hardis/org/retrieve/sources/metadata.js.map +1 -1
  96. package/lib/commands/hardis/org/select.js +3 -1
  97. package/lib/commands/hardis/org/select.js.map +1 -1
  98. package/lib/commands/hardis/org/user/freeze.js +3 -1
  99. package/lib/commands/hardis/org/user/freeze.js.map +1 -1
  100. package/lib/commands/hardis/org/user/unfreeze.js +3 -1
  101. package/lib/commands/hardis/org/user/unfreeze.js.map +1 -1
  102. package/lib/commands/hardis/package/create.js +3 -1
  103. package/lib/commands/hardis/package/create.js.map +1 -1
  104. package/lib/commands/hardis/package/mergexml.js +3 -1
  105. package/lib/commands/hardis/package/mergexml.js.map +1 -1
  106. package/lib/commands/hardis/package/version/create.js +3 -1
  107. package/lib/commands/hardis/package/version/create.js.map +1 -1
  108. package/lib/commands/hardis/package/version/list.js +3 -1
  109. package/lib/commands/hardis/package/version/list.js.map +1 -1
  110. package/lib/commands/hardis/package/version/promote.js +3 -1
  111. package/lib/commands/hardis/package/version/promote.js.map +1 -1
  112. package/lib/commands/hardis/packagexml/append.d.ts +1 -1
  113. package/lib/commands/hardis/packagexml/append.js +3 -1
  114. package/lib/commands/hardis/packagexml/append.js.map +1 -1
  115. package/lib/commands/hardis/packagexml/remove.d.ts +1 -1
  116. package/lib/commands/hardis/packagexml/remove.js +3 -1
  117. package/lib/commands/hardis/packagexml/remove.js.map +1 -1
  118. package/lib/commands/hardis/project/audit/callincallout.js +3 -1
  119. package/lib/commands/hardis/project/audit/callincallout.js.map +1 -1
  120. package/lib/commands/hardis/project/audit/duplicatefiles.js +3 -1
  121. package/lib/commands/hardis/project/audit/duplicatefiles.js.map +1 -1
  122. package/lib/commands/hardis/project/audit/remotesites.js +3 -1
  123. package/lib/commands/hardis/project/audit/remotesites.js.map +1 -1
  124. package/lib/commands/hardis/project/clean/emptyitems.js +3 -1
  125. package/lib/commands/hardis/project/clean/emptyitems.js.map +1 -1
  126. package/lib/commands/hardis/project/clean/filter-xml-content.d.ts +1 -1
  127. package/lib/commands/hardis/project/clean/filter-xml-content.js +3 -1
  128. package/lib/commands/hardis/project/clean/filter-xml-content.js.map +1 -1
  129. package/lib/commands/hardis/project/clean/hiddenitems.js +3 -1
  130. package/lib/commands/hardis/project/clean/hiddenitems.js.map +1 -1
  131. package/lib/commands/hardis/project/clean/manageditems.js +3 -1
  132. package/lib/commands/hardis/project/clean/manageditems.js.map +1 -1
  133. package/lib/commands/hardis/project/clean/orgmissingitems.js +3 -1
  134. package/lib/commands/hardis/project/clean/orgmissingitems.js.map +1 -1
  135. package/lib/commands/hardis/project/clean/references.js +3 -1
  136. package/lib/commands/hardis/project/clean/references.js.map +1 -1
  137. package/lib/commands/hardis/project/clean/retrievefolders.js +3 -1
  138. package/lib/commands/hardis/project/clean/retrievefolders.js.map +1 -1
  139. package/lib/commands/hardis/project/clean/standarditems.js +3 -1
  140. package/lib/commands/hardis/project/clean/standarditems.js.map +1 -1
  141. package/lib/commands/hardis/project/clean/systemdebug.js +3 -1
  142. package/lib/commands/hardis/project/clean/systemdebug.js.map +1 -1
  143. package/lib/commands/hardis/project/configure/auth.js +2 -1
  144. package/lib/commands/hardis/project/configure/auth.js.map +1 -1
  145. package/lib/commands/hardis/project/convert/profilestopermsets.js +3 -1
  146. package/lib/commands/hardis/project/convert/profilestopermsets.js.map +1 -1
  147. package/lib/commands/hardis/project/deploy/simulate.js +3 -1
  148. package/lib/commands/hardis/project/deploy/simulate.js.map +1 -1
  149. package/lib/commands/hardis/project/fix/profiletabs.js +3 -1
  150. package/lib/commands/hardis/project/fix/profiletabs.js.map +1 -1
  151. package/lib/commands/hardis/project/fix/v53flexipages.js +3 -1
  152. package/lib/commands/hardis/project/fix/v53flexipages.js.map +1 -1
  153. package/lib/commands/hardis/project/generate/bypass.js +3 -1
  154. package/lib/commands/hardis/project/generate/bypass.js.map +1 -1
  155. package/lib/commands/hardis/project/generate/gitdelta.js +3 -1
  156. package/lib/commands/hardis/project/generate/gitdelta.js.map +1 -1
  157. package/lib/commands/hardis/project/lint.js +3 -1
  158. package/lib/commands/hardis/project/lint.js.map +1 -1
  159. package/lib/commands/hardis/scratch/delete.js +3 -1
  160. package/lib/commands/hardis/scratch/delete.js.map +1 -1
  161. package/lib/commands/hardis/scratch/pool/localauth.js +3 -1
  162. package/lib/commands/hardis/scratch/pool/localauth.js.map +1 -1
  163. package/lib/commands/hardis/scratch/pool/refresh.js +3 -1
  164. package/lib/commands/hardis/scratch/pool/refresh.js.map +1 -1
  165. package/lib/commands/hardis/scratch/pool/reset.js +3 -1
  166. package/lib/commands/hardis/scratch/pool/reset.js.map +1 -1
  167. package/lib/commands/hardis/scratch/pool/view.js +3 -1
  168. package/lib/commands/hardis/scratch/pool/view.js.map +1 -1
  169. package/lib/commands/hardis/scratch/pull.js +3 -1
  170. package/lib/commands/hardis/scratch/pull.js.map +1 -1
  171. package/lib/commands/hardis/scratch/push.js +3 -1
  172. package/lib/commands/hardis/scratch/push.js.map +1 -1
  173. package/lib/commands/hardis/source/retrieve.d.ts +1 -1
  174. package/lib/commands/hardis/source/retrieve.js +3 -1
  175. package/lib/commands/hardis/source/retrieve.js.map +1 -1
  176. package/lib/commands/hardis/work/new.js +26 -10
  177. package/lib/commands/hardis/work/new.js.map +1 -1
  178. package/lib/commands/hardis/work/refresh.js +3 -1
  179. package/lib/commands/hardis/work/refresh.js.map +1 -1
  180. package/lib/commands/hardis/work/resetselection.js +3 -1
  181. package/lib/commands/hardis/work/resetselection.js.map +1 -1
  182. package/lib/commands/hardis/work/save.js +3 -1
  183. package/lib/commands/hardis/work/save.js.map +1 -1
  184. package/lib/commands/hardis/work/ws.js +3 -1
  185. package/lib/commands/hardis/work/ws.js.map +1 -1
  186. package/lib/commands/hello/world.d.ts +1 -1
  187. package/lib/commands/hello/world.js +3 -1
  188. package/lib/commands/hello/world.js.map +1 -1
  189. package/lib/common/utils/apiUtils.js +4 -4
  190. package/lib/common/utils/apiUtils.js.map +1 -1
  191. package/lib/common/utils/deployUtils.js +2 -2
  192. package/lib/common/utils/deployUtils.js.map +1 -1
  193. package/lib/common/utils/filesUtils.d.ts +1 -0
  194. package/lib/common/utils/filesUtils.js +5 -3
  195. package/lib/common/utils/filesUtils.js.map +1 -1
  196. package/lib/common/utils/orgConfigUtils.d.ts +6 -0
  197. package/lib/common/utils/orgConfigUtils.js +17 -8
  198. package/lib/common/utils/orgConfigUtils.js.map +1 -1
  199. package/lib/common/utils/orgUtils.js +14 -1
  200. package/lib/common/utils/orgUtils.js.map +1 -1
  201. package/lib/common/utils/refresh/connectedAppUtils.d.ts +68 -0
  202. package/lib/common/utils/refresh/connectedAppUtils.js +340 -0
  203. package/lib/common/utils/refresh/connectedAppUtils.js.map +1 -0
  204. package/oclif.lock +730 -699
  205. package/oclif.manifest.json +428 -189
  206. package/package.json +5 -5
@@ -0,0 +1,725 @@
1
+ import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
2
+ import { Messages, SfError } from '@salesforce/core';
3
+ import fs from 'fs-extra';
4
+ import c from 'chalk';
5
+ import open from 'open';
6
+ import axios from 'axios';
7
+ import path from 'path';
8
+ import puppeteer from 'puppeteer-core';
9
+ import { execCommand, execSfdxJson, isCI, uxLog } from '../../../../common/utils/index.js';
10
+ import { prompts } from '../../../../common/utils/prompts.js';
11
+ import { parsePackageXmlFile, parseXmlFile, writePackageXmlFile } from '../../../../common/utils/xmlUtils.js';
12
+ import { getChromeExecutablePath } from '../../../../common/utils/orgConfigUtils.js';
13
+ import { deleteConnectedApps, retrieveConnectedApps, validateConnectedApps, findConnectedAppFile, selectConnectedAppsForProcessing, createConnectedAppSuccessResponse, handleConnectedAppError } from '../../../../common/utils/refresh/connectedAppUtils.js';
14
+ import { getConfig, setConfig } from '../../../../config/index.js';
15
+ import { soqlQuery } from '../../../../common/utils/apiUtils.js';
16
+ import { WebSocketClient } from '../../../../common/websocketClient.js';
17
+ import { PACKAGE_ROOT_DIR } from '../../../../settings.js';
18
+ Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
19
+ const messages = Messages.loadMessages('sfdx-hardis', 'org');
20
+ export default class OrgRefreshBeforeRefresh extends SfCommand {
21
+ static description = `
22
+ ## Command Behavior
23
+
24
+ **Backs up all Connected Apps, their secrets, certificates, and custom settings from a Salesforce org before a sandbox refresh, enabling full restoration after the refresh.**
25
+
26
+ This command is essential for Salesforce sandbox refresh operations where Connected Apps (and their Consumer Secrets), certificates, and custom settings would otherwise be lost. It automates the extraction, secure storage, and (optionally) deletion of Connected Apps, ensuring that all credentials and configuration can be restored post-refresh.
27
+
28
+ Key functionalities:
29
+
30
+ - **Connected App Discovery:** Lists all Connected Apps in the org, with options to filter by name, process all, or interactively select.
31
+ - **User Selection:** Allows interactive or flag-based selection of which Connected Apps to back up.
32
+ - **Metadata Retrieval:** Retrieves Connected App metadata and saves it in a dedicated project folder for the sandbox instance.
33
+ - **Consumer Secret Extraction:** Attempts to extract Consumer Secrets automatically using browser automation (Puppeteer), or prompts for manual entry if automation fails.
34
+ - **Config Persistence:** Stores the list of selected apps in the project config for use during restoration.
35
+ - **Optional Deletion:** Can delete the Connected Apps from the org after backup, as required for re-upload after refresh.
36
+ - **Certificate Backup:** Retrieves all org certificates and their definitions, saving them for later restoration.
37
+ - **Custom Settings Backup:** Lists all custom settings in the org, allows user selection, and exports their data to JSON files for backup.
38
+ - **Summary and Reporting:** Provides a summary of actions, including which apps, certificates, and custom settings were saved and whether secrets were captured.
39
+
40
+ This command is part of [sfdx-hardis Sandbox Refresh](https://sfdx-hardis.cloudity.com/salesforce-sandbox-refresh/) and is designed to be run before a sandbox refresh. It ensures that all Connected Apps, secrets, certificates, and custom settings are safely stored for later restoration.
41
+
42
+ <details markdown="1">
43
+ <summary>Technical explanations</summary>
44
+
45
+ - **Salesforce CLI Integration:** Uses \`sf org list metadata\`, \`sf project retrieve start\`, and other CLI commands to discover and retrieve Connected Apps, certificates, and custom settings.
46
+ - **Metadata Handling:** Saves Connected App XML files and certificate files in a dedicated folder under \`scripts/sandbox-refresh/<sandbox-folder>\`.
47
+ - **Consumer Secret Handling:** Uses Puppeteer to automate browser login and extraction of Consumer Secrets, falling back to manual prompts if needed.
48
+ - **Custom Settings Handling:** Lists all custom settings, allows user selection, and exports their data using \`sf data tree export\` to JSON files.
49
+ - **Config Management:** Updates \`config/.sfdx-hardis.yml\` with the list of selected apps for later use.
50
+ - **Deletion Logic:** Optionally deletes Connected Apps from the org (required for re-upload after refresh), with user confirmation unless running in CI or with \`--delete\` flag.
51
+ - **Error Handling:** Provides detailed error messages and guidance if retrieval or extraction fails.
52
+ - **Reporting:** Sends summary and configuration files to the WebSocket client for reporting and traceability.
53
+
54
+ </details>
55
+ `;
56
+ static examples = [
57
+ "$ sf hardis:org:refresh:before-refresh",
58
+ "$ sf hardis:org:refresh:before-refresh --name \"MyConnectedApp\"",
59
+ "$ sf hardis:org:refresh:before-refresh --name \"App1,App2,App3\"",
60
+ "$ sf hardis:org:refresh:before-refresh --all",
61
+ "$ sf hardis:org:refresh:before-refresh --delete",
62
+ ];
63
+ static flags = {
64
+ "target-org": Flags.requiredOrg(),
65
+ delete: Flags.boolean({
66
+ char: 'd',
67
+ summary: 'Delete Connected Apps from org after saving',
68
+ description: 'By default, Connected Apps are not deleted from the org after saving. Set this flag to force their deletion so they will be able to be reuploaded again after refreshing the org.',
69
+ default: false
70
+ }),
71
+ name: Flags.string({
72
+ char: 'n',
73
+ summary: messages.getMessage('nameFilter'),
74
+ description: 'Connected App name(s) to process. For multiple apps, separate with commas (e.g., "App1,App2")'
75
+ }),
76
+ all: Flags.boolean({
77
+ char: 'a',
78
+ summary: 'Process all Connected Apps without selection prompt',
79
+ description: 'If set, all Connected Apps from the org will be processed. Takes precedence over --name if both are specified.'
80
+ }),
81
+ websocket: Flags.string({
82
+ description: messages.getMessage('websocket'),
83
+ }),
84
+ skipauth: Flags.boolean({
85
+ description: 'Skip authentication check when a default username is required',
86
+ })
87
+ };
88
+ static requiresProject = true;
89
+ conn;
90
+ saveProjectPath = '';
91
+ orgUsername = '';
92
+ instanceUrl = '';
93
+ refreshSandboxConfig = {};
94
+ result;
95
+ async run() {
96
+ const { flags } = await this.parse(OrgRefreshBeforeRefresh);
97
+ this.conn = flags["target-org"].getConnection();
98
+ this.orgUsername = flags["target-org"].getUsername(); // Cast to string to avoid TypeScript error
99
+ this.instanceUrl = this.conn.instanceUrl;
100
+ const accessToken = this.conn.accessToken; // Ensure accessToken is a string
101
+ const processAll = flags.all || false;
102
+ const nameFilter = processAll ? undefined : flags.name; // If --all is set, ignore --name
103
+ const config = await getConfig("user");
104
+ this.refreshSandboxConfig = config?.refreshSandboxConfig || {};
105
+ this.result = { success: true, message: 'before-refresh command performed successfully' };
106
+ uxLog("action", this, c.cyan(`This command with save information that will need to be restored after org refresh
107
+ - Certificates
108
+ - Custom Settings
109
+ - Connected Apps`));
110
+ // Check org is connected
111
+ if (!accessToken) {
112
+ throw new SfError(c.red('Access token is required to retrieve Connected Apps from the org. Please authenticate to a default org.'));
113
+ }
114
+ this.saveProjectPath = await this.createSaveProject();
115
+ await this.saveMetadatas();
116
+ await this.saveCustomSettings();
117
+ // If metadatas folder is not empty, ask if we want to retrieve them again
118
+ let retrieveConnectedApps = true;
119
+ const connectedAppsFolder = path.join(this.saveProjectPath, 'force-app', 'main', 'default', 'connectedApps');
120
+ if (fs.existsSync(connectedAppsFolder) && fs.readdirSync(connectedAppsFolder).length > 0) {
121
+ const confirmRetrieval = await prompts({
122
+ type: 'confirm',
123
+ name: 'retrieveAgain',
124
+ message: `Connected Apps folder is not empty. Do you want to retrieve Connected Apps again?`,
125
+ description: `If you do not retrieve them again, the Connected Apps will not be updated with the latest changes from the org.`,
126
+ initial: false
127
+ });
128
+ if (!confirmRetrieval.retrieveAgain) {
129
+ retrieveConnectedApps = false;
130
+ }
131
+ }
132
+ if (retrieveConnectedApps) {
133
+ try {
134
+ // Step 1: Get Connected Apps from org or based on provided name filter
135
+ const connectedApps = await this.getConnectedApps(this.orgUsername, nameFilter, processAll);
136
+ if (connectedApps.length === 0) {
137
+ uxLog("warning", this, c.yellow('No Connected Apps found'));
138
+ return { success: false, message: 'No Connected Apps found' };
139
+ }
140
+ // Step 2: Determine which apps to process (all, filtered, or user-selected)
141
+ const selectedApps = await this.selectConnectedApps(connectedApps, processAll, nameFilter);
142
+ if (selectedApps.length === 0) {
143
+ uxLog("warning", this, c.yellow('No Connected Apps selected'));
144
+ return { success: false, message: 'No Connected Apps selected' };
145
+ }
146
+ this.refreshSandboxConfig.connectedApps = selectedApps.map(app => app.fullName).sort();
147
+ await this.saveConfig();
148
+ // Step 3: Process the selected Connected Apps
149
+ const updatedApps = await this.processConnectedApps(this.orgUsername, selectedApps, this.instanceUrl, accessToken);
150
+ // Step 4: Delete Connected Apps from org if required (default behavior)
151
+ let deleteApps = flags.delete || false;
152
+ if (!isCI && !deleteApps) {
153
+ const connectedAppNames = updatedApps.map(app => app.fullName).join(', ');
154
+ const deletePrompt = await prompts({
155
+ type: 'confirm',
156
+ name: 'delete',
157
+ message: `Do you want to delete the Connected Apps from the org after saving? ${connectedAppNames}`,
158
+ description: 'If you do not delete them, they will remain in the org and can be re-uploaded after refreshing the org.',
159
+ initial: false
160
+ });
161
+ deleteApps = deletePrompt.delete;
162
+ }
163
+ if (deleteApps) {
164
+ uxLog("action", this, c.cyan(`Deleting ${updatedApps.length} Connected Apps from ${this.conn.instanceUrl} ...`));
165
+ await deleteConnectedApps(this.orgUsername, updatedApps, this, this.saveProjectPath);
166
+ uxLog("success", this, c.green('Connected Apps were successfully deleted from the org.'));
167
+ }
168
+ const summaryMessage = deleteApps
169
+ ? `You are now ready to refresh your sandbox org, as you will be able to re-upload the Connected Apps after the refresh.`
170
+ : `Dry-run successful, run again the command with Connected Apps deletion to be able to refresh your org and re-upload the Connected Apps after the refresh.`;
171
+ uxLog("action", this, c.cyan(summaryMessage));
172
+ // Add a summary message at the end
173
+ if (updatedApps.length > 0) {
174
+ uxLog("success", this, c.green(`Successfully saved locally ${updatedApps.length} Connected App(s) with their Consumer Secrets`));
175
+ }
176
+ uxLog("success", this, c.cyan('Saved refresh sandbox configuration in config/.sfdx-hardis.yml'));
177
+ WebSocketClient.sendReportFileMessage(path.join(process.cwd(), 'config', '.sfdx-hardis.yml#refreshSandboxConfig'), "Sandbox refresh configuration", 'report');
178
+ const connectedAppRes = createConnectedAppSuccessResponse(`Successfully processed ${updatedApps.length} Connected App(s)`, updatedApps.map(app => app.fullName), {
179
+ consumerSecretsAdded: updatedApps.map(app => app.consumerSecret ? app.fullName : null).filter(Boolean)
180
+ });
181
+ this.result = Object.assign(this.result || {}, connectedAppRes);
182
+ }
183
+ catch (error) {
184
+ this.result = Object.assign(this.result || {}, handleConnectedAppError(error, this));
185
+ }
186
+ }
187
+ return this.result;
188
+ }
189
+ async createSaveProject() {
190
+ const folderName = this.conn.instanceUrl.replace(/https?:\/\//, '').replace("my.salesforce.com", "").replace(/\//g, '-').replace(/[^a-zA-Z0-9-]/g, '');
191
+ const sandboxRefreshRootFolder = path.join(process.cwd(), 'scripts', 'sandbox-refresh');
192
+ const projectPath = path.join(sandboxRefreshRootFolder, folderName);
193
+ if (fs.existsSync(projectPath)) {
194
+ uxLog("log", this, c.cyan(`Project folder ${projectPath} already exists. Reusing it.\n(Delete it and run again this command if you want to start fresh)`));
195
+ return projectPath;
196
+ }
197
+ await fs.ensureDir(projectPath);
198
+ uxLog("action", this, c.cyan(`Creating sfdx-project for sandbox info storage`));
199
+ const createCommand = `sf project generate --name "${folderName}"`;
200
+ await execCommand(createCommand, this, {
201
+ output: true,
202
+ fail: true,
203
+ });
204
+ uxLog("log", this, c.grey('Moving sfdx-project to root...'));
205
+ await fs.copy(folderName, projectPath, { overwrite: true });
206
+ await fs.remove(folderName);
207
+ uxLog("log", this, c.grey(`Save Project created in folder ${projectPath}`));
208
+ return projectPath;
209
+ }
210
+ async getConnectedApps(orgUsername, nameFilter, processAll) {
211
+ // Set appropriate log message based on flags
212
+ if (processAll) {
213
+ uxLog("action", this, c.cyan('Processing all Connected Apps from org (selection prompt bypassed)'));
214
+ }
215
+ else if (nameFilter) {
216
+ uxLog("action", this, c.cyan(`Processing specified Connected App(s): ${nameFilter} (selection prompt bypassed)`));
217
+ }
218
+ else {
219
+ uxLog("action", this, c.cyan(`Retrieving list of Connected Apps from org ${this.conn.instanceUrl} ...`));
220
+ }
221
+ const command = `sf org list metadata --metadata-type ConnectedApp --target-org ${orgUsername}`;
222
+ const result = await execSfdxJson(command, this, { output: true });
223
+ const availableApps = result?.result && Array.isArray(result.result) ? result.result : [];
224
+ if (availableApps.length === 0) {
225
+ uxLog("warning", this, c.yellow('No Connected Apps were found in the org.'));
226
+ return [];
227
+ }
228
+ availableApps.sort((a, b) => a.fullName.localeCompare(b.fullName));
229
+ const availableAppNames = availableApps.map(app => app.fullName);
230
+ uxLog("log", this, c.grey(`Found ${availableApps.length} Connected App(s) in the org`));
231
+ // If name filter is provided, validate and filter the requested apps
232
+ if (nameFilter) {
233
+ const appNames = nameFilter.split(',').map(name => name.trim());
234
+ uxLog("action", this, c.cyan(`Validating specified Connected App(s): ${appNames.join(', ')}`));
235
+ validateConnectedApps(appNames, availableAppNames, this, 'org');
236
+ // Filter available apps to only include the ones specified in the name filter (case-insensitive)
237
+ const connectedApps = availableApps.filter(app => appNames.some(name => name.toLowerCase() === app.fullName.toLowerCase()));
238
+ uxLog("success", this, c.green(`Successfully validated ${connectedApps.length} Connected App(s) in the org`));
239
+ return connectedApps;
240
+ }
241
+ // If no name filter, return all available apps
242
+ return availableApps;
243
+ }
244
+ async selectConnectedApps(connectedApps, processAll, nameFilter) {
245
+ const initialSelection = [];
246
+ if (this.refreshSandboxConfig.connectedApps && this.refreshSandboxConfig.connectedApps.length > 0) {
247
+ initialSelection.push(...this.refreshSandboxConfig.connectedApps);
248
+ }
249
+ return selectConnectedAppsForProcessing(connectedApps, initialSelection, processAll, nameFilter, 'Select Connected Apps that you will want to restore after org refresh', this);
250
+ }
251
+ async processConnectedApps(orgUsername, connectedApps, instanceUrl, accessToken = '') {
252
+ if (!orgUsername) {
253
+ throw new Error('Organization username is required');
254
+ }
255
+ const updatedApps = [];
256
+ let browserContext = null;
257
+ try {
258
+ // Step 1: Retrieve the Connected Apps from org
259
+ await this.retrieveConnectedAppsFromOrg(orgUsername, connectedApps, this.saveProjectPath);
260
+ // Step 2: Query for applicationIds for all Connected Apps
261
+ const connectedAppIdMap = await this.queryConnectedAppIds(orgUsername, connectedApps);
262
+ // Step 3: Initialize browser for automation if access token is available
263
+ uxLog("action", this, c.cyan('Initializing browser for automated Connected App Secrets extraction...'));
264
+ try {
265
+ browserContext = await this.initializeBrowser(instanceUrl, accessToken);
266
+ }
267
+ catch (e) {
268
+ uxLog("error", this, c.red(`Error initializing browser for automated Consumer Secret extraction: ${e.message}.
269
+ You might need to set variable PUPPETEER_EXECUTABLE_PATH with the target of a Chrome/Chromium path. example: /usr/bin/chromium-browser`));
270
+ // Continue without browser automation - will fall back to manual entry
271
+ }
272
+ // Step 4: Process each Connected App
273
+ for (const app of connectedApps) {
274
+ try {
275
+ const updatedApp = await this.processIndividualApp(app, connectedAppIdMap, browserContext, instanceUrl, this.saveProjectPath);
276
+ if (updatedApp) {
277
+ updatedApps.push(updatedApp);
278
+ }
279
+ }
280
+ catch (error) {
281
+ uxLog("warning", this, c.yellow(`Error processing ${app.fullName}: ${error.message || error}`));
282
+ }
283
+ }
284
+ return updatedApps;
285
+ }
286
+ finally {
287
+ // Close browser if it was opened
288
+ if (browserContext?.browser) {
289
+ uxLog("log", this, c.cyan('Closing browser...'));
290
+ await browserContext.browser.close();
291
+ }
292
+ }
293
+ }
294
+ async retrieveConnectedAppsFromOrg(orgUsername, connectedApps, saveProjectPath) {
295
+ uxLog("action", this, c.cyan(`Retrieving ${connectedApps.length} Connected App(s) from ${orgUsername}`));
296
+ await retrieveConnectedApps(orgUsername, connectedApps, this, saveProjectPath);
297
+ this.verifyConnectedAppsRetrieval(connectedApps);
298
+ }
299
+ verifyConnectedAppsRetrieval(connectedApps) {
300
+ if (connectedApps.length === 0)
301
+ return;
302
+ // Check if the Connected App files exist in the project
303
+ const missingApps = [];
304
+ for (const app of connectedApps) {
305
+ // Try to find the app in the standard location
306
+ const appPath = path.join(this.saveProjectPath, `force-app/main/default/connectedApps/${app.fullName}.connectedApp-meta.xml`);
307
+ if (!fs.existsSync(appPath)) {
308
+ // Also check in alternative locations where it might have been retrieved
309
+ const altPaths = [
310
+ path.join(this.saveProjectPath, `force-app/main/default/connectedApps/${app.fileName}.connectedApp-meta.xml`),
311
+ path.join(this.saveProjectPath, `force-app/main/default/connectedApps/${app.fullName.replace(/\s/g, '_')}.connectedApp-meta.xml`)
312
+ ];
313
+ const found = altPaths.some(path => fs.existsSync(path));
314
+ if (!found) {
315
+ missingApps.push(app.fullName);
316
+ }
317
+ }
318
+ }
319
+ // If any apps are missing, throw an error
320
+ if (missingApps.length > 0) {
321
+ const errorMsg = `Failed to retrieve the following Connected App(s): ${missingApps.join(', ')}`;
322
+ uxLog("error", this, c.red(errorMsg));
323
+ const dtlErrorMsg = "This could be due to:\n" +
324
+ " - Temporary Salesforce API issues\n" +
325
+ " - Permissions or profile issues in the org\n" +
326
+ " - Connected Apps that exist but are not accessible\n" +
327
+ "Please exclude the app or check your permissions in the org then try again.";
328
+ uxLog("warning", this, c.yellow(dtlErrorMsg));
329
+ throw new Error(errorMsg);
330
+ }
331
+ }
332
+ async queryConnectedAppIds(orgUsername, connectedApps) {
333
+ const connectedAppIdMap = {};
334
+ const appNamesForQuery = connectedApps.map(app => `'${app.fullName}'`).join(',');
335
+ if (appNamesForQuery.length === 0) {
336
+ return connectedAppIdMap;
337
+ }
338
+ uxLog("action", this, c.cyan('Retrieving applicationIds for all Connected Apps...'));
339
+ const queryCommand = `SELECT Id, Name FROM ConnectedApplication WHERE Name IN (${appNamesForQuery})`;
340
+ try {
341
+ const appQueryRes = await soqlQuery(queryCommand, this.conn);
342
+ if (appQueryRes?.records?.length > 0) {
343
+ // Populate the map with applicationIds
344
+ let logMsg = `Found ${appQueryRes.records.length} applicationId(s) for Connected Apps:`;
345
+ for (const record of appQueryRes.records) {
346
+ connectedAppIdMap[record.Name] = record.Id;
347
+ logMsg += `\n - ${record.Name}: ${record.Id}`;
348
+ }
349
+ uxLog("log", this, c.grey(logMsg));
350
+ }
351
+ else {
352
+ uxLog("warning", this, c.yellow('No applicationIds found in the org. Will use the fallback URL.'));
353
+ }
354
+ }
355
+ catch (queryError) {
356
+ uxLog("error", this, c.yellow(`Error retrieving applicationIds: ${queryError}`));
357
+ }
358
+ return connectedAppIdMap;
359
+ }
360
+ async initializeBrowser(instanceUrl, accessToken) {
361
+ // Get chrome/chromium executable path using shared utility
362
+ const chromeExecutablePath = getChromeExecutablePath();
363
+ uxLog("log", this, c.cyan(`chromeExecutablePath: ${chromeExecutablePath}`));
364
+ const browser = await puppeteer.launch({
365
+ args: ['--no-sandbox', '--disable-setuid-sandbox'],
366
+ headless: false, // Always show the browser window
367
+ executablePath: chromeExecutablePath,
368
+ timeout: 60000 // Increase timeout for browser launch
369
+ });
370
+ // Log in once for the session
371
+ const loginUrl = `${instanceUrl}/secur/frontdoor.jsp?sid=${accessToken}`;
372
+ uxLog("log", this, c.cyan(`Log in via browser using frontdoor.jsp...`));
373
+ const page = await browser.newPage();
374
+ await page.goto(loginUrl, { waitUntil: ['domcontentloaded', 'networkidle0'] });
375
+ await page.close();
376
+ return { browser, instanceUrl, accessToken };
377
+ }
378
+ async processIndividualApp(app, connectedAppIdMap, browserContext, instanceUrl, saveProjectPath) {
379
+ const connectedAppFile = await findConnectedAppFile(app.fullName, this, saveProjectPath);
380
+ if (!connectedAppFile) {
381
+ uxLog("warning", this, c.yellow(`Connected App file not found for ${app.fullName}`));
382
+ return undefined;
383
+ }
384
+ const connectedAppId = connectedAppIdMap[app.fullName];
385
+ let consumerSecretValue = null;
386
+ let viewLink;
387
+ // Try to extract application ID and view link
388
+ if (connectedAppId) {
389
+ try {
390
+ uxLog("action", this, c.cyan(`Extracting info for Connected App ${app.fullName}...`));
391
+ const applicationId = await this.extractApplicationId(instanceUrl, connectedAppId, app.fullName, browserContext?.accessToken ?? '');
392
+ viewLink = `${instanceUrl}/app/mgmt/forceconnectedapps/forceAppDetail.apexp?applicationId=${applicationId}`;
393
+ uxLog("success", this, c.green(`Successfully extracted application ID: ${applicationId} (viewLink: ${viewLink})`));
394
+ // Try automated extraction if browser is available
395
+ if (browserContext?.browser) {
396
+ uxLog("log", this, c.cyan(`Attempting to automatically extract Consumer Secret for ${app.fullName}...`));
397
+ try {
398
+ consumerSecretValue = await this.extractConsumerSecret(browserContext.browser, viewLink);
399
+ }
400
+ catch (puppeteerError) {
401
+ uxLog("warning", this, c.yellow(`Error extracting Consumer Secret with Puppeteer: ${puppeteerError}`));
402
+ consumerSecretValue = null;
403
+ }
404
+ }
405
+ }
406
+ catch (error) {
407
+ uxLog("error", this, c.red(`Could not extract application ID for : ${app.fullName}. Error message : ${error}`));
408
+ viewLink = `${instanceUrl}/lightning/setup/NavigationMenus/home`;
409
+ uxLog("action", this, c.cyan(`Opening application list page. Please manually find ${app.fullName}.`));
410
+ }
411
+ }
412
+ else {
413
+ // Fallback to the connected apps list page if applicationId can't be found
414
+ uxLog("warning", this, c.yellow(`No applicationId found for ${app.fullName}, opening application list page instead`));
415
+ viewLink = `${instanceUrl}/lightning/setup/NavigationMenus/home`;
416
+ }
417
+ try {
418
+ // If consumer secret was automatically extracted
419
+ if (consumerSecretValue) {
420
+ const xmlData = await parseXmlFile(connectedAppFile);
421
+ if (xmlData && xmlData.ConnectedApp) {
422
+ const consumerKey = xmlData.ConnectedApp.consumerKey ? xmlData.ConnectedApp.consumerKey[0] : 'unknown';
423
+ return await this.updateConnectedAppWithSecret(connectedAppFile, xmlData, consumerSecretValue, app, consumerKey);
424
+ }
425
+ }
426
+ else {
427
+ // Manual entry flow - open browser and prompt for secret
428
+ const msg = [
429
+ `Unable to automatically extract Consumer Secret for Connected App ${app.fullName}.`,
430
+ `- Open Connected App detail page of ${app.fullName} (Contextual menu -> View)`,
431
+ '- Click "Manage Consumer Details" button',
432
+ `- Copy the ${c.green('Consumer Secret')} value`
433
+ ].join('\n');
434
+ uxLog("action", this, c.cyan(msg));
435
+ await open(viewLink);
436
+ // Prompt for the Consumer Secret (manual entry)
437
+ const secretPromptResponse = await prompts({
438
+ type: 'text',
439
+ name: 'consumerSecret',
440
+ message: `Enter the Consumer Secret for ${app.fullName}:`,
441
+ description: 'You can find this in the browser after clicking "Manage Consumer Details"',
442
+ validate: (value) => value && value.trim() !== '' ? true : 'Consumer Secret is required'
443
+ });
444
+ if (!secretPromptResponse.consumerSecret) {
445
+ uxLog("warning", this, c.yellow(`Skipping ${app.fullName} due to missing Consumer Secret`));
446
+ return undefined;
447
+ }
448
+ // Parse the Connected App XML file
449
+ const xmlData = await parseXmlFile(connectedAppFile);
450
+ if (xmlData && xmlData.ConnectedApp) {
451
+ // Store the consumer secret
452
+ const consumerSecret = secretPromptResponse.consumerSecret;
453
+ const consumerKey = xmlData.ConnectedApp.consumerKey ? xmlData.ConnectedApp.consumerKey[0] : 'unknown';
454
+ return await this.updateConnectedAppWithSecret(connectedAppFile, xmlData, consumerSecret, app, consumerKey);
455
+ }
456
+ else {
457
+ uxLog("warning", this, c.yellow(`Could not parse XML for ${app.fullName}`));
458
+ }
459
+ }
460
+ }
461
+ catch (error) {
462
+ uxLog("warning", this, c.yellow(`Error processing ${app.fullName}: ${error.message}`));
463
+ }
464
+ return undefined;
465
+ }
466
+ async extractApplicationId(instanceUrl, connectedAppId, connectedAppName, accessToken) {
467
+ uxLog("log", this, c.cyan(`Extracting application ID for Connected App with ID: ${connectedAppName}`));
468
+ const url = `${instanceUrl}/${connectedAppId}`;
469
+ const response = await axios.get(url, {
470
+ headers: {
471
+ Cookie: `sid=${accessToken}`
472
+ }
473
+ });
474
+ const html = response.data;
475
+ const appIdMatch = html.match(/applicationId=([a-zA-Z0-9]+)/i);
476
+ if (!appIdMatch || !appIdMatch[1]) {
477
+ throw new Error('Could not extract application ID from HTML');
478
+ }
479
+ return appIdMatch[1];
480
+ }
481
+ async extractConsumerSecret(browser, appUrl) {
482
+ let page;
483
+ try {
484
+ page = await browser.newPage();
485
+ uxLog("log", this, c.grey(`Navigating to Connected App detail page...`));
486
+ await page.goto(appUrl, { waitUntil: ['domcontentloaded', 'networkidle0'] });
487
+ uxLog("log", this, c.grey(`Attempting to extract Consumer Secret...`));
488
+ // Click Manage Consumer Details button
489
+ const manageBtnId = 'input[id="appsetup:setupForm:details:oauthSettingsSection:manageConsumerKeySecretSection:manageConsumer"]';
490
+ await page.waitForSelector(manageBtnId, { timeout: 60000 });
491
+ await page.click(manageBtnId);
492
+ await page.waitForNavigation();
493
+ // Extract Consumer Secret value
494
+ const consumerSecretSpanId = '#appsetup\\:setupForm\\:consumerDetails\\:oauthConsumerSection\\:consumerSecretSection\\:consumerSecret';
495
+ await page.waitForSelector(consumerSecretSpanId, { timeout: 60000 });
496
+ const consumerSecretValue = await page.$eval(consumerSecretSpanId, element => element.textContent);
497
+ uxLog("success", this, c.green(`Successfully extracted Consumer Secret`));
498
+ return consumerSecretValue || null;
499
+ }
500
+ catch (error) {
501
+ uxLog("error", this, c.red(`Error extracting Consumer Secret: ${error}`));
502
+ return null;
503
+ }
504
+ finally {
505
+ if (page)
506
+ await page.close();
507
+ }
508
+ }
509
+ async updateConnectedAppWithSecret(connectedAppFile, xmlData, consumerSecret, app, consumerKey) {
510
+ const xmlString = await fs.readFile(connectedAppFile, 'utf8');
511
+ if (xmlString.includes('<consumerSecret>')) {
512
+ const updatedXmlString = xmlString.replace(/<consumerSecret>.*?<\/consumerSecret>/, `<consumerSecret>${consumerSecret}</consumerSecret>`);
513
+ await fs.writeFile(connectedAppFile, updatedXmlString);
514
+ }
515
+ else {
516
+ // Insert consumerSecret right after consumerKey
517
+ const updatedXmlString = xmlString.replace(/<consumerKey>.*?<\/consumerKey>/, `$&\n <consumerSecret>${consumerSecret}</consumerSecret>`);
518
+ await fs.writeFile(connectedAppFile, updatedXmlString);
519
+ }
520
+ xmlData.ConnectedApp.consumerSecret = [consumerSecret];
521
+ uxLog("success", this, c.green(`Successfully added Consumer Secret to ${app.fullName} in ${connectedAppFile}`));
522
+ return {
523
+ ...app,
524
+ consumerKey: consumerKey,
525
+ consumerSecret: consumerSecret
526
+ };
527
+ }
528
+ async saveConfig() {
529
+ const config = await getConfig("project");
530
+ if (!config.refreshSandboxConfig) {
531
+ config.refreshSandboxConfig = {};
532
+ }
533
+ if (JSON.stringify(this.refreshSandboxConfig) !== JSON.stringify(config.refreshSandboxConfig)) {
534
+ await setConfig("project", { refreshSandboxConfig: this.refreshSandboxConfig });
535
+ uxLog("log", this, c.cyan('Refresh sandbox configuration has been saved successfully.'));
536
+ }
537
+ }
538
+ async saveMetadatas() {
539
+ const metadataToSave = path.join(this.saveProjectPath, "manifest", 'package-metadatas-to-save.xml');
540
+ if (fs.existsSync(metadataToSave)) {
541
+ const promptResponse = await prompts({
542
+ type: 'confirm',
543
+ name: 'retrieveAgain',
544
+ message: `It seems you already have metadatas saved from a previous run.\nDo you want to retrieve certificates and metadata again ?`,
545
+ description: 'This will overwrite the existing package-metadatas-to-save.xml file and related certificates and metadatas.',
546
+ initial: false
547
+ });
548
+ if (!promptResponse.retrieveAgain) {
549
+ uxLog("log", this, c.grey(`Skipping metadata retrieval as it already exists at ${this.saveProjectPath}`));
550
+ return;
551
+ }
552
+ }
553
+ // Retrieve certificates
554
+ await this.retrieveCertificates();
555
+ // Metadata package.Xml for backup
556
+ uxLog("action", this, c.cyan('Saving metadata files before sandbox refresh...'));
557
+ const savePackageXml = await this.createSavePackageXml();
558
+ // Retrieve metadata from org using the package XML
559
+ await this.retrieveMetadatasToSave(savePackageXml);
560
+ // Generate new package.xml from saveProjectPath, and remove ConnectedApps from it
561
+ await this.generatePackageXmlToRestore();
562
+ }
563
+ async createSavePackageXml() {
564
+ uxLog("log", this, c.cyan(`Managing "package-metadatas-to-save.xml" file, that will be used to retrieve the metadatas before refreshing the org.`));
565
+ // Copy default package xml to the save project path
566
+ const sourceFile = path.join(PACKAGE_ROOT_DIR, 'defaults/refresh-sandbox', 'package-metadatas-to-save.xml');
567
+ const targetFile = path.join(this.saveProjectPath, "manifest", 'package-metadatas-to-save.xml');
568
+ await fs.ensureDir(path.dirname(targetFile));
569
+ if (fs.existsSync(targetFile)) {
570
+ const promptResponse = await prompts({
571
+ type: 'confirm',
572
+ name: 'overwrite',
573
+ message: `The file ${targetFile} already exists. Do you want to overwrite it?`,
574
+ description: 'This file is used to save the metadata that will be restored after org refresh.',
575
+ initial: false
576
+ });
577
+ if (promptResponse.overwrite) {
578
+ uxLog("log", this, c.grey(`Overwriting default save package xml to ${targetFile}`));
579
+ await fs.copy(sourceFile, targetFile, { overwrite: true });
580
+ }
581
+ }
582
+ else {
583
+ uxLog("log", this, c.grey(`Copying default package xml to ${targetFile}`));
584
+ await fs.copy(sourceFile, targetFile, { overwrite: true });
585
+ }
586
+ uxLog("log", this, c.grey(`Save package XML is located at ${targetFile}`));
587
+ WebSocketClient.sendReportFileMessage(targetFile, "Save package XML", 'report');
588
+ return targetFile;
589
+ }
590
+ async retrieveMetadatasToSave(savePackageXml) {
591
+ uxLog("action", this, c.cyan(`Retrieving metadatas to save...`));
592
+ await execCommand(`sf project retrieve start --manifest ${savePackageXml} --target-org ${this.orgUsername} --ignore-conflicts --json`, this, { output: true, fail: true, cwd: this.saveProjectPath });
593
+ }
594
+ async generatePackageXmlToRestore() {
595
+ uxLog("action", this, c.cyan(`Generating new package.xml from saved project path ${this.saveProjectPath}...`));
596
+ const restorePackageXmlFileName = 'package-metadata-to-restore.xml';
597
+ const restorePackageXmlFile = path.join(this.saveProjectPath, 'manifest', restorePackageXmlFileName);
598
+ await execCommand(`sf project generate manifest --source-dir force-app --output-dir manifest --name ${restorePackageXmlFileName} --json`, this, { output: true, fail: true, cwd: this.saveProjectPath });
599
+ uxLog("success", this, c.grey(`Generated package.xml for restore at ${restorePackageXmlFile}`));
600
+ const restorePackage = await parsePackageXmlFile(restorePackageXmlFile);
601
+ if (restorePackage?.["ConnectedApp"]) {
602
+ delete restorePackage["ConnectedApp"];
603
+ await writePackageXmlFile(restorePackageXmlFile, restorePackage);
604
+ uxLog("log", this, c.grey(`Removed ConnectedApps from ${restorePackageXmlFileName} as they will be handled separately`));
605
+ }
606
+ }
607
+ async retrieveCertificates() {
608
+ uxLog("action", this, c.cyan('Retrieving certificates (.crt) from org...'));
609
+ // Retrieve certificates using metadata api coz with source api it does not work
610
+ const certificatesPackageXml = path.join(PACKAGE_ROOT_DIR, 'defaults/refresh-sandbox', 'package-certificates-to-save.xml');
611
+ const packageCertsXml = path.join(this.saveProjectPath, 'manifest', 'package-certificates-to-save.xml');
612
+ uxLog("log", this, c.grey(`Copying default package XML for certificates to ${packageCertsXml}`));
613
+ await fs.copy(certificatesPackageXml, packageCertsXml, { overwrite: true });
614
+ uxLog("log", this, c.grey(`Retrieving certificates from org ${this.instanceUrl} using Metadata API (Source APi does not support it)...`));
615
+ await execSfdxJson(`sf project retrieve start --manifest ${packageCertsXml} --target-org ${this.orgUsername} --target-metadata-dir ./mdapi_certs --unzip`, this, { output: true, fail: true, cwd: this.saveProjectPath });
616
+ // Copy the extracted certificates to the main directory
617
+ const mdapiCertsDir = path.join(this.saveProjectPath, 'mdapi_certs', 'unpackaged', 'unpackaged', 'certs');
618
+ const certsDir = path.join(this.saveProjectPath, 'force-app', 'main', 'default', 'certs');
619
+ uxLog("log", this, c.grey(`Copying certificates from ${mdapiCertsDir} to ${certsDir}`));
620
+ await fs.ensureDir(certsDir);
621
+ await fs.copy(mdapiCertsDir, certsDir, { overwrite: true });
622
+ await fs.remove(path.join(this.saveProjectPath, 'mdapi_certs'));
623
+ uxLog("success", this, c.green(`Successfully retrieved certificates from org and saved them to ${certsDir}`));
624
+ uxLog("action", this, c.cyan('Retrieving certificates definitions (.crt-meta.xml) from org...'));
625
+ // Retrieve certificates definitions using source api
626
+ await execCommand(`sf project retrieve start -m Certificate --target-org ${this.orgUsername} --ignore-conflicts --json`, this, { output: true, fail: true, cwd: this.saveProjectPath });
627
+ }
628
+ async saveCustomSettings() {
629
+ const customSettingsFolder = path.join(this.saveProjectPath, 'savedCustomSettings');
630
+ // If savedCustomSettings is not empty, ask if we want to retrieve them again
631
+ if (fs.existsSync(customSettingsFolder) && fs.readdirSync(customSettingsFolder).length > 0) {
632
+ const confirmRetrieval = await prompts({
633
+ type: 'confirm',
634
+ name: 'retrieveAgain',
635
+ message: `Custom Settings folder is not empty. Do you want to retrieve Custom Settings again?`,
636
+ description: `If you do not retrieve them again, the Custom Settings will not be updated with the latest changes from the org.`,
637
+ initial: false
638
+ });
639
+ if (!confirmRetrieval.retrieveAgain) {
640
+ uxLog("log", this, c.grey(`Skipping Custom Settings retrieval as it already exists at ${customSettingsFolder}`));
641
+ return;
642
+ }
643
+ }
644
+ // List custom settings in the org
645
+ uxLog("action", this, c.cyan(`Listing Custom Settings in the org...`));
646
+ const globalDesc = await this.conn.describeGlobal();
647
+ const customSettings = globalDesc.sobjects.filter(sobject => sobject.customSetting);
648
+ if (customSettings.length === 0) {
649
+ uxLog("warning", this, c.yellow('No Custom Settings found in the org.'));
650
+ return;
651
+ }
652
+ const customSettingsNames = customSettings.map(cs => `- ${cs.name}`).sort().join('\n');
653
+ uxLog("log", this, c.grey(`Found ${customSettings.length} Custom Setting(s) in the org:\n${customSettingsNames}`));
654
+ // Ask user to select which Custom Settings to retrieve
655
+ const selectedSettings = await prompts({
656
+ type: 'multiselect',
657
+ name: 'settings',
658
+ message: 'Select Custom Settings to retrieve',
659
+ description: 'You can select multiple Custom Settings to retrieve.',
660
+ choices: customSettings.map(cs => ({ title: cs.name, value: cs.name })),
661
+ initial: customSettings.map(cs => cs.name),
662
+ });
663
+ if (selectedSettings.settings.length === 0) {
664
+ uxLog("warning", this, c.yellow('No Custom Settings selected for retrieval'));
665
+ return;
666
+ }
667
+ uxLog("action", this, c.cyan(`Retrieving ${selectedSettings.settings.length} selected Custom Settings`));
668
+ const successCs = [];
669
+ const errorCs = [];
670
+ // Retrieve each selected Custom Setting
671
+ for (const settingName of selectedSettings.settings) {
672
+ try {
673
+ uxLog("action", this, c.cyan(`Retrieving Custom Setting: ${settingName}`));
674
+ // List all fields of the Custom Setting using globalDesc
675
+ const customSettingDesc = globalDesc.sobjects.find(sobject => sobject.name === settingName);
676
+ if (!customSettingDesc) {
677
+ uxLog("error", this, c.red(`Custom Setting ${settingName} not found in the org.`));
678
+ errorCs.push(settingName);
679
+ continue;
680
+ }
681
+ const csDescribe = await this.conn.sobject(settingName).describe();
682
+ const fieldList = csDescribe.fields.map(field => field.name).join(', ');
683
+ uxLog("log", this, c.grey(`Fields in Custom Setting ${settingName}: ${fieldList}`));
684
+ // Use data tree export to retrieve the Custom Setting
685
+ uxLog("log", this, c.cyan(`Running tree export for Custom Setting ${settingName}...`));
686
+ const retrieveCommand = `sf data tree export --query "SELECT ${fieldList} FROM ${settingName}" --target-org ${this.orgUsername} --json`;
687
+ const csFolder = path.join(customSettingsFolder, settingName);
688
+ await fs.ensureDir(csFolder);
689
+ const result = await execSfdxJson(retrieveCommand, this, {
690
+ output: true,
691
+ fail: true,
692
+ cwd: csFolder
693
+ });
694
+ if (!(result?.status === 0)) {
695
+ uxLog("error", this, c.red(`Failed to retrieve Custom Setting ${settingName}: ${JSON.stringify(result)}`));
696
+ continue;
697
+ }
698
+ const resultFile = path.join(csFolder, `${settingName}.json`);
699
+ if (fs.existsSync(resultFile)) {
700
+ uxLog("log", this, c.grey(`Custom Setting ${settingName} has been downloaded to ${resultFile}`));
701
+ successCs.push(settingName);
702
+ }
703
+ else {
704
+ uxLog("warning", this, c.red(`Custom Setting ${settingName} was not retrieved correctly, or has no values. No file found at ${resultFile}`));
705
+ errorCs.push(settingName);
706
+ continue;
707
+ }
708
+ }
709
+ catch (error) {
710
+ errorCs.push(settingName);
711
+ uxLog("error", this, c.red(`Error retrieving Custom Setting ${settingName}: ${error.message || error}`));
712
+ }
713
+ }
714
+ uxLog("action", this, c.cyan(`Custom Settings retrieval completed (${successCs.length} successful, ${errorCs.length} failed)`));
715
+ if (successCs.length > 0) {
716
+ const successCsNames = successCs.map(cs => "- " + cs).join('\n');
717
+ uxLog("success", this, c.green(`Successfully retrieved Custom Settings:\n${successCsNames}`));
718
+ }
719
+ if (errorCs.length > 0) {
720
+ const errorCsNames = errorCs.map(cs => "- " + cs).join('\n');
721
+ uxLog("error", this, c.red(`Failed to retrieve Custom Settings:\n${errorCsNames}`));
722
+ }
723
+ }
724
+ }
725
+ //# sourceMappingURL=before-refresh.js.map