UNPKG

6.62 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) 2020, 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 */
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.scratchOrgCreate = exports.DEFAULT_STREAM_TIMEOUT_MINUTES = void 0;
10// third
11const kit_1 = require("@salesforce/kit");
12const ts_types_1 = require("@salesforce/ts-types");
13// Local
14const org_1 = require("./org");
15const logger_1 = require("./logger");
16const messages_1 = require("./messages");
17const sfdxError_1 = require("./sfdxError");
18const connection_1 = require("./connection");
19const sfdxProject_1 = require("./sfdxProject");
20const configAggregator_1 = require("./config/configAggregator");
21const scratchOrgInfoApi_1 = require("./scratchOrgInfoApi");
22const scratchOrgSettingsGenerator_1 = require("./scratchOrgSettingsGenerator");
23const scratchOrgInfoGenerator_1 = require("./scratchOrgInfoGenerator");
24messages_1.Messages.importMessagesDirectory(__dirname);
25const messages = messages_1.Messages.loadMessages('@salesforce/core', 'scratchOrgCreate');
26exports.DEFAULT_STREAM_TIMEOUT_MINUTES = 6;
27const validateDuration = (durationDays) => {
28 const min = 1;
29 const max = 30;
30 if (Number.isInteger(durationDays)) {
31 if (durationDays < min) {
32 throw new sfdxError_1.SfdxError(`Expected 'durationDays' greater than or equal to ${min} but received ${durationDays}`, 'BoundsError');
33 }
34 if (durationDays > max) {
35 throw new sfdxError_1.SfdxError(`Expected 'durationDays' less than or equal to ${max} but received ${durationDays}`, 'BoundsError');
36 }
37 return;
38 }
39 throw new sfdxError_1.SfdxError("Expected 'durationDays' to be an integer number", 'TypeError');
40};
41const validateRetry = (retry) => {
42 if (Number.isInteger(retry)) {
43 return;
44 }
45 throw new sfdxError_1.SfdxError("Expected 'retry' to be an integer number", 'TypeError');
46};
47const validateWait = (wait) => {
48 const min = 2;
49 if (wait.minutes < min) {
50 throw new sfdxError_1.SfdxError(`Expected 'wait' greater than or equal to ${min} but received ${wait}`, 'BoundsError');
51 }
52};
53const scratchOrgCreate = async (options) => {
54 var _a;
55 const logger = await logger_1.Logger.child('scratchOrgCreate');
56 logger.debug('scratchOrgCreate');
57 const { hubOrg, connectedAppConsumerKey, durationDays = 1, nonamespace, noancestors, wait = kit_1.Duration.minutes(exports.DEFAULT_STREAM_TIMEOUT_MINUTES), retry = 0, apiversion, definitionjson, definitionfile, orgConfig, clientSecret = undefined, } = options;
58 validateDuration(durationDays);
59 validateRetry(retry);
60 validateWait(wait);
61 const { scratchOrgInfoPayload, ignoreAncestorIds, warnings } = await scratchOrgInfoGenerator_1.getScratchOrgInfoPayload({
62 definitionjson,
63 definitionfile,
64 connectedAppConsumerKey,
65 durationDays,
66 nonamespace,
67 noancestors,
68 orgConfig,
69 });
70 const scratchOrgInfo = await scratchOrgInfoGenerator_1.generateScratchOrgInfo({
71 hubOrg,
72 scratchOrgInfoPayload,
73 nonamespace,
74 ignoreAncestorIds,
75 });
76 // gets the scratch org settings (will use in both signup paths AND to deploy the settings)
77 const settingsGenerator = new scratchOrgSettingsGenerator_1.default();
78 await settingsGenerator.extract(scratchOrgInfo);
79 logger.debug(`the scratch org def file has settings: ${settingsGenerator.hasSettings()}`);
80 // creates the scratch org info in the devhub
81 const scratchOrgInfoRequestResult = await scratchOrgInfoApi_1.requestScratchOrgCreation(hubOrg, scratchOrgInfo, settingsGenerator);
82 const scratchOrgInfoId = ts_types_1.ensureString(ts_types_1.getString(scratchOrgInfoRequestResult, 'id'));
83 logger.debug(`scratch org has recordId ${scratchOrgInfoId}`);
84 const scratchOrgInfoResult = await scratchOrgInfoApi_1.pollForScratchOrgInfo(hubOrg, scratchOrgInfoId, wait);
85 const signupTargetLoginUrlConfig = await getSignupTargetLoginUrl();
86 const scratchOrgAuthInfo = await scratchOrgInfoApi_1.authorizeScratchOrg({
87 scratchOrgInfoComplete: scratchOrgInfoResult,
88 hubOrg,
89 clientSecret,
90 signupTargetLoginUrlConfig,
91 retry: retry || 0,
92 });
93 // we'll need this scratch org connection later;
94 const connection = await connection_1.Connection.create({ authInfo: scratchOrgAuthInfo });
95 const scratchOrg = await org_1.Org.create({ connection }); // scartchOrg should come from command
96 const username = scratchOrg.getUsername();
97 logger.debug(`scratch org username ${username}`);
98 const configAggregator = new configAggregator_1.ConfigAggregator();
99 const authInfo = await scratchOrgInfoApi_1.deploySettingsAndResolveUrl(scratchOrgAuthInfo, (_a = apiversion !== null && apiversion !== void 0 ? apiversion : configAggregator.getPropertyValue('apiVersion')) !== null && _a !== void 0 ? _a : (await scratchOrg.retrieveMaxApiVersion()), settingsGenerator, scratchOrg);
100 logger.trace('Settings deployed to org');
101 /** updating the revision num to zero during org:creation if source members are created during org:create.This only happens for some specific scratch org definition file.*/
102 await updateRevisionCounterToZero(scratchOrg);
103 return {
104 username,
105 scratchOrgInfo: scratchOrgInfoResult,
106 authInfo,
107 authFields: authInfo === null || authInfo === void 0 ? void 0 : authInfo.getFields(),
108 warnings,
109 };
110};
111exports.scratchOrgCreate = scratchOrgCreate;
112const getSignupTargetLoginUrl = async () => {
113 try {
114 const project = await sfdxProject_1.SfdxProject.resolve();
115 const projectJson = await project.resolveProjectConfig();
116 return projectJson.signupTargetLoginUrl;
117 }
118 catch {
119 // a project isn't required for org:create
120 }
121};
122const updateRevisionCounterToZero = async (scratchOrg) => {
123 const conn = scratchOrg.getConnection();
124 const queryResult = await conn.tooling.sobject('SourceMember').find({ RevisionCounter: { $gt: 0 } }, ['Id']);
125 try {
126 await conn.tooling
127 .sobject('SourceMember')
128 .update(queryResult.map((record) => ({ Id: record.Id, RevisionCounter: 0 })));
129 }
130 catch (err) {
131 const message = messages.getMessage('SourceStatusResetFailure', [scratchOrg.getOrgId(), scratchOrg.getUsername()]);
132 throw new sfdxError_1.SfdxError(message, 'SourceStatusResetFailure');
133 }
134};
135//# sourceMappingURL=scratchOrgCreate.js.map
\No newline at end of file