1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.scratchOrgCreate = exports.scratchOrgResume = exports.DEFAULT_STREAM_TIMEOUT_MINUTES = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const kit_1 = require("@salesforce/kit");
|
11 | const ts_types_1 = require("@salesforce/ts-types");
|
12 | const messages_1 = require("../messages");
|
13 | const logger_1 = require("../logger");
|
14 | const configAggregator_1 = require("../config/configAggregator");
|
15 | const sfProject_1 = require("../sfProject");
|
16 | const stateAggregator_1 = require("../stateAggregator");
|
17 | const org_1 = require("./org");
|
18 | const scratchOrgInfoApi_1 = require("./scratchOrgInfoApi");
|
19 | const scratchOrgSettingsGenerator_1 = require("./scratchOrgSettingsGenerator");
|
20 | const scratchOrgInfoGenerator_1 = require("./scratchOrgInfoGenerator");
|
21 | const authInfo_1 = require("./authInfo");
|
22 | const scratchOrgLifecycleEvents_1 = require("./scratchOrgLifecycleEvents");
|
23 | const scratchOrgCache_1 = require("./scratchOrgCache");
|
24 | const scratchOrgErrorCodes_1 = require("./scratchOrgErrorCodes");
|
25 | messages_1.Messages.importMessagesDirectory(__dirname);
|
26 | const messages = messages_1.Messages.load('@salesforce/core', 'scratchOrgCreate', [
|
27 | 'DurationDaysValidationMaxError',
|
28 | 'DurationDaysValidationMinError',
|
29 | 'RetryNotIntError',
|
30 | 'DurationDaysNotIntError',
|
31 | 'CacheMissError',
|
32 | ]);
|
33 | exports.DEFAULT_STREAM_TIMEOUT_MINUTES = 6;
|
34 | const validateDuration = (durationDays) => {
|
35 | const min = 1;
|
36 | const max = 30;
|
37 | if (Number.isInteger(durationDays)) {
|
38 | if (durationDays < min) {
|
39 | throw messages.createError('DurationDaysValidationMinError', [min, durationDays]);
|
40 | }
|
41 | if (durationDays > max) {
|
42 | throw messages.createError('DurationDaysValidationMaxError', [max, durationDays]);
|
43 | }
|
44 | return;
|
45 | }
|
46 | throw messages.createError('DurationDaysNotIntError');
|
47 | };
|
48 | const validateRetry = (retry) => {
|
49 | if (!Number.isInteger(retry)) {
|
50 | throw messages.createError('RetryNotIntError');
|
51 | }
|
52 | };
|
53 | const scratchOrgResume = async (jobId) => {
|
54 | const [logger, cache] = await Promise.all([
|
55 | logger_1.Logger.child('scratchOrgResume'),
|
56 | scratchOrgCache_1.ScratchOrgCache.create(),
|
57 | (0, scratchOrgLifecycleEvents_1.emit)({ stage: 'send request' }),
|
58 | ]);
|
59 | logger.debug(`resuming scratch org creation for jobId: ${jobId}`);
|
60 | if (!cache.has(jobId)) {
|
61 | throw messages.createError('CacheMissError', [jobId]);
|
62 | }
|
63 | const { hubUsername, apiVersion, clientSecret, signupTargetLoginUrlConfig, definitionjson, alias, setDefault, tracksSource, } = cache.get(jobId);
|
64 | const hubOrg = await org_1.Org.create({ aliasOrUsername: hubUsername });
|
65 | const soi = await (0, scratchOrgInfoApi_1.queryScratchOrgInfo)(hubOrg, jobId);
|
66 | await (0, scratchOrgErrorCodes_1.validateScratchOrgInfoForResume)({ jobId, scratchOrgInfo: soi, cache, hubUsername });
|
67 |
|
68 |
|
69 | const username = soi.Username ?? soi.SignupUsername;
|
70 |
|
71 | const stateAggregator = await stateAggregator_1.StateAggregator.getInstance();
|
72 | const scratchOrgAuthInfo = (await stateAggregator.orgs.exists(username))
|
73 | ? await authInfo_1.AuthInfo.create({ username })
|
74 | : await (0, scratchOrgInfoApi_1.authorizeScratchOrg)({
|
75 | scratchOrgInfoComplete: soi,
|
76 | hubOrg,
|
77 | clientSecret,
|
78 | signupTargetLoginUrlConfig,
|
79 | retry: 0,
|
80 | });
|
81 | const scratchOrg = await org_1.Org.create({ aliasOrUsername: username });
|
82 | await (0, scratchOrgLifecycleEvents_1.emit)({ stage: 'deploy settings', scratchOrgInfo: soi });
|
83 | const settingsGenerator = new scratchOrgSettingsGenerator_1.default();
|
84 | settingsGenerator.extract({ ...soi, ...definitionjson });
|
85 | const [authInfo] = await Promise.all([
|
86 | (0, scratchOrgInfoApi_1.resolveUrl)(scratchOrgAuthInfo),
|
87 | (0, scratchOrgInfoApi_1.deploySettings)(scratchOrg, settingsGenerator, apiVersion ??
|
88 | new configAggregator_1.ConfigAggregator().getPropertyValue('apiVersion') ??
|
89 | (await scratchOrg.retrieveMaxApiVersion())),
|
90 | ]);
|
91 | await scratchOrgAuthInfo.handleAliasAndDefaultSettings({
|
92 | alias,
|
93 | setDefault: setDefault ?? false,
|
94 | setDefaultDevHub: false,
|
95 | setTracksSource: tracksSource ?? true,
|
96 | });
|
97 | cache.unset(soi.Id ?? jobId);
|
98 | const authFields = authInfo.getFields();
|
99 | await Promise.all([(0, scratchOrgLifecycleEvents_1.emit)({ stage: 'done', scratchOrgInfo: soi }), cache.write(), (0, scratchOrgLifecycleEvents_1.emitPostOrgCreate)(authFields)]);
|
100 | return {
|
101 | username,
|
102 | scratchOrgInfo: soi,
|
103 | authInfo,
|
104 | authFields,
|
105 | warnings: [],
|
106 | };
|
107 | };
|
108 | exports.scratchOrgResume = scratchOrgResume;
|
109 | const scratchOrgCreate = async (options) => {
|
110 | const logger = await logger_1.Logger.child('scratchOrgCreate');
|
111 | logger.debug('scratchOrgCreate');
|
112 | await (0, scratchOrgLifecycleEvents_1.emit)({ stage: 'prepare request' });
|
113 | 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, alias, setDefault = false, tracksSource = true, } = options;
|
114 | validateDuration(durationDays);
|
115 | validateRetry(retry);
|
116 | const { scratchOrgInfoPayload, ignoreAncestorIds, warnings } = await (0, scratchOrgInfoGenerator_1.getScratchOrgInfoPayload)({
|
117 | definitionjson,
|
118 | definitionfile,
|
119 | connectedAppConsumerKey,
|
120 | durationDays,
|
121 | nonamespace,
|
122 | noancestors,
|
123 | orgConfig,
|
124 | });
|
125 | const scratchOrgInfo = await (0, scratchOrgInfoGenerator_1.generateScratchOrgInfo)({
|
126 | hubOrg,
|
127 | scratchOrgInfoPayload,
|
128 | nonamespace,
|
129 | ignoreAncestorIds,
|
130 | });
|
131 |
|
132 | const settingsGenerator = new scratchOrgSettingsGenerator_1.default();
|
133 | const settings = await settingsGenerator.extract(scratchOrgInfo);
|
134 | logger.debug(`the scratch org def file has settings: ${settingsGenerator.hasSettings()}`);
|
135 | const [scratchOrgInfoRequestResult, signupTargetLoginUrlConfig] = await Promise.all([
|
136 |
|
137 | (0, scratchOrgInfoApi_1.requestScratchOrgCreation)(hubOrg, scratchOrgInfo, settingsGenerator),
|
138 | getSignupTargetLoginUrl(),
|
139 | ]);
|
140 | const scratchOrgInfoId = (0, ts_types_1.ensureString)(scratchOrgInfoRequestResult.id);
|
141 | const cache = await scratchOrgCache_1.ScratchOrgCache.create();
|
142 | cache.set(scratchOrgInfoId, {
|
143 | hubUsername: hubOrg.getUsername(),
|
144 | hubBaseUrl: hubOrg.getField(org_1.Org.Fields.INSTANCE_URL)?.toString(),
|
145 | definitionjson: { ...(definitionjson ? JSON.parse(definitionjson) : {}), ...orgConfig, ...settings },
|
146 | clientSecret,
|
147 | alias,
|
148 | setDefault,
|
149 | tracksSource,
|
150 | });
|
151 | await cache.write();
|
152 | logger.debug(`scratch org has recordId ${scratchOrgInfoId}`);
|
153 |
|
154 | if (wait.minutes === 0) {
|
155 | const soi = await (0, scratchOrgInfoApi_1.queryScratchOrgInfo)(hubOrg, scratchOrgInfoId);
|
156 | return {
|
157 | username: soi.SignupUsername,
|
158 | warnings: [],
|
159 | scratchOrgInfo: soi,
|
160 | };
|
161 | }
|
162 | const soi = await (0, scratchOrgInfoApi_1.pollForScratchOrgInfo)(hubOrg, scratchOrgInfoId, wait);
|
163 | const scratchOrgAuthInfo = await (0, scratchOrgInfoApi_1.authorizeScratchOrg)({
|
164 | scratchOrgInfoComplete: soi,
|
165 | hubOrg,
|
166 | clientSecret,
|
167 | signupTargetLoginUrlConfig,
|
168 | retry: retry || 0,
|
169 | });
|
170 |
|
171 | const scratchOrg = await org_1.Org.create({
|
172 | aliasOrUsername: soi.Username ?? soi.SignupUsername,
|
173 | });
|
174 | const username = scratchOrg.getUsername();
|
175 | logger.debug(`scratch org username ${username}`);
|
176 | await (0, scratchOrgLifecycleEvents_1.emit)({ stage: 'deploy settings', scratchOrgInfo: soi });
|
177 | const [authInfo] = await Promise.all([
|
178 | (0, scratchOrgInfoApi_1.resolveUrl)(scratchOrgAuthInfo),
|
179 | (0, scratchOrgInfoApi_1.deploySettings)(scratchOrg, settingsGenerator, apiversion ??
|
180 | new configAggregator_1.ConfigAggregator().getPropertyValue('org-api-version') ??
|
181 | (await scratchOrg.retrieveMaxApiVersion())),
|
182 | ]);
|
183 | await scratchOrgAuthInfo.handleAliasAndDefaultSettings({
|
184 | ...{
|
185 | alias,
|
186 | setDefault,
|
187 | setDefaultDevHub: false,
|
188 | setTracksSource: tracksSource === false ? false : true,
|
189 | },
|
190 | });
|
191 | cache.unset(scratchOrgInfoId);
|
192 | const authFields = authInfo.getFields();
|
193 | await Promise.all([(0, scratchOrgLifecycleEvents_1.emit)({ stage: 'done', scratchOrgInfo: soi }), cache.write(), (0, scratchOrgLifecycleEvents_1.emitPostOrgCreate)(authFields)]);
|
194 | return {
|
195 | username,
|
196 | scratchOrgInfo: soi,
|
197 | authInfo,
|
198 | authFields: authInfo?.getFields(),
|
199 | warnings,
|
200 | };
|
201 | };
|
202 | exports.scratchOrgCreate = scratchOrgCreate;
|
203 | const getSignupTargetLoginUrl = async () => {
|
204 | try {
|
205 | const project = await sfProject_1.SfProject.resolve();
|
206 | const projectJson = await project.resolveProjectConfig();
|
207 | return projectJson.signupTargetLoginUrl;
|
208 | }
|
209 | catch {
|
210 |
|
211 | }
|
212 | };
|
213 |
|
\ | No newline at end of file |