1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.scratchOrgCreate = exports.DEFAULT_STREAM_TIMEOUT_MINUTES = void 0;
|
10 |
|
11 | const kit_1 = require("@salesforce/kit");
|
12 | const ts_types_1 = require("@salesforce/ts-types");
|
13 |
|
14 | const org_1 = require("./org");
|
15 | const logger_1 = require("./logger");
|
16 | const messages_1 = require("./messages");
|
17 | const sfdxError_1 = require("./sfdxError");
|
18 | const connection_1 = require("./connection");
|
19 | const sfdxProject_1 = require("./sfdxProject");
|
20 | const configAggregator_1 = require("./config/configAggregator");
|
21 | const scratchOrgInfoApi_1 = require("./scratchOrgInfoApi");
|
22 | const scratchOrgSettingsGenerator_1 = require("./scratchOrgSettingsGenerator");
|
23 | const scratchOrgInfoGenerator_1 = require("./scratchOrgInfoGenerator");
|
24 | messages_1.Messages.importMessagesDirectory(__dirname);
|
25 | const messages = messages_1.Messages.loadMessages('@salesforce/core', 'scratchOrgCreate');
|
26 | exports.DEFAULT_STREAM_TIMEOUT_MINUTES = 6;
|
27 | const 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 | };
|
41 | const 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 | };
|
47 | const 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 | };
|
53 | const 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 |
|
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 |
|
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 |
|
94 | const connection = await connection_1.Connection.create({ authInfo: scratchOrgAuthInfo });
|
95 | const scratchOrg = await org_1.Org.create({ connection });
|
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 |
|
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 | };
|
111 | exports.scratchOrgCreate = scratchOrgCreate;
|
112 | const 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 |
|
120 | }
|
121 | };
|
122 | const 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 |
|
\ | No newline at end of file |