@salesforce/core 3.7.4 → 4.0.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.
- package/CHANGELOG.md +215 -0
- package/lib/config/configStore.js +2 -1
- package/lib/config/envVars.d.ts +20 -5
- package/lib/config/envVars.js +70 -5
- package/lib/crypto/keyChainImpl.js +0 -1
- package/lib/exported.d.ts +4 -2
- package/lib/exported.js +5 -1
- package/lib/lifecycleEvents.js +1 -1
- package/lib/logger.js +0 -1
- package/lib/org/authInfo.js +2 -3
- package/lib/org/connection.d.ts +6 -1
- package/lib/org/connection.js +13 -0
- package/lib/org/org.d.ts +12 -2
- package/lib/org/org.js +39 -36
- package/lib/org/scratchOrgCreate.d.ts +43 -0
- package/lib/org/scratchOrgCreate.js +142 -0
- package/lib/org/scratchOrgErrorCodes.d.ts +4 -0
- package/lib/org/scratchOrgErrorCodes.js +62 -0
- package/lib/org/scratchOrgFeatureDeprecation.d.ts +26 -0
- package/lib/org/scratchOrgFeatureDeprecation.js +110 -0
- package/lib/org/scratchOrgInfoApi.d.ts +94 -0
- package/lib/org/scratchOrgInfoApi.js +350 -0
- package/lib/org/scratchOrgInfoGenerator.d.ts +63 -0
- package/lib/org/scratchOrgInfoGenerator.js +223 -0
- package/lib/org/scratchOrgSettingsGenerator.d.ts +56 -0
- package/lib/org/scratchOrgSettingsGenerator.js +210 -0
- package/lib/org/user.js +4 -3
- package/lib/status/pollingClient.d.ts +2 -2
- package/lib/status/pollingClient.js +8 -2
- package/lib/status/streamingClient.d.ts +1 -1
- package/lib/util/jsonXmlTools.d.ts +14 -0
- package/lib/util/jsonXmlTools.js +41 -0
- package/lib/util/mapKeys.d.ts +14 -0
- package/lib/util/mapKeys.js +48 -0
- package/lib/util/zipWriter.d.ts +14 -0
- package/lib/util/zipWriter.js +68 -0
- package/lib/webOAuthServer.js +1 -1
- package/messages/envVars.md +53 -13
- package/messages/org.md +4 -0
- package/messages/scratchOrgCreate.md +27 -0
- package/messages/scratchOrgErrorCodes.md +99 -0
- package/messages/scratchOrgFeatureDeprecation.md +11 -0
- package/messages/scratchOrgInfoApi.md +11 -0
- package/messages/scratchOrgInfoGenerator.md +19 -0
- package/messages/user.md +4 -0
- package/package.json +5 -1
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2021, salesforce.com, inc.
|
|
4
|
+
* All rights reserved.
|
|
5
|
+
* Licensed under the BSD 3-Clause license.
|
|
6
|
+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.deploySettingsAndResolveUrl = exports.pollForScratchOrgInfo = exports.requestScratchOrgCreation = exports.authorizeScratchOrg = void 0;
|
|
10
|
+
const kit_1 = require("@salesforce/kit");
|
|
11
|
+
const ts_types_1 = require("@salesforce/ts-types");
|
|
12
|
+
const ts_retry_promise_1 = require("ts-retry-promise");
|
|
13
|
+
const logger_1 = require("../logger");
|
|
14
|
+
const mapKeys_1 = require("../util/mapKeys");
|
|
15
|
+
const messages_1 = require("../messages");
|
|
16
|
+
const sfdxError_1 = require("../sfdxError");
|
|
17
|
+
const sfdcUrl_1 = require("../util/sfdcUrl");
|
|
18
|
+
const pollingClient_1 = require("../status/pollingClient");
|
|
19
|
+
const myDomainResolver_1 = require("../status/myDomainResolver");
|
|
20
|
+
const authInfo_1 = require("./authInfo");
|
|
21
|
+
const org_1 = require("./org");
|
|
22
|
+
const scratchOrgErrorCodes_1 = require("./scratchOrgErrorCodes");
|
|
23
|
+
messages_1.Messages.importMessagesDirectory(__dirname);
|
|
24
|
+
const messages = messages_1.Messages.loadMessages('@salesforce/core', 'scratchOrgInfoApi');
|
|
25
|
+
const errorCodes = messages_1.Messages.load('@salesforce/core', 'scratchOrgErrorCodes', ['C-1007']);
|
|
26
|
+
/**
|
|
27
|
+
* Returns the url to be used to authorize into the new scratch org
|
|
28
|
+
*
|
|
29
|
+
* @param scratchOrgInfoComplete The completed ScratchOrgInfo
|
|
30
|
+
* @param hubOrgLoginUrl the hun org login url
|
|
31
|
+
* @param signupTargetLoginUrlConfig the login url
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
const getOrgInstanceAuthority = function (scratchOrgInfoComplete, hubOrgLoginUrl, signupTargetLoginUrlConfig) {
|
|
35
|
+
const createdOrgInstance = scratchOrgInfoComplete.SignupInstance;
|
|
36
|
+
if (createdOrgInstance === 'utf8') {
|
|
37
|
+
return hubOrgLoginUrl;
|
|
38
|
+
}
|
|
39
|
+
let altUrl;
|
|
40
|
+
// For non-Falcon (ie - instance names not ending in -s) sandboxes, use the instance URL
|
|
41
|
+
if (createdOrgInstance && !createdOrgInstance.toLowerCase().endsWith('s')) {
|
|
42
|
+
altUrl = `https://${createdOrgInstance}.salesforce.com`;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
// For Falcon sandboxes, try the LoginURL instead; createdOrgInstance will not yield a valid URL
|
|
46
|
+
altUrl = scratchOrgInfoComplete.LoginUrl;
|
|
47
|
+
}
|
|
48
|
+
return signupTargetLoginUrlConfig !== null && signupTargetLoginUrlConfig !== void 0 ? signupTargetLoginUrlConfig : altUrl;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Returns OAuth2Options object
|
|
52
|
+
*
|
|
53
|
+
* @returns {OAuth2Options, number, number, number} options, retries, timeout, delay
|
|
54
|
+
* @param options
|
|
55
|
+
*/
|
|
56
|
+
const buildOAuth2Options = async (options) => {
|
|
57
|
+
const logger = await logger_1.Logger.child('buildOAuth2Options');
|
|
58
|
+
const isJwtFlow = !!options.hubOrg.getConnection().getAuthInfoFields().privateKey;
|
|
59
|
+
const oauth2Options = {
|
|
60
|
+
loginUrl: getOrgInstanceAuthority(options.scratchOrgInfoComplete, options.hubOrg.getField(org_1.Org.Fields.LOGIN_URL), options.signupTargetLoginUrlConfig),
|
|
61
|
+
};
|
|
62
|
+
logger.debug(`isJwtFlow: ${isJwtFlow}`);
|
|
63
|
+
if (isJwtFlow && !process.env.SFDX_CLIENT_SECRET) {
|
|
64
|
+
oauth2Options.privateKeyFile = options.hubOrg.getConnection().getAuthInfoFields().privateKey;
|
|
65
|
+
const retries = (options === null || options === void 0 ? void 0 : options.retry) || kit_1.env.getNumber('SFDX_JWT_AUTH_RETRY_ATTEMPTS') || 0;
|
|
66
|
+
const timeoutInSeconds = kit_1.env.getNumber('SFDX_JWT_AUTH_RETRY_TIMEOUT') || 300;
|
|
67
|
+
const timeout = kit_1.Duration.seconds(timeoutInSeconds).milliseconds;
|
|
68
|
+
const delay = retries ? timeout / retries : 1000;
|
|
69
|
+
return {
|
|
70
|
+
options: oauth2Options,
|
|
71
|
+
retries,
|
|
72
|
+
timeout,
|
|
73
|
+
delay,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// Web Server OAuth "auth code exchange" flow
|
|
78
|
+
if (process.env.SFDX_CLIENT_SECRET) {
|
|
79
|
+
oauth2Options.clientSecret = process.env.SFDX_CLIENT_SECRET;
|
|
80
|
+
}
|
|
81
|
+
else if (options.clientSecret) {
|
|
82
|
+
oauth2Options.clientSecret = options.clientSecret;
|
|
83
|
+
}
|
|
84
|
+
oauth2Options.redirectUri = options.scratchOrgInfoComplete.ConnectedAppCallbackUrl;
|
|
85
|
+
oauth2Options.authCode = options.scratchOrgInfoComplete.AuthCode;
|
|
86
|
+
return {
|
|
87
|
+
options: oauth2Options,
|
|
88
|
+
retries: 0,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Returns OAuth2Options object
|
|
94
|
+
*
|
|
95
|
+
* hubOrg the environment hub org
|
|
96
|
+
* username The OAuth client secret. May be null for JWT OAuth flow.
|
|
97
|
+
* oauth2Options The completed ScratchOrgInfo which should contain an access token.
|
|
98
|
+
* retries auth retry a
|
|
99
|
+
* timeout the login url
|
|
100
|
+
* delay the login url
|
|
101
|
+
*
|
|
102
|
+
* @returns {OAuth2Options, number, number, number} options, retries, timeout, delay
|
|
103
|
+
*/
|
|
104
|
+
const getAuthInfo = async (options) => {
|
|
105
|
+
const logger = await logger_1.Logger.child('getAuthInfo');
|
|
106
|
+
const retryAuthorize = (0, ts_retry_promise_1.retryDecorator)(async (opts) => authInfo_1.AuthInfo.create(opts), {
|
|
107
|
+
timeout: options.timeout,
|
|
108
|
+
delay: options.delay,
|
|
109
|
+
retries: options.retries,
|
|
110
|
+
});
|
|
111
|
+
if (options.retries) {
|
|
112
|
+
try {
|
|
113
|
+
return await retryAuthorize({
|
|
114
|
+
username: options.username,
|
|
115
|
+
parentUsername: options.hubOrg.getUsername(),
|
|
116
|
+
oauth2Options: options.oauth2Options,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
const error = err;
|
|
121
|
+
logger.error(error);
|
|
122
|
+
throw error.lastError || error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
return authInfo_1.AuthInfo.create({
|
|
127
|
+
username: options.username,
|
|
128
|
+
parentUsername: options.hubOrg.getUsername(),
|
|
129
|
+
oauth2Options: options.oauth2Options,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* after we successfully signup an org we need to trade the auth token for access and refresh token.
|
|
135
|
+
*
|
|
136
|
+
* scratchOrgInfoComplete - The completed ScratchOrgInfo which should contain an access token.
|
|
137
|
+
* hubOrg - the environment hub org
|
|
138
|
+
* clientSecret - The OAuth client secret. May be null for JWT OAuth flow.
|
|
139
|
+
* signupTargetLoginUrlConfig - Login url
|
|
140
|
+
* retry - auth retry attempts
|
|
141
|
+
*
|
|
142
|
+
* @returns {Promise<AuthInfo>}
|
|
143
|
+
*/
|
|
144
|
+
const authorizeScratchOrg = async (options) => {
|
|
145
|
+
var _a;
|
|
146
|
+
const { scratchOrgInfoComplete, hubOrg, clientSecret, signupTargetLoginUrlConfig, retry: maxRetries } = options;
|
|
147
|
+
const logger = await logger_1.Logger.child('authorizeScratchOrg');
|
|
148
|
+
logger.debug(`scratchOrgInfoComplete: ${JSON.stringify(scratchOrgInfoComplete, null, 4)}`);
|
|
149
|
+
// if we didn't have it marked as a devhub but just successfully used it as one, this will update the authFile, fix cache, etc
|
|
150
|
+
if (!hubOrg.isDevHubOrg()) {
|
|
151
|
+
await hubOrg.determineIfDevHubOrg(true);
|
|
152
|
+
}
|
|
153
|
+
const oAuth2Options = await buildOAuth2Options({
|
|
154
|
+
hubOrg,
|
|
155
|
+
clientSecret,
|
|
156
|
+
scratchOrgInfoComplete,
|
|
157
|
+
retry: maxRetries,
|
|
158
|
+
signupTargetLoginUrlConfig,
|
|
159
|
+
});
|
|
160
|
+
const authInfo = await getAuthInfo({
|
|
161
|
+
hubOrg,
|
|
162
|
+
username: scratchOrgInfoComplete.SignupUsername,
|
|
163
|
+
oauth2Options: oAuth2Options.options,
|
|
164
|
+
retries: oAuth2Options.retries,
|
|
165
|
+
timeout: oAuth2Options.timeout,
|
|
166
|
+
delay: oAuth2Options.delay,
|
|
167
|
+
});
|
|
168
|
+
await authInfo.save({
|
|
169
|
+
devHubUsername: hubOrg.getUsername(),
|
|
170
|
+
created: new Date((_a = scratchOrgInfoComplete.CreatedDate) !== null && _a !== void 0 ? _a : new Date()).valueOf().toString(),
|
|
171
|
+
expirationDate: scratchOrgInfoComplete.ExpirationDate,
|
|
172
|
+
clientId: scratchOrgInfoComplete.ConnectedAppConsumerKey,
|
|
173
|
+
createdOrgInstance: scratchOrgInfoComplete.SignupInstance,
|
|
174
|
+
isDevHub: false,
|
|
175
|
+
snapshot: scratchOrgInfoComplete.Snapshot,
|
|
176
|
+
});
|
|
177
|
+
return authInfo;
|
|
178
|
+
};
|
|
179
|
+
exports.authorizeScratchOrg = authorizeScratchOrg;
|
|
180
|
+
const checkOrgDoesntExist = async (scratchOrgInfo) => {
|
|
181
|
+
const usernameKey = Object.keys(scratchOrgInfo).find((key) => key.toUpperCase() === 'USERNAME');
|
|
182
|
+
if (!usernameKey) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const username = (0, ts_types_1.getString)(scratchOrgInfo, usernameKey);
|
|
186
|
+
if (username && username.length > 0) {
|
|
187
|
+
try {
|
|
188
|
+
await authInfo_1.AuthInfo.create({ username: username.toLowerCase() });
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
const sfdxError = sfdxError_1.SfdxError.wrap(error);
|
|
192
|
+
// if an AuthInfo couldn't be created that means no AuthFile exists.
|
|
193
|
+
if (sfdxError.name === 'NamedOrgNotFound') {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
// Something unexpected
|
|
197
|
+
throw sfdxError;
|
|
198
|
+
}
|
|
199
|
+
// An org file already exists
|
|
200
|
+
throw errorCodes.createError('C-1007');
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* This extracts orgPrefs/settings from the user input and performs a basic scratchOrgInfo request.
|
|
205
|
+
*
|
|
206
|
+
* @param hubOrg - the environment hub org
|
|
207
|
+
* @param scratchOrgRequest - An object containing the fields of the ScratchOrgInfo
|
|
208
|
+
* @param settings - An object containing org settings
|
|
209
|
+
* @returns {Promise<SaveResult>}
|
|
210
|
+
*/
|
|
211
|
+
const requestScratchOrgCreation = async (hubOrg, scratchOrgRequest, settings) => {
|
|
212
|
+
// If these were present, they were already used to initialize the scratchOrgSettingsGenerator.
|
|
213
|
+
// They shouldn't be submitted as part of the scratchOrgInfo.
|
|
214
|
+
delete scratchOrgRequest.settings;
|
|
215
|
+
delete scratchOrgRequest.objectSettings;
|
|
216
|
+
// We do not allow you to specify the old and the new way of doing post create settings
|
|
217
|
+
if (scratchOrgRequest.orgPreferences && settings.hasSettings()) {
|
|
218
|
+
// This is not allowed
|
|
219
|
+
throw new sfdxError_1.SfdxError('SignupDuplicateSettingsSpecifiedError');
|
|
220
|
+
}
|
|
221
|
+
// deprecated old style orgPreferences
|
|
222
|
+
if (scratchOrgRequest.orgPreferences) {
|
|
223
|
+
throw new sfdxError_1.SfdxError(messages.getMessage('DeprecatedPrefFormat'));
|
|
224
|
+
}
|
|
225
|
+
const scratchOrgInfo = (0, mapKeys_1.default)(scratchOrgRequest, kit_1.upperFirst, true);
|
|
226
|
+
await checkOrgDoesntExist(scratchOrgInfo); // throw if it does exist.
|
|
227
|
+
try {
|
|
228
|
+
return await hubOrg.getConnection().sobject('ScratchOrgInfo').create(scratchOrgInfo);
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
// this is a jsforce error which contains the property "fields" which regular error don't
|
|
232
|
+
const jsForceError = error;
|
|
233
|
+
if (jsForceError.errorCode === 'REQUIRED_FIELD_MISSING') {
|
|
234
|
+
throw new sfdxError_1.SfdxError(messages.getMessage('SignupFieldsMissingError', [jsForceError.fields.toString()]));
|
|
235
|
+
}
|
|
236
|
+
throw sfdxError_1.SfdxError.wrap(jsForceError);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
exports.requestScratchOrgCreation = requestScratchOrgCreation;
|
|
240
|
+
/**
|
|
241
|
+
* This retrieves the ScratchOrgInfo, polling until the status is Active or Error
|
|
242
|
+
*
|
|
243
|
+
* @param hubOrg
|
|
244
|
+
* @param scratchOrgInfoId - the id of the scratchOrgInfo that we are retrieving
|
|
245
|
+
* @param timeout - A Duration object
|
|
246
|
+
* @returns {Promise<ScratchOrgInfo>}
|
|
247
|
+
*/
|
|
248
|
+
const pollForScratchOrgInfo = async (hubOrg, scratchOrgInfoId,
|
|
249
|
+
// org:create specifies a default timeout of 6. This longer default is for other consumers
|
|
250
|
+
timeout = kit_1.Duration.minutes(15)) => {
|
|
251
|
+
const logger = await logger_1.Logger.child('scratchOrgInfoApi-pollForScratchOrgInfo');
|
|
252
|
+
logger.debug(`PollingTimeout in minutes: ${timeout.minutes}`);
|
|
253
|
+
const pollingOptions = {
|
|
254
|
+
async poll() {
|
|
255
|
+
try {
|
|
256
|
+
const resultInProgress = await hubOrg.getConnection().sobject('ScratchOrgInfo').retrieve(scratchOrgInfoId);
|
|
257
|
+
logger.debug(`polling client result: ${JSON.stringify(resultInProgress, null, 4)}`);
|
|
258
|
+
// Once it's "done" we can return it
|
|
259
|
+
if (resultInProgress.Status === 'Active' || resultInProgress.Status === 'Error') {
|
|
260
|
+
return {
|
|
261
|
+
completed: true,
|
|
262
|
+
payload: resultInProgress,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
logger.debug(`Scratch org status is ${resultInProgress.Status}`);
|
|
266
|
+
return {
|
|
267
|
+
completed: false,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
logger.debug(`An error occurred trying to retrieve scratchOrgInfo for ${scratchOrgInfoId}`);
|
|
272
|
+
logger.debug(`Error: ${error.message}`);
|
|
273
|
+
logger.debug('Re-trying deploy check again....');
|
|
274
|
+
return {
|
|
275
|
+
completed: false,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
timeout,
|
|
280
|
+
frequency: kit_1.Duration.seconds(1),
|
|
281
|
+
timeoutErrorName: 'ScratchOrgInfoTimeoutError',
|
|
282
|
+
};
|
|
283
|
+
const client = await pollingClient_1.PollingClient.create(pollingOptions);
|
|
284
|
+
try {
|
|
285
|
+
const resultInProgress = await client.subscribe();
|
|
286
|
+
return (0, scratchOrgErrorCodes_1.checkScratchOrgInfoForErrors)(resultInProgress, hubOrg.getUsername(), logger);
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
const err = error;
|
|
290
|
+
if (err.message) {
|
|
291
|
+
throw sfdxError_1.SfdxError.wrap(err);
|
|
292
|
+
}
|
|
293
|
+
throw new sfdxError_1.SfdxError(`The scratch org did not complete within ${timeout.minutes} minutes`, 'orgCreationTimeout', [
|
|
294
|
+
'Try your force:org:create command again with a longer --wait value',
|
|
295
|
+
]);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
exports.pollForScratchOrgInfo = pollForScratchOrgInfo;
|
|
299
|
+
/**
|
|
300
|
+
* This authenticates into the newly created org and sets org preferences
|
|
301
|
+
*
|
|
302
|
+
* @param scratchOrgAuthInfo - an object containing the AuthInfo of the ScratchOrg
|
|
303
|
+
* @param apiVersion - the target api version
|
|
304
|
+
* @param orgSettings - The ScratchOrg settings
|
|
305
|
+
* @param scratchOrg - The scratchOrg Org info
|
|
306
|
+
* @returns {Promise<Optional<AuthInfo>>}
|
|
307
|
+
*/
|
|
308
|
+
const deploySettingsAndResolveUrl = async (scratchOrgAuthInfo, apiVersion, orgSettings, scratchOrg) => {
|
|
309
|
+
const logger = await logger_1.Logger.child('scratchOrgInfoApi-deploySettingsAndResolveUrl');
|
|
310
|
+
if (orgSettings.hasSettings()) {
|
|
311
|
+
// deploy the settings to the newly created scratch org
|
|
312
|
+
logger.debug(`deploying scratch org settings with apiVersion ${apiVersion}`);
|
|
313
|
+
try {
|
|
314
|
+
await orgSettings.createDeploy();
|
|
315
|
+
await orgSettings.deploySettingsViaFolder(scratchOrg, apiVersion);
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
throw sfdxError_1.SfdxError.wrap(error);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const { instanceUrl } = scratchOrgAuthInfo.getFields();
|
|
322
|
+
if (instanceUrl) {
|
|
323
|
+
logger.debug(`processScratchOrgInfoResult - resultData.instanceUrl: ${instanceUrl}`);
|
|
324
|
+
const options = {
|
|
325
|
+
timeout: kit_1.Duration.minutes(3),
|
|
326
|
+
frequency: kit_1.Duration.seconds(10),
|
|
327
|
+
url: new sfdcUrl_1.SfdcUrl(instanceUrl),
|
|
328
|
+
};
|
|
329
|
+
try {
|
|
330
|
+
const resolver = await myDomainResolver_1.MyDomainResolver.create(options);
|
|
331
|
+
await resolver.resolve();
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
const sfdxError = sfdxError_1.SfdxError.wrap(error);
|
|
335
|
+
logger.debug('processScratchOrgInfoResult - err: %s', error);
|
|
336
|
+
if (sfdxError.name === 'MyDomainResolverTimeoutError') {
|
|
337
|
+
sfdxError.setData({
|
|
338
|
+
orgId: scratchOrgAuthInfo.getFields().orgId,
|
|
339
|
+
username: scratchOrgAuthInfo.getFields().username,
|
|
340
|
+
instanceUrl,
|
|
341
|
+
});
|
|
342
|
+
logger.debug('processScratchOrgInfoResult - err data: %s', sfdxError.data);
|
|
343
|
+
}
|
|
344
|
+
throw sfdxError;
|
|
345
|
+
}
|
|
346
|
+
return scratchOrgAuthInfo;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
exports.deploySettingsAndResolveUrl = deploySettingsAndResolveUrl;
|
|
350
|
+
//# sourceMappingURL=scratchOrgInfoApi.js.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { SfdxProjectJson } from '../sfdxProject';
|
|
2
|
+
import { Org } from './org';
|
|
3
|
+
import { ScratchOrgInfo } from './scratchOrgInfoApi';
|
|
4
|
+
declare type PartialScratchOrgInfo = Pick<ScratchOrgInfo, 'ConnectedAppConsumerKey' | 'AuthCode' | 'Snapshot' | 'Status' | 'LoginUrl' | 'SignupEmail' | 'SignupUsername' | 'SignupInstance' | 'Username'>;
|
|
5
|
+
export interface ScratchOrgInfoPayload extends PartialScratchOrgInfo {
|
|
6
|
+
orgName: string;
|
|
7
|
+
package2AncestorIds: string;
|
|
8
|
+
features: string | string[];
|
|
9
|
+
connectedAppConsumerKey: string;
|
|
10
|
+
namespace: string;
|
|
11
|
+
connectedAppCallbackUrl: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Generates the package2AncestorIds scratch org property
|
|
15
|
+
*
|
|
16
|
+
* @param scratchOrgInfo - the scratchOrgInfo passed in by the user
|
|
17
|
+
* @param projectJson - sfdxProjectJson
|
|
18
|
+
* @param hubOrg - the hub org, in case we need to do queries
|
|
19
|
+
*/
|
|
20
|
+
export declare const getAncestorIds: (scratchOrgInfo: ScratchOrgInfoPayload, projectJson: SfdxProjectJson, hubOrg: Org) => Promise<string>;
|
|
21
|
+
/**
|
|
22
|
+
* Takes in a scratchOrgInfo and fills in the missing fields
|
|
23
|
+
*
|
|
24
|
+
* @param hubOrg the environment hub org
|
|
25
|
+
* @param scratchOrgInfoPayload - the scratchOrgInfo passed in by the user
|
|
26
|
+
* @param nonamespace create the scratch org with no namespace
|
|
27
|
+
* @param ignoreAncestorIds true if the sfdx-project.json ancestorId keys should be ignored
|
|
28
|
+
*/
|
|
29
|
+
export declare const generateScratchOrgInfo: ({ hubOrg, scratchOrgInfoPayload, nonamespace, ignoreAncestorIds, }: {
|
|
30
|
+
hubOrg: Org;
|
|
31
|
+
scratchOrgInfoPayload: ScratchOrgInfoPayload;
|
|
32
|
+
nonamespace?: boolean | undefined;
|
|
33
|
+
ignoreAncestorIds?: boolean | undefined;
|
|
34
|
+
}) => Promise<ScratchOrgInfoPayload>;
|
|
35
|
+
/**
|
|
36
|
+
* Returns a valid signup json
|
|
37
|
+
*
|
|
38
|
+
* definitionjson org definition in JSON format
|
|
39
|
+
* definitionfile path to an org definition file
|
|
40
|
+
* connectedAppConsumerKey The connected app consumer key. May be null for JWT OAuth flow.
|
|
41
|
+
* durationdays duration of the scratch org (in days) (default:1, min:1, max:30)
|
|
42
|
+
* nonamespace create the scratch org with no namespace
|
|
43
|
+
* noancestors do not include second-generation package ancestors in the scratch org
|
|
44
|
+
* orgConfig overrides definitionjson
|
|
45
|
+
*
|
|
46
|
+
* @returns scratchOrgInfoPayload: ScratchOrgInfoPayload;
|
|
47
|
+
ignoreAncestorIds: boolean;
|
|
48
|
+
warnings: string[];
|
|
49
|
+
*/
|
|
50
|
+
export declare const getScratchOrgInfoPayload: (options: {
|
|
51
|
+
durationDays: number;
|
|
52
|
+
definitionjson?: string;
|
|
53
|
+
definitionfile?: string;
|
|
54
|
+
connectedAppConsumerKey?: string;
|
|
55
|
+
nonamespace?: boolean;
|
|
56
|
+
noancestors?: boolean;
|
|
57
|
+
orgConfig?: Record<string, unknown>;
|
|
58
|
+
}) => Promise<{
|
|
59
|
+
scratchOrgInfoPayload: ScratchOrgInfoPayload;
|
|
60
|
+
ignoreAncestorIds: boolean;
|
|
61
|
+
warnings: string[];
|
|
62
|
+
}>;
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getScratchOrgInfoPayload = exports.generateScratchOrgInfo = exports.getAncestorIds = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (c) 2021, salesforce.com, inc.
|
|
6
|
+
* All rights reserved.
|
|
7
|
+
* Licensed under the BSD 3-Clause license.
|
|
8
|
+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
9
|
+
*/
|
|
10
|
+
const fs_1 = require("fs");
|
|
11
|
+
const kit_1 = require("@salesforce/kit");
|
|
12
|
+
const ts_types_1 = require("@salesforce/ts-types");
|
|
13
|
+
const sfdc_1 = require("../util/sfdc");
|
|
14
|
+
const sfdxProject_1 = require("../sfdxProject");
|
|
15
|
+
const webOAuthServer_1 = require("../webOAuthServer");
|
|
16
|
+
const messages_1 = require("../messages");
|
|
17
|
+
const sfdxError_1 = require("../sfdxError");
|
|
18
|
+
const scratchOrgFeatureDeprecation_1 = require("./scratchOrgFeatureDeprecation");
|
|
19
|
+
const defaultConnectedAppInfo = {
|
|
20
|
+
clientId: 'PlatformCLI',
|
|
21
|
+
legacyClientId: 'SalesforceDevelopmentExperience',
|
|
22
|
+
legacyClientSecret: '1384510088588713504',
|
|
23
|
+
};
|
|
24
|
+
messages_1.Messages.importMessagesDirectory(__dirname);
|
|
25
|
+
const messages = messages_1.Messages.loadMessages('@salesforce/core', 'scratchOrgInfoGenerator');
|
|
26
|
+
const SNAPSHOT_UNSUPPORTED_OPTIONS = [
|
|
27
|
+
'features',
|
|
28
|
+
'orgPreferences',
|
|
29
|
+
'edition',
|
|
30
|
+
'sourceOrg',
|
|
31
|
+
'settingsPath',
|
|
32
|
+
'releaseVersion',
|
|
33
|
+
'language',
|
|
34
|
+
];
|
|
35
|
+
// A validator function to ensure any options parameters entered by the user adhere
|
|
36
|
+
// to a allowlist of valid option settings. Because org:create allows options to be
|
|
37
|
+
// input either key=value pairs or within the definition file, this validator is
|
|
38
|
+
// executed within the ctor and also after parsing/normalization of the definition file.
|
|
39
|
+
const optionsValidator = (key, scratchOrgInfoPayload) => {
|
|
40
|
+
if (key.toLowerCase() === 'durationdays') {
|
|
41
|
+
throw new sfdxError_1.SfdxError('unrecognizedScratchOrgOption', 'durationDays');
|
|
42
|
+
}
|
|
43
|
+
if (key.toLowerCase() === 'snapshot') {
|
|
44
|
+
const foundInvalidFields = SNAPSHOT_UNSUPPORTED_OPTIONS.filter((invalidField) => invalidField in scratchOrgInfoPayload);
|
|
45
|
+
if (foundInvalidFields.length > 0) {
|
|
46
|
+
throw new sfdxError_1.SfdxError(messages.getMessage('unsupportedSnapshotOrgCreateOptions', [foundInvalidFields.join(', ')]), 'orgSnapshot');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Generates the package2AncestorIds scratch org property
|
|
52
|
+
*
|
|
53
|
+
* @param scratchOrgInfo - the scratchOrgInfo passed in by the user
|
|
54
|
+
* @param projectJson - sfdxProjectJson
|
|
55
|
+
* @param hubOrg - the hub org, in case we need to do queries
|
|
56
|
+
*/
|
|
57
|
+
const getAncestorIds = async (scratchOrgInfo, projectJson, hubOrg) => {
|
|
58
|
+
if (Reflect.has(scratchOrgInfo, 'package2AncestorIds')) {
|
|
59
|
+
throw new sfdxError_1.SfdxError(messages.getMessage('Package2AncestorsIdsKeyNotSupportedError'), 'DeprecationError');
|
|
60
|
+
}
|
|
61
|
+
const packagesWithAncestors = (await projectJson.getPackageDirectories())
|
|
62
|
+
// check that the package has any ancestor types (id or version)
|
|
63
|
+
.filter((packageDir) => packageDir.ancestorId || packageDir.ancestorVersion);
|
|
64
|
+
if (packagesWithAncestors.length === 0) {
|
|
65
|
+
return '';
|
|
66
|
+
}
|
|
67
|
+
const ancestorIds = await Promise.all(packagesWithAncestors.map(async (packageDir) => {
|
|
68
|
+
var _a, _b, _c;
|
|
69
|
+
// ancestorID can be 05i, or 04t, alias; OR "ancestorVersion": "4.6.0.1"
|
|
70
|
+
// according to docs, 05i is not ok: https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev2gp_config_file.htm
|
|
71
|
+
if (packageDir.ancestorVersion) {
|
|
72
|
+
if (!/^[0-9]+.[0-9]+.[0-9]+(.[0-9]+)?$/.test(packageDir.ancestorVersion)) {
|
|
73
|
+
throw messages.createError('InvalidAncestorVersionFormatError', [packageDir.ancestorVersion]);
|
|
74
|
+
}
|
|
75
|
+
// package can be an ID, but not according to docs
|
|
76
|
+
const packageAliases = projectJson.get('packageAliases');
|
|
77
|
+
const packageId = (_a = packageAliases[(0, ts_types_1.ensureString)(packageDir.package)]) !== null && _a !== void 0 ? _a : packageDir.package;
|
|
78
|
+
const [major, minor, patch] = packageDir.ancestorVersion.split('.');
|
|
79
|
+
let releasedAncestor;
|
|
80
|
+
try {
|
|
81
|
+
releasedAncestor = await hubOrg
|
|
82
|
+
.getConnection()
|
|
83
|
+
.singleRecordQuery(`SELECT Id, IsReleased FROM Package2Version WHERE Package2Id = '${packageId}' AND MajorVersion = ${major} AND MinorVersion = ${minor} AND PatchVersion = ${patch} and IsReleased = true`, { tooling: true });
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
throw new sfdxError_1.SfdxError(messages.getMessage('NoMatchingAncestorError', [packageDir.ancestorVersion, packageDir.package]), 'NoMatchingAncestorError', [messages.getMessage('AncestorNotReleasedError', [packageDir.ancestorVersion])]);
|
|
87
|
+
}
|
|
88
|
+
if (packageDir.ancestorId && packageDir.ancestorId !== releasedAncestor.Id) {
|
|
89
|
+
throw messages.createError('AncestorIdVersionMismatchError', [
|
|
90
|
+
packageDir.ancestorVersion,
|
|
91
|
+
packageDir.ancestorId,
|
|
92
|
+
]);
|
|
93
|
+
}
|
|
94
|
+
return releasedAncestor.Id;
|
|
95
|
+
}
|
|
96
|
+
if ((_b = packageDir === null || packageDir === void 0 ? void 0 : packageDir.ancestorId) === null || _b === void 0 ? void 0 : _b.startsWith('05i')) {
|
|
97
|
+
// if it's already a 05i return it, otherwise query for it
|
|
98
|
+
return packageDir.ancestorId;
|
|
99
|
+
}
|
|
100
|
+
if ((_c = packageDir === null || packageDir === void 0 ? void 0 : packageDir.ancestorId) === null || _c === void 0 ? void 0 : _c.startsWith('04t')) {
|
|
101
|
+
// query for the Id
|
|
102
|
+
return (await hubOrg
|
|
103
|
+
.getConnection()
|
|
104
|
+
.singleRecordQuery(`SELECT Id FROM Package2Version WHERE SubscriberPackageVersionId = '${packageDir.ancestorId}'`, { tooling: true })).Id;
|
|
105
|
+
}
|
|
106
|
+
// ancestorID can be an alias get it from projectJson
|
|
107
|
+
const packageAliases = projectJson.get('packageAliases');
|
|
108
|
+
if (packageDir.ancestorId && (packageAliases === null || packageAliases === void 0 ? void 0 : packageAliases[packageDir.ancestorId])) {
|
|
109
|
+
return packageAliases[packageDir.ancestorId];
|
|
110
|
+
}
|
|
111
|
+
throw new sfdxError_1.SfdxError(`Invalid ancestorId ${packageDir.ancestorId}`, 'InvalidAncestorId');
|
|
112
|
+
}));
|
|
113
|
+
return Array.from(new Set(ancestorIds)).join(';');
|
|
114
|
+
};
|
|
115
|
+
exports.getAncestorIds = getAncestorIds;
|
|
116
|
+
/**
|
|
117
|
+
* Takes in a scratchOrgInfo and fills in the missing fields
|
|
118
|
+
*
|
|
119
|
+
* @param hubOrg the environment hub org
|
|
120
|
+
* @param scratchOrgInfoPayload - the scratchOrgInfo passed in by the user
|
|
121
|
+
* @param nonamespace create the scratch org with no namespace
|
|
122
|
+
* @param ignoreAncestorIds true if the sfdx-project.json ancestorId keys should be ignored
|
|
123
|
+
*/
|
|
124
|
+
const generateScratchOrgInfo = async ({ hubOrg, scratchOrgInfoPayload, nonamespace, ignoreAncestorIds, }) => {
|
|
125
|
+
var _a, _b;
|
|
126
|
+
let sfdxProject;
|
|
127
|
+
try {
|
|
128
|
+
sfdxProject = await sfdxProject_1.SfdxProjectJson.create({});
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
// project is not required
|
|
132
|
+
}
|
|
133
|
+
scratchOrgInfoPayload.orgName = (_a = scratchOrgInfoPayload.orgName) !== null && _a !== void 0 ? _a : 'Company';
|
|
134
|
+
scratchOrgInfoPayload.package2AncestorIds =
|
|
135
|
+
!ignoreAncestorIds && (sfdxProject === null || sfdxProject === void 0 ? void 0 : sfdxProject.hasPackages())
|
|
136
|
+
? await (0, exports.getAncestorIds)(scratchOrgInfoPayload, sfdxProject, hubOrg)
|
|
137
|
+
: '';
|
|
138
|
+
// Use the Hub org's client ID value, if one wasn't provided to us, or the default
|
|
139
|
+
if (!scratchOrgInfoPayload.connectedAppConsumerKey) {
|
|
140
|
+
scratchOrgInfoPayload.connectedAppConsumerKey =
|
|
141
|
+
(_b = hubOrg.getConnection().getAuthInfoFields().clientId) !== null && _b !== void 0 ? _b : defaultConnectedAppInfo.clientId;
|
|
142
|
+
}
|
|
143
|
+
if (!nonamespace && (sfdxProject === null || sfdxProject === void 0 ? void 0 : sfdxProject.get('namespace'))) {
|
|
144
|
+
scratchOrgInfoPayload.namespace = sfdxProject.get('namespace');
|
|
145
|
+
}
|
|
146
|
+
// we already have the info, and want to get rid of configApi, so this doesn't use that
|
|
147
|
+
scratchOrgInfoPayload.connectedAppCallbackUrl = `http://localhost:${await webOAuthServer_1.WebOAuthServer.determineOauthPort()}/OauthRedirect`;
|
|
148
|
+
return scratchOrgInfoPayload;
|
|
149
|
+
};
|
|
150
|
+
exports.generateScratchOrgInfo = generateScratchOrgInfo;
|
|
151
|
+
/**
|
|
152
|
+
* Returns a valid signup json
|
|
153
|
+
*
|
|
154
|
+
* definitionjson org definition in JSON format
|
|
155
|
+
* definitionfile path to an org definition file
|
|
156
|
+
* connectedAppConsumerKey The connected app consumer key. May be null for JWT OAuth flow.
|
|
157
|
+
* durationdays duration of the scratch org (in days) (default:1, min:1, max:30)
|
|
158
|
+
* nonamespace create the scratch org with no namespace
|
|
159
|
+
* noancestors do not include second-generation package ancestors in the scratch org
|
|
160
|
+
* orgConfig overrides definitionjson
|
|
161
|
+
*
|
|
162
|
+
* @returns scratchOrgInfoPayload: ScratchOrgInfoPayload;
|
|
163
|
+
ignoreAncestorIds: boolean;
|
|
164
|
+
warnings: string[];
|
|
165
|
+
*/
|
|
166
|
+
const getScratchOrgInfoPayload = async (options) => {
|
|
167
|
+
var _a;
|
|
168
|
+
let warnings = [];
|
|
169
|
+
// orgConfig input overrides definitionjson (-j option; hidden/deprecated)
|
|
170
|
+
const definitionJson = options.definitionjson ? JSON.parse(options.definitionjson) : {};
|
|
171
|
+
const orgConfigInput = { ...definitionJson, ...((_a = options.orgConfig) !== null && _a !== void 0 ? _a : {}) };
|
|
172
|
+
let scratchOrgInfoPayload = orgConfigInput;
|
|
173
|
+
// the -f option
|
|
174
|
+
if (options.definitionfile) {
|
|
175
|
+
try {
|
|
176
|
+
const fileData = await fs_1.promises.readFile(options.definitionfile, 'utf8');
|
|
177
|
+
const defFileContents = (0, kit_1.parseJson)(fileData);
|
|
178
|
+
// definitionjson and orgConfig override file input
|
|
179
|
+
scratchOrgInfoPayload = { ...defFileContents, ...orgConfigInput };
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
const error = err;
|
|
183
|
+
if (error.name === 'JsonParseError') {
|
|
184
|
+
throw new sfdxError_1.SfdxError(`An error occurred parsing ${options.definitionfile}`);
|
|
185
|
+
}
|
|
186
|
+
throw sfdxError_1.SfdxError.wrap(error);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// scratchOrgInfoPayload must be heads down camelcase.
|
|
190
|
+
const upperCaseKey = sfdc_1.sfdc.findUpperCaseKeys(scratchOrgInfoPayload);
|
|
191
|
+
if (upperCaseKey) {
|
|
192
|
+
throw new sfdxError_1.SfdxError('InvalidJsonCasing', upperCaseKey);
|
|
193
|
+
}
|
|
194
|
+
// Now run the fully resolved user input against the validator
|
|
195
|
+
Object.keys(scratchOrgInfoPayload).forEach((key) => {
|
|
196
|
+
optionsValidator(key, scratchOrgInfoPayload);
|
|
197
|
+
});
|
|
198
|
+
if (options.connectedAppConsumerKey) {
|
|
199
|
+
scratchOrgInfoPayload.connectedAppConsumerKey = options.connectedAppConsumerKey;
|
|
200
|
+
}
|
|
201
|
+
scratchOrgInfoPayload.durationDays = options.durationDays;
|
|
202
|
+
// Throw warnings for deprecated scratch org features.
|
|
203
|
+
const scratchOrgFeatureDeprecation = new scratchOrgFeatureDeprecation_1.ScratchOrgFeatureDeprecation();
|
|
204
|
+
// convert various supported array and string formats to a semi-colon-delimited string
|
|
205
|
+
if (scratchOrgInfoPayload.features) {
|
|
206
|
+
if (typeof scratchOrgInfoPayload.features === 'string') {
|
|
207
|
+
scratchOrgInfoPayload.features = scratchOrgInfoPayload.features.split(/[;,]/);
|
|
208
|
+
}
|
|
209
|
+
warnings = scratchOrgFeatureDeprecation.getFeatureWarnings(scratchOrgInfoPayload.features);
|
|
210
|
+
scratchOrgInfoPayload.features = scratchOrgInfoPayload.features.map((feature) => feature.trim());
|
|
211
|
+
scratchOrgInfoPayload.features = scratchOrgFeatureDeprecation
|
|
212
|
+
.filterDeprecatedFeatures(scratchOrgInfoPayload.features)
|
|
213
|
+
.join(';');
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
scratchOrgInfoPayload,
|
|
217
|
+
// Ignore ancestor ids only when 'nonamespace' or 'noancestors' options are specified
|
|
218
|
+
ignoreAncestorIds: options.nonamespace || options.noancestors || false,
|
|
219
|
+
warnings,
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
exports.getScratchOrgInfoPayload = getScratchOrgInfoPayload;
|
|
223
|
+
//# sourceMappingURL=scratchOrgInfoGenerator.js.map
|