@pdfbutler/migration-cli 0.0.26 → 0.0.28
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.
- package/README.md +193 -30
- package/lib/commands/butler/pb/admincredentials.d.ts +0 -5
- package/lib/commands/butler/pb/admincredentials.js +46 -115
- package/lib/commands/butler/pb/admincredentials.js.map +1 -1
- package/lib/commands/butler/pb/adminsettings.d.ts +1 -7
- package/lib/commands/butler/pb/adminsettings.js +41 -110
- package/lib/commands/butler/pb/adminsettings.js.map +1 -1
- package/lib/commands/butler/pb/export.d.ts +10 -8
- package/lib/commands/butler/pb/export.js +302 -312
- package/lib/commands/butler/pb/export.js.map +1 -1
- package/lib/commands/butler/pb/exportdatasource.d.ts +27 -0
- package/lib/commands/butler/pb/exportdatasource.js +161 -0
- package/lib/commands/butler/pb/exportdatasource.js.map +1 -0
- package/lib/commands/butler/pb/exportpack.d.ts +0 -5
- package/lib/commands/butler/pb/exportpack.js +60 -142
- package/lib/commands/butler/pb/exportpack.js.map +1 -1
- package/lib/commands/butler/pb/import.d.ts +16 -9
- package/lib/commands/butler/pb/import.js +400 -267
- package/lib/commands/butler/pb/import.js.map +1 -1
- package/lib/commands/butler/pb/importdatasource.d.ts +30 -0
- package/lib/commands/butler/pb/importdatasource.js +250 -0
- package/lib/commands/butler/pb/importdatasource.js.map +1 -0
- package/lib/commands/butler/pb/importpack.d.ts +0 -5
- package/lib/commands/butler/pb/importpack.js +57 -138
- package/lib/commands/butler/pb/importpack.js.map +1 -1
- package/lib/commands/butler/sb/export/signtemplate.d.ts +23 -0
- package/lib/commands/butler/sb/export/signtemplate.js +111 -0
- package/lib/commands/butler/sb/export/signtemplate.js.map +1 -0
- package/lib/commands/butler/sb/import/signtemplate.d.ts +23 -0
- package/lib/commands/butler/sb/import/signtemplate.js +108 -0
- package/lib/commands/butler/sb/import/signtemplate.js.map +1 -0
- package/lib/utils/authentication.d.ts +4 -0
- package/lib/utils/authentication.js +69 -0
- package/lib/utils/authentication.js.map +1 -0
- package/lib/utils/constants.d.ts +137 -0
- package/lib/utils/constants.js +141 -0
- package/lib/utils/constants.js.map +1 -0
- package/lib/utils/extract-templates.d.ts +1 -0
- package/lib/utils/extract-templates.js +89 -0
- package/lib/utils/extract-templates.js.map +1 -0
- package/lib/utils/logger.d.ts +9 -0
- package/lib/utils/logger.js +51 -0
- package/lib/utils/logger.js.map +1 -0
- package/messages/butler.pb.export.md +4 -0
- package/messages/butler.pb.exportdatasource.md +51 -0
- package/messages/butler.pb.import.md +4 -0
- package/messages/butler.pb.importdatasource.md +51 -0
- package/messages/butler.sb.export.signtemplate.md +35 -0
- package/messages/butler.sb.import.signtemplate.md +35 -0
- package/oclif.manifest.json +321 -48
- package/package.json +223 -215
|
@@ -1,355 +1,345 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
6
|
-
/* eslint-disable complexity */
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-misused-promises */
|
|
8
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
9
|
-
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
10
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
11
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
12
|
-
/* eslint-disable sf-plugin/no-missing-messages */
|
|
13
|
-
/* eslint-disable @typescript-eslint/prefer-for-of */
|
|
14
|
-
/* eslint-disable no-await-in-loop */
|
|
15
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
16
|
-
/* eslint-disable no-console */
|
|
17
|
-
const fs_1 = require("fs");
|
|
39
|
+
const node_fs_1 = require("node:fs");
|
|
40
|
+
const path = __importStar(require("node:path"));
|
|
18
41
|
const sf_plugins_core_1 = require("@salesforce/sf-plugins-core");
|
|
19
42
|
const core_1 = require("@salesforce/core");
|
|
20
43
|
const AdmZip = require("adm-zip");
|
|
21
|
-
const got_1 = require("got");
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const MSG_SPLITTED_DOC_CONFIGS = 'Number of DocConfig Ids after splitting: [VALUE].';
|
|
27
|
-
const MSG_AUTHORIZATION = 'Authorization with the username: [VALUE].';
|
|
28
|
-
const MSG_EXPORTED_DOC_CONFIGS = 'Number of exported DocConfigs: [VALUE].';
|
|
29
|
-
const MSG_FOLDER_CREATED = 'Folder structure created at: [VALUE].';
|
|
30
|
-
const MSG_DOC_CONFIG_HAS_NO_BACKEND = 'DocConfig with ID - [VALUE], does not have a backend.';
|
|
31
|
-
const MSG_NO_AUTH_PARAMS = 'No authentication parameters provided. Please provide either username or session and instance parameters.';
|
|
32
|
-
const MSG_NO_MATCHING_DOC_CONFIGS = 'No matching Doc Configs ids found.';
|
|
33
|
-
const MSG_CONNECTION_FAILED = 'Connection to Salesforce has been failed.';
|
|
34
|
-
const MSG_EXPORT_FAILED = 'DocConfig export has failed. Check the IDs and try again.';
|
|
35
|
-
const MSG_BACKEND_DATA_ARCHIVED = 'Exported data from backend has been archived.';
|
|
36
|
-
const MSG_BLANK_BACKEND = 'One or more docConfigs have no backend.';
|
|
37
|
-
const MSG_USER_HAS_PERMISSIONS = 'User has read/write permissions.';
|
|
38
|
-
const MSG_USER_HAS_NO_PERMISSIONS = "User doesn't have read/write permissions.";
|
|
39
|
-
const MSG_ARCHIVE_UNZIPPED = 'Archive unzipped and recorded locally.';
|
|
40
|
-
const MSG_ARCHIVE_RECORDED = 'Archive recorded locally.';
|
|
41
|
-
const MSG_CREATION_CONNECTION = 'Creating Connection with the Salesforce org.';
|
|
42
|
-
const MSG_FETCH_DATA = 'Attempting to fetch data from the org.';
|
|
43
|
-
const MSG_UNZIP_FILES = 'Unzipping the files.';
|
|
44
|
-
const MSG_SAVE_AS_ZIP = 'Saving as zip.';
|
|
45
|
-
const MSG_SAVE_BACKEND = 'Saving backend only.';
|
|
46
|
-
const MSG_PARTIAL_EXPORT_ACTIVE = 'The partial export is active. DocConfigs that do not have a backend will not be exported.';
|
|
47
|
-
const MSG_TRY_TO_GET_DOC_CONFIG_BACKEND = 'Trying to get backend of DocConfig with ID: [VALUE]';
|
|
48
|
-
const MSG_START_EXPORT_BACKEND = 'Starting to export DocConfigs backend.';
|
|
49
|
-
const MSG_RUN_AUTH_COMMAND = 'Run the command "sf org login web" before authenticating with the target.';
|
|
50
|
-
const MSG_CHECK_SESSION_INSTANCE = 'Check if the session is up to date and if the instance is correct.';
|
|
44
|
+
const got_1 = __importDefault(require("got"));
|
|
45
|
+
const logger_1 = require("../../../utils/logger");
|
|
46
|
+
const constants_1 = require("../../../utils/constants");
|
|
47
|
+
const authentication_1 = require("../../../utils/authentication");
|
|
48
|
+
const extract_templates_1 = require("../../../utils/extract-templates");
|
|
51
49
|
core_1.Messages.importMessagesDirectory(__dirname);
|
|
52
50
|
const messages = core_1.Messages.loadMessages('@pdfbutler/migration-cli', 'butler.pb.export');
|
|
53
51
|
class ButlerPbExport extends sf_plugins_core_1.SfCommand {
|
|
52
|
+
static summary = messages.getMessage('summary');
|
|
53
|
+
static description = messages.getMessage('description');
|
|
54
|
+
static examples = messages.getMessages('examples');
|
|
55
|
+
static flags = {
|
|
56
|
+
name: sf_plugins_core_1.Flags.string({
|
|
57
|
+
summary: messages.getMessage('flags.name.summary'),
|
|
58
|
+
description: messages.getMessage('flags.name.description'),
|
|
59
|
+
char: 'n',
|
|
60
|
+
required: false,
|
|
61
|
+
}),
|
|
62
|
+
target: sf_plugins_core_1.Flags.string({
|
|
63
|
+
summary: messages.getMessage('flags.target.summary'),
|
|
64
|
+
char: 't',
|
|
65
|
+
required: false,
|
|
66
|
+
}),
|
|
67
|
+
id: sf_plugins_core_1.Flags.string({
|
|
68
|
+
summary: messages.getMessage('flags.id.summary'),
|
|
69
|
+
char: 'i',
|
|
70
|
+
required: true,
|
|
71
|
+
}),
|
|
72
|
+
out: sf_plugins_core_1.Flags.string({
|
|
73
|
+
summary: messages.getMessage('flags.out.summary'),
|
|
74
|
+
// eslint-disable-next-line sf-plugin/dash-o
|
|
75
|
+
char: 'o',
|
|
76
|
+
required: true,
|
|
77
|
+
}),
|
|
78
|
+
backend: sf_plugins_core_1.Flags.boolean({
|
|
79
|
+
summary: messages.getMessage('flags.backend.summary'),
|
|
80
|
+
char: 'b',
|
|
81
|
+
}),
|
|
82
|
+
'auth-env-var': sf_plugins_core_1.Flags.string({
|
|
83
|
+
summary: messages.getMessage('flags.auth-env-var.summary'),
|
|
84
|
+
char: 'a',
|
|
85
|
+
}),
|
|
86
|
+
endpoint: sf_plugins_core_1.Flags.url({
|
|
87
|
+
summary: messages.getMessage('flags.endpoint.summary'),
|
|
88
|
+
char: 'e',
|
|
89
|
+
}),
|
|
90
|
+
stage: sf_plugins_core_1.Flags.string({
|
|
91
|
+
summary: messages.getMessage('flags.stage.summary'),
|
|
92
|
+
char: 's',
|
|
93
|
+
required: true,
|
|
94
|
+
}),
|
|
95
|
+
unzip: sf_plugins_core_1.Flags.boolean({
|
|
96
|
+
summary: messages.getMessage('flags.unzip.summary'),
|
|
97
|
+
char: 'u',
|
|
98
|
+
}),
|
|
99
|
+
logs: sf_plugins_core_1.Flags.boolean({
|
|
100
|
+
summary: messages.getMessage('flags.logs.summary'),
|
|
101
|
+
char: 'l',
|
|
102
|
+
}),
|
|
103
|
+
partial: sf_plugins_core_1.Flags.boolean({
|
|
104
|
+
summary: messages.getMessage('flags.partial.summary'),
|
|
105
|
+
char: 'p',
|
|
106
|
+
}),
|
|
107
|
+
session: sf_plugins_core_1.Flags.string({
|
|
108
|
+
summary: messages.getMessage('flags.session.summary'),
|
|
109
|
+
}),
|
|
110
|
+
instance: sf_plugins_core_1.Flags.string({
|
|
111
|
+
summary: messages.getMessage('flags.instance.summary'),
|
|
112
|
+
}),
|
|
113
|
+
cut: sf_plugins_core_1.Flags.boolean({
|
|
114
|
+
summary: messages.getMessage('flags.cut.summary'),
|
|
115
|
+
char: 'c',
|
|
116
|
+
}),
|
|
117
|
+
shortpaths: sf_plugins_core_1.Flags.boolean({
|
|
118
|
+
summary: messages.getMessage('flags.shortpaths.summary'),
|
|
119
|
+
}),
|
|
120
|
+
templates: sf_plugins_core_1.Flags.boolean({
|
|
121
|
+
summary: messages.getMessage('flags.templates.summary'),
|
|
122
|
+
char: 'm',
|
|
123
|
+
}),
|
|
124
|
+
};
|
|
125
|
+
static isLogsActive = false;
|
|
126
|
+
processedIds = new Set();
|
|
54
127
|
static getDocConfigIds(id) {
|
|
55
|
-
const docConfigIds = id
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
this.log(MSG_SPLITTED_DOC_CONFIGS.replace('[VALUE]', docConfigIds.length.toString()));
|
|
63
|
-
if (docConfigIds.length < 0) {
|
|
64
|
-
throw new Error(MSG_NO_MATCHING_DOC_CONFIGS);
|
|
128
|
+
const docConfigIds = id
|
|
129
|
+
.split(constants_1.PATTERN.DOC_CONFIG)
|
|
130
|
+
.map((docId) => docId.trim())
|
|
131
|
+
.filter((docId) => docId.length >= 36);
|
|
132
|
+
logger_1.Logger.info(constants_1.MESSAGE.SPLITTED_DOC_CONFIGS.replace('[VALUE]', docConfigIds.length.toString()), this.isLogsActive);
|
|
133
|
+
if (docConfigIds.length === 0) {
|
|
134
|
+
throw new Error(constants_1.ERROR.NO_MATCHING_DOC_CONFIGS);
|
|
65
135
|
}
|
|
66
136
|
return docConfigIds;
|
|
67
137
|
}
|
|
68
|
-
static
|
|
69
|
-
return value === ''
|
|
70
|
-
|| value === null
|
|
71
|
-
|| value === undefined;
|
|
72
|
-
}
|
|
73
|
-
static log(message, type = 'info') {
|
|
74
|
-
const prefix = type === 'info' ? '[INFO]' : '[ERROR]';
|
|
75
|
-
console.log(`${prefix} ${message}`);
|
|
76
|
-
}
|
|
77
|
-
static async authenticateWithTarget(username) {
|
|
138
|
+
static async getDocConfigs(docConfigIds, connection) {
|
|
78
139
|
try {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
this.log(`Connected to ${username} (Org ID: ${authInfo.getFields().orgId})`);
|
|
86
|
-
return connection;
|
|
140
|
+
logger_1.Logger.info(constants_1.MESSAGE.FETCH_DATA, ButlerPbExport.isLogsActive);
|
|
141
|
+
const docConfigPromises = docConfigIds.map((docConfigId) => {
|
|
142
|
+
const endpoint = `${constants_1.PATH.EXPORT_DOC_CONFIG}${docConfigId}`;
|
|
143
|
+
return connection.apex.get(endpoint);
|
|
144
|
+
});
|
|
145
|
+
return await Promise.all(docConfigPromises);
|
|
87
146
|
}
|
|
88
|
-
catch (
|
|
89
|
-
|
|
90
|
-
throw new Error(MSG_CONNECTION_FAILED);
|
|
147
|
+
catch (e) {
|
|
148
|
+
throw new Error(constants_1.ERROR.EXPORT_FAILED);
|
|
91
149
|
}
|
|
92
150
|
}
|
|
93
|
-
static
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const connection = await new jsforce.Connection({
|
|
100
|
-
instanceUrl: `${instance}`,
|
|
101
|
-
sessionId: `${session}`,
|
|
102
|
-
});
|
|
103
|
-
this.log(`Connected to ${connection.instanceUrl}`);
|
|
104
|
-
return connection;
|
|
151
|
+
static convertBufferToUtf8(files) {
|
|
152
|
+
for (const file of files.getEntries()) {
|
|
153
|
+
if (file.entryName.endsWith('.json')) {
|
|
154
|
+
const parsed = JSON.parse(file.getData().toString('utf8'));
|
|
155
|
+
file.setData(JSON.stringify(parsed, null, 2));
|
|
156
|
+
}
|
|
105
157
|
}
|
|
106
|
-
|
|
107
|
-
|
|
158
|
+
return files;
|
|
159
|
+
}
|
|
160
|
+
static async logPermissionStatus(outDir) {
|
|
161
|
+
if (!ButlerPbExport.isLogsActive)
|
|
162
|
+
return;
|
|
163
|
+
try {
|
|
164
|
+
await node_fs_1.promises.access(path.resolve(outDir), node_fs_1.promises.constants.R_OK | node_fs_1.promises.constants.W_OK);
|
|
165
|
+
logger_1.Logger.info(constants_1.MESSAGE.USER_HAS_PERMISSIONS);
|
|
108
166
|
}
|
|
109
|
-
|
|
110
|
-
|
|
167
|
+
catch (err) {
|
|
168
|
+
logger_1.Logger.error(constants_1.ERROR.USER_NO_PERMISSIONS);
|
|
111
169
|
}
|
|
112
|
-
|
|
113
|
-
|
|
170
|
+
}
|
|
171
|
+
static getDocConfigResponse(docConfigId, idsToProcess, docConfigResponses) {
|
|
172
|
+
const responseIndex = idsToProcess.indexOf(docConfigId);
|
|
173
|
+
if (responseIndex < 0) {
|
|
174
|
+
throw new Error(constants_1.ERROR.EXPORT_FAILED);
|
|
114
175
|
}
|
|
115
|
-
|
|
116
|
-
throw new Error(MSG_CONNECTION_FAILED);
|
|
176
|
+
return docConfigResponses[responseIndex];
|
|
117
177
|
}
|
|
118
|
-
static async
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
178
|
+
static async fetchBackendZips(idsToProcess, flags, isUseShortPath) {
|
|
179
|
+
const zipMap = new Map();
|
|
180
|
+
let lastBuffer = Buffer.from('');
|
|
181
|
+
if (flags.backend) {
|
|
182
|
+
return { zipMap, lastBuffer };
|
|
122
183
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
184
|
+
const uniqueIds = Array.from(new Set(idsToProcess));
|
|
185
|
+
logger_1.Logger.info(constants_1.MESSAGE.START_EXPORT_BACKEND, ButlerPbExport.isLogsActive);
|
|
186
|
+
const auth = flags['auth-env-var'];
|
|
187
|
+
const base64Auth = auth?.includes(':') ? Buffer.from(auth, 'utf8').toString('base64') : auth ?? '';
|
|
188
|
+
logger_1.Logger.info(constants_1.MESSAGE.PARTIAL_EXPORT_ACTIVE, ButlerPbExport.isLogsActive && Boolean(flags.partial));
|
|
189
|
+
const endpointBaseUrl = flags.endpoint?.toString() ?? '';
|
|
190
|
+
if (ButlerPbExport.isLogsActive) {
|
|
191
|
+
logger_1.Logger.info(constants_1.MESSAGE.BACKEND_RETRIEVAL_START.replace('[COUNT]', uniqueIds.length.toString()).replace('[IDS]', JSON.stringify(uniqueIds, null, 2)));
|
|
192
|
+
}
|
|
193
|
+
await uniqueIds.reduce(async (chain, docConfigId, index) => {
|
|
194
|
+
await chain;
|
|
195
|
+
if (ButlerPbExport.isLogsActive) {
|
|
196
|
+
logger_1.Logger.info(constants_1.MESSAGE.BACKEND_RETRIEVING.replace('[INDEX]', (index + 1).toString())
|
|
197
|
+
.replace('[COUNT]', uniqueIds.length.toString())
|
|
198
|
+
.replace('[ID]', docConfigId));
|
|
126
199
|
}
|
|
127
|
-
|
|
128
|
-
|
|
200
|
+
let finalUrl = `${endpointBaseUrl}config/export-config/${flags.stage}/${docConfigId}`;
|
|
201
|
+
if (isUseShortPath) {
|
|
202
|
+
logger_1.Logger.info(constants_1.MESSAGE.USED_SHORT_PATH);
|
|
203
|
+
finalUrl += '?shortPaths=true';
|
|
129
204
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
205
|
+
try {
|
|
206
|
+
lastBuffer = await (0, got_1.default)(finalUrl, {
|
|
207
|
+
headers: { Authorization: `Basic ${base64Auth}` },
|
|
208
|
+
}).buffer();
|
|
209
|
+
zipMap.set(docConfigId, new AdmZip(lastBuffer));
|
|
210
|
+
if (ButlerPbExport.isLogsActive) {
|
|
211
|
+
logger_1.Logger.success(constants_1.MESSAGE.BACKEND_RETRIEVED.replace('[INDEX]', (index + 1).toString())
|
|
212
|
+
.replace('[COUNT]', uniqueIds.length.toString())
|
|
213
|
+
.replace('[ID]', docConfigId));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
catch (err) {
|
|
217
|
+
const message = err instanceof Error ? err.message : JSON.stringify(err);
|
|
218
|
+
logger_1.Logger.error(message);
|
|
219
|
+
logger_1.Logger.error(constants_1.MESSAGE.DOC_CONFIG_NO_BACKEND.replace('[VALUE]', docConfigId), ButlerPbExport.isLogsActive);
|
|
220
|
+
if (!flags.partial) {
|
|
221
|
+
throw new Error(constants_1.ERROR.BLANK_BACKEND);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}, Promise.resolve());
|
|
225
|
+
if (uniqueIds.length > 0) {
|
|
226
|
+
logger_1.Logger.info(constants_1.MESSAGE.BACKEND_DATA_ARCHIVED_PROGRESS
|
|
227
|
+
.replace('[EXPORTED]', zipMap.size.toString())
|
|
228
|
+
.replace('[TOTAL]', uniqueIds.length.toString()), ButlerPbExport.isLogsActive);
|
|
134
229
|
}
|
|
230
|
+
return { zipMap, lastBuffer };
|
|
135
231
|
}
|
|
136
|
-
static async
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const endpoint = `${PATH_EXPORT}${docConfigId}`;
|
|
144
|
-
return connection.apex.get(endpoint);
|
|
145
|
-
});
|
|
146
|
-
responses = await Promise.all(docConfigPromises);
|
|
147
|
-
}
|
|
148
|
-
catch (e) {
|
|
149
|
-
throw new Error(MSG_EXPORT_FAILED);
|
|
150
|
-
}
|
|
151
|
-
return responses;
|
|
232
|
+
static async writeBackendOnly(idsToProcess, docConfigResponses, outDir) {
|
|
233
|
+
const tasks = idsToProcess.map(async (docId) => {
|
|
234
|
+
const data = JSON.stringify(ButlerPbExport.getDocConfigResponse(docId, idsToProcess, docConfigResponses), null, 2);
|
|
235
|
+
await node_fs_1.promises.writeFile(path.join(outDir, 'sfdc', `${docId}.json`), data);
|
|
236
|
+
});
|
|
237
|
+
await Promise.all(tasks);
|
|
238
|
+
logger_1.Logger.success(constants_1.MESSAGE.SAVE_BACKEND);
|
|
152
239
|
}
|
|
153
|
-
static
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
240
|
+
static async processZipMapUnzip(zipMap, idsToProcess, docConfigResponses, outDir, shouldExtractTemplates) {
|
|
241
|
+
const entryTasks = Array.from(zipMap.entries()).map(async ([key, value]) => {
|
|
242
|
+
const extractionPath = path.join(outDir, key);
|
|
243
|
+
ButlerPbExport.convertBufferToUtf8(value).extractAllTo(extractionPath, false);
|
|
244
|
+
const foundIds = shouldExtractTemplates
|
|
245
|
+
? await (0, extract_templates_1.extractTemplateIds)(extractionPath)
|
|
246
|
+
: [];
|
|
247
|
+
const data = JSON.stringify(ButlerPbExport.getDocConfigResponse(key, idsToProcess, docConfigResponses), null, 2);
|
|
248
|
+
await node_fs_1.promises.writeFile(path.join(outDir, 'sfdc', `${key}.json`), data);
|
|
249
|
+
return foundIds;
|
|
158
250
|
});
|
|
159
|
-
|
|
251
|
+
const discoveredTemplateIds = await Promise.all(entryTasks);
|
|
252
|
+
return discoveredTemplateIds.flat();
|
|
160
253
|
}
|
|
161
|
-
async
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const instance = flags.instance;
|
|
171
|
-
docConfigIds = ButlerPbExport.getDocConfigIds(flags.id);
|
|
172
|
-
ButlerPbExport.log(JSON.stringify(docConfigIds, null, 2));
|
|
173
|
-
const connection = await ButlerPbExport.authenticate(targetUsername, session, instance);
|
|
174
|
-
docConfigResponses = await ButlerPbExport.getDocConfigs(docConfigIds, connection);
|
|
175
|
-
if (ButlerPbExport.isLogsActive)
|
|
176
|
-
ButlerPbExport.log(MSG_EXPORTED_DOC_CONFIGS.replace('[VALUE]', docConfigResponses.length.toString()));
|
|
177
|
-
if (docConfigResponses.length < 1)
|
|
178
|
-
throw new Error(MSG_NO_MATCHING_DOC_CONFIGS);
|
|
179
|
-
let zipMap = new Map();
|
|
180
|
-
let result = Buffer.from('');
|
|
181
|
-
if (!flags.backend) {
|
|
182
|
-
if (ButlerPbExport.isLogsActive)
|
|
183
|
-
ButlerPbExport.log(MSG_START_EXPORT_BACKEND);
|
|
184
|
-
const auth = flags['auth-env-var'];
|
|
185
|
-
let base64Auth = auth;
|
|
186
|
-
if (auth?.indexOf(':')) {
|
|
187
|
-
base64Auth = Buffer.from(auth).toString('base64');
|
|
254
|
+
static async processZipMapZip(zipMap, idsToProcess, docConfigResponses, flags, outDir, shouldExtractTemplates) {
|
|
255
|
+
const entryTasks = Array.from(zipMap.entries()).map(async ([key, value]) => {
|
|
256
|
+
const zipToExport = new AdmZip();
|
|
257
|
+
let foundIds = [];
|
|
258
|
+
if (shouldExtractTemplates) {
|
|
259
|
+
const tempExtractPath = path.join(outDir, `.temp_${key}`);
|
|
260
|
+
ButlerPbExport.convertBufferToUtf8(value).extractAllTo(tempExtractPath, false);
|
|
261
|
+
foundIds = await (0, extract_templates_1.extractTemplateIds)(tempExtractPath);
|
|
262
|
+
await node_fs_1.promises.rm(tempExtractPath, { recursive: true, force: true });
|
|
188
263
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
finalUrl += '?shortPaths=true';
|
|
198
|
-
}
|
|
199
|
-
try {
|
|
200
|
-
result = await (0, got_1.default)(finalUrl, {
|
|
201
|
-
headers: {
|
|
202
|
-
Authorization: `Basic ${base64Auth}`,
|
|
203
|
-
},
|
|
204
|
-
}).buffer();
|
|
205
|
-
zipMap.set(docConfigId, new AdmZip(result));
|
|
206
|
-
if (ButlerPbExport.isLogsActive)
|
|
207
|
-
ButlerPbExport.log(MSG_BACKEND_DATA_ARCHIVED);
|
|
208
|
-
}
|
|
209
|
-
catch (err) {
|
|
210
|
-
ButlerPbExport.log(err, 'error');
|
|
211
|
-
if (ButlerPbExport.isLogsActive)
|
|
212
|
-
ButlerPbExport.log(MSG_DOC_CONFIG_HAS_NO_BACKEND.replace('[VALUE]', docConfigId));
|
|
213
|
-
if (!flags.partial) {
|
|
214
|
-
throw new Error(MSG_BLANK_BACKEND);
|
|
215
|
-
}
|
|
264
|
+
for (const entry of value.getEntries()) {
|
|
265
|
+
let entryName = entry.entryName;
|
|
266
|
+
const fileName = entryName.substring(Number(entryName.lastIndexOf('/')) + 1, entryName.lastIndexOf('.'));
|
|
267
|
+
entryName = flags.cut ? entryName.replace(fileName, fileName.substring(0, constants_1.CUT.AFTER)) : entryName;
|
|
268
|
+
const entryData = value.readFile(entry);
|
|
269
|
+
if (!entryData) {
|
|
270
|
+
logger_1.Logger.error(constants_1.ERROR.FAILED_TO_SAVE_FILE);
|
|
271
|
+
continue;
|
|
216
272
|
}
|
|
273
|
+
zipToExport.addFile(entryName, entryData);
|
|
217
274
|
}
|
|
275
|
+
zipToExport.addFile('sfdc/docconfig.json', Buffer.from(JSON.stringify(ButlerPbExport.getDocConfigResponse(key, idsToProcess, docConfigResponses), null, 2)));
|
|
276
|
+
const zipData = new Uint8Array(zipToExport.toBuffer());
|
|
277
|
+
await node_fs_1.promises.writeFile(path.join(outDir, `${key}.zip`), zipData);
|
|
278
|
+
const data = JSON.stringify(ButlerPbExport.getDocConfigResponse(key, idsToProcess, docConfigResponses), null, 2);
|
|
279
|
+
await node_fs_1.promises.writeFile(path.join(outDir, 'sfdc', `${key}.json`), data);
|
|
280
|
+
return foundIds;
|
|
281
|
+
});
|
|
282
|
+
const discoveredTemplateIds = await Promise.all(entryTasks);
|
|
283
|
+
return discoveredTemplateIds.flat();
|
|
284
|
+
}
|
|
285
|
+
static async processZipMap(zipMap, idsToProcess, docConfigResponses, flags, outDir, shouldExtractTemplates) {
|
|
286
|
+
if (zipMap.size === 0) {
|
|
287
|
+
await ButlerPbExport.writeBackendOnly(idsToProcess, docConfigResponses, outDir);
|
|
288
|
+
return [];
|
|
218
289
|
}
|
|
219
|
-
|
|
220
|
-
|
|
290
|
+
if (flags.unzip) {
|
|
291
|
+
logger_1.Logger.info(constants_1.MESSAGE.UNZIP_FILES, ButlerPbExport.isLogsActive);
|
|
292
|
+
const discoveredTemplateIds = await ButlerPbExport.processZipMapUnzip(zipMap, idsToProcess, docConfigResponses, outDir, shouldExtractTemplates);
|
|
293
|
+
logger_1.Logger.success(constants_1.MESSAGE.ARCHIVE_UNZIPPED, ButlerPbExport.isLogsActive);
|
|
294
|
+
return discoveredTemplateIds;
|
|
221
295
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
296
|
+
logger_1.Logger.info(constants_1.MESSAGE.SAVE_AS_ZIP, ButlerPbExport.isLogsActive);
|
|
297
|
+
const discoveredTemplateIds = await ButlerPbExport.processZipMapZip(zipMap, idsToProcess, docConfigResponses, flags, outDir, shouldExtractTemplates);
|
|
298
|
+
logger_1.Logger.success(constants_1.MESSAGE.ARCHIVE_RECORDED);
|
|
299
|
+
return discoveredTemplateIds;
|
|
300
|
+
}
|
|
301
|
+
async run() {
|
|
302
|
+
const parsed = (await this.parse(ButlerPbExport));
|
|
303
|
+
const flags = parsed.flags;
|
|
304
|
+
ButlerPbExport.isLogsActive = Boolean(flags.logs);
|
|
305
|
+
const outDir = flags.out;
|
|
306
|
+
const isUseShortPath = Boolean(flags.shortpaths);
|
|
307
|
+
const shouldExtractTemplates = Boolean(flags.templates);
|
|
308
|
+
const targetUsername = flags.target ?? '';
|
|
309
|
+
const session = flags.session ?? '';
|
|
310
|
+
const instance = flags.instance ?? '';
|
|
311
|
+
const connection = await (0, authentication_1.authenticate)(targetUsername, session, instance);
|
|
312
|
+
const initialBatch = ButlerPbExport.getDocConfigIds(flags.id);
|
|
313
|
+
let result = Buffer.from('');
|
|
314
|
+
const processBatch = async (batch) => {
|
|
315
|
+
const idsToProcess = batch.filter((id) => !this.processedIds.has(id));
|
|
316
|
+
if (idsToProcess.length === 0)
|
|
317
|
+
return;
|
|
318
|
+
for (const id of idsToProcess) {
|
|
319
|
+
this.processedIds.add(id);
|
|
229
320
|
}
|
|
230
|
-
|
|
231
|
-
|
|
321
|
+
const uniqueIds = Array.from(new Set(idsToProcess));
|
|
322
|
+
const docConfigResponses = await ButlerPbExport.getDocConfigs(uniqueIds, connection);
|
|
323
|
+
logger_1.Logger.info(constants_1.MESSAGE.EXPORTED_DOC_CONFIGS.replace('[VALUE]', docConfigResponses.length.toString()), ButlerPbExport.isLogsActive);
|
|
324
|
+
if (docConfigResponses.length < 1) {
|
|
325
|
+
throw new Error(constants_1.ERROR.NO_MATCHING_DOC_CONFIGS);
|
|
232
326
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (ButlerPbExport.isLogsActive)
|
|
237
|
-
ButlerPbExport.log(MSG_UNZIP_FILES);
|
|
238
|
-
const extractionAndWritePromises = Array.from(zipMap.entries()).map(async ([key, value]) => {
|
|
239
|
-
ButlerPbExport.convertBufferToUtf8(value).extractAllTo(`./${outDir}/${key}`, false);
|
|
240
|
-
const filePath = `./${outDir}/sfdc/${key}.json`;
|
|
241
|
-
const data = JSON.stringify(docConfigResponses[docConfigIds.indexOf(key)], null, 2);
|
|
242
|
-
await fs_1.promises.writeFile(filePath, data);
|
|
243
|
-
});
|
|
244
|
-
await Promise.all(extractionAndWritePromises);
|
|
245
|
-
if (ButlerPbExport.isLogsActive)
|
|
246
|
-
ButlerPbExport.log(MSG_ARCHIVE_UNZIPPED);
|
|
327
|
+
const { zipMap, lastBuffer } = await ButlerPbExport.fetchBackendZips(uniqueIds, flags, isUseShortPath);
|
|
328
|
+
if (lastBuffer.length > 0) {
|
|
329
|
+
result = lastBuffer;
|
|
247
330
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
let entryName = entry.entryName;
|
|
255
|
-
const fileName = entryName.substring(Number(entryName.lastIndexOf('/')) + 1, entryName.lastIndexOf('.'));
|
|
256
|
-
entryName = flags.cut ?
|
|
257
|
-
entryName.replace(fileName, fileName.substring(0, CUT_AFTER))
|
|
258
|
-
: entryName;
|
|
259
|
-
zipToExport.addFile(entryName, value.readFile(entry));
|
|
260
|
-
}
|
|
261
|
-
zipToExport.addFile('sfdc/docconfig.json', Buffer.from(JSON.stringify(docConfigResponses[docConfigIds.indexOf(key)], null, 2)));
|
|
262
|
-
const zipPromise = fs_1.promises.writeFile(`./${outDir}/${key}.zip`, zipToExport.toBuffer());
|
|
263
|
-
const jsonPromise = fs_1.promises.writeFile(`./${outDir}/sfdc/${key}.json`, JSON.stringify(docConfigResponses[docConfigIds.indexOf(key)], null, 2));
|
|
264
|
-
await Promise.all([zipPromise, jsonPromise]);
|
|
265
|
-
});
|
|
266
|
-
await Promise.all(zipAndWritePromises);
|
|
267
|
-
if (ButlerPbExport.isLogsActive)
|
|
268
|
-
ButlerPbExport.log(MSG_ARCHIVE_RECORDED);
|
|
331
|
+
await node_fs_1.promises.mkdir(path.join(outDir, 'sfdc'), { recursive: true });
|
|
332
|
+
await ButlerPbExport.logPermissionStatus(outDir);
|
|
333
|
+
const discoveredTemplateIds = await ButlerPbExport.processZipMap(zipMap, uniqueIds, docConfigResponses, flags, outDir, shouldExtractTemplates);
|
|
334
|
+
const nextBatch = discoveredTemplateIds.filter((id) => !this.processedIds.has(id));
|
|
335
|
+
if (nextBatch.length > 0) {
|
|
336
|
+
logger_1.Logger.info(constants_1.MESSAGE.NEW_TEMPLATES_FOUND.replace('[VALUE]', nextBatch.length.toString()), ButlerPbExport.isLogsActive);
|
|
269
337
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
const filePath = `./${outDir}/sfdc/${docConfigId}.json`;
|
|
274
|
-
const data = JSON.stringify(docConfigResponses[docConfigIds.indexOf(docConfigId)], null, 2);
|
|
275
|
-
await fs_1.promises.writeFile(filePath, data);
|
|
276
|
-
});
|
|
277
|
-
await Promise.all(writePromises);
|
|
278
|
-
if (ButlerPbExport.isLogsActive)
|
|
279
|
-
ButlerPbExport.log(MSG_SAVE_BACKEND);
|
|
280
|
-
}
|
|
338
|
+
await processBatch(nextBatch);
|
|
339
|
+
};
|
|
340
|
+
await processBatch(initialBatch);
|
|
281
341
|
return result;
|
|
282
342
|
}
|
|
283
343
|
}
|
|
284
|
-
ButlerPbExport.summary = messages.getMessage('summary');
|
|
285
|
-
ButlerPbExport.description = messages.getMessage('description');
|
|
286
|
-
ButlerPbExport.examples = messages.getMessages('examples');
|
|
287
|
-
ButlerPbExport.flags = {
|
|
288
|
-
name: sf_plugins_core_1.Flags.string({
|
|
289
|
-
summary: messages.getMessage('flags.name.summary'),
|
|
290
|
-
description: messages.getMessage('flags.name.description'),
|
|
291
|
-
char: 'n',
|
|
292
|
-
required: false,
|
|
293
|
-
}),
|
|
294
|
-
target: sf_plugins_core_1.Flags.string({
|
|
295
|
-
summary: messages.getMessage('flags.target.summary'),
|
|
296
|
-
char: 't',
|
|
297
|
-
required: false,
|
|
298
|
-
}),
|
|
299
|
-
id: sf_plugins_core_1.Flags.string({
|
|
300
|
-
summary: messages.getMessage('flags.id.summary'),
|
|
301
|
-
char: 'i',
|
|
302
|
-
required: true,
|
|
303
|
-
}),
|
|
304
|
-
out: sf_plugins_core_1.Flags.string({
|
|
305
|
-
summary: messages.getMessage('flags.out.summary'),
|
|
306
|
-
// eslint-disable-next-line sf-plugin/dash-o
|
|
307
|
-
char: 'o',
|
|
308
|
-
required: true,
|
|
309
|
-
}),
|
|
310
|
-
backend: sf_plugins_core_1.Flags.boolean({
|
|
311
|
-
summary: messages.getMessage('flags.backend.summary'),
|
|
312
|
-
char: 'b',
|
|
313
|
-
}),
|
|
314
|
-
'auth-env-var': sf_plugins_core_1.Flags.string({
|
|
315
|
-
summary: messages.getMessage('flags.auth-env-var.summary'),
|
|
316
|
-
char: 'a',
|
|
317
|
-
}),
|
|
318
|
-
endpoint: sf_plugins_core_1.Flags.url({
|
|
319
|
-
summary: messages.getMessage('flags.endpoint.summary'),
|
|
320
|
-
char: 'e',
|
|
321
|
-
}),
|
|
322
|
-
stage: sf_plugins_core_1.Flags.string({
|
|
323
|
-
summary: messages.getMessage('flags.stage.summary'),
|
|
324
|
-
char: 's',
|
|
325
|
-
required: true,
|
|
326
|
-
}),
|
|
327
|
-
unzip: sf_plugins_core_1.Flags.boolean({
|
|
328
|
-
summary: messages.getMessage('flags.unzip.summary'),
|
|
329
|
-
char: 'u',
|
|
330
|
-
}),
|
|
331
|
-
logs: sf_plugins_core_1.Flags.boolean({
|
|
332
|
-
summary: messages.getMessage('flags.logs.summary'),
|
|
333
|
-
char: 'l',
|
|
334
|
-
}),
|
|
335
|
-
partial: sf_plugins_core_1.Flags.boolean({
|
|
336
|
-
summary: messages.getMessage('flags.partial.summary'),
|
|
337
|
-
char: 'p',
|
|
338
|
-
}),
|
|
339
|
-
session: sf_plugins_core_1.Flags.string({
|
|
340
|
-
summary: messages.getMessage('flags.session.summary'),
|
|
341
|
-
}),
|
|
342
|
-
instance: sf_plugins_core_1.Flags.string({
|
|
343
|
-
summary: messages.getMessage('flags.instance.summary'),
|
|
344
|
-
}),
|
|
345
|
-
cut: sf_plugins_core_1.Flags.boolean({
|
|
346
|
-
summary: messages.getMessage('flags.cut.summary'),
|
|
347
|
-
char: 'c',
|
|
348
|
-
}),
|
|
349
|
-
shortpaths: sf_plugins_core_1.Flags.boolean({
|
|
350
|
-
summary: messages.getMessage('flags.shortpaths.summary'),
|
|
351
|
-
}),
|
|
352
|
-
};
|
|
353
|
-
ButlerPbExport.isLogsActive = false;
|
|
354
344
|
exports.default = ButlerPbExport;
|
|
355
345
|
//# sourceMappingURL=export.js.map
|