UNPKG

21.5 kBTypeScriptView Raw
1import { AsyncOptionalCreatable, Duration } from '@salesforce/kit';
2import { AnyJson, JsonMap, Nullable } from '@salesforce/ts-types';
3import { ConfigAggregator } from '../config/configAggregator';
4import { OrgUsersConfig } from '../config/orgUsersConfig';
5import { Connection } from './connection';
6import { AuthFields, AuthInfo } from './authInfo';
7import { ScratchOrgCreateOptions, ScratchOrgCreateResult } from './scratchOrgCreate';
8export type OrganizationInformation = {
9 Name: string;
10 InstanceName: string;
11 IsSandbox: boolean;
12 TrialExpirationDate: string | null;
13 NamespacePrefix: string | null;
14};
15export declare enum OrgTypes {
16 Scratch = "scratch",
17 Sandbox = "sandbox"
18}
19export type StatusEvent = {
20 sandboxProcessObj: SandboxProcessObject;
21 interval: number;
22 remainingWait: number;
23 waitingOnAuth: boolean;
24};
25export type ResultEvent = {
26 sandboxProcessObj: SandboxProcessObject;
27 sandboxRes: SandboxUserAuthResponse;
28};
29export type SandboxUserAuthRequest = {
30 sandboxName: string;
31 clientId: string;
32 callbackUrl: string;
33};
34export declare enum SandboxEvents {
35 EVENT_STATUS = "status",
36 EVENT_ASYNC_RESULT = "asyncResult",
37 EVENT_RESULT = "result",
38 EVENT_AUTH = "auth",
39 EVENT_RESUME = "resume",
40 EVENT_MULTIPLE_SBX_PROCESSES = "multipleMatchingSbxProcesses"
41}
42export type SandboxUserAuthResponse = {
43 authUserName: string;
44 authCode: string;
45 instanceUrl: string;
46 loginUrl: string;
47};
48export declare function sandboxIsResumable(value: string): boolean;
49export type SandboxProcessObject = {
50 Id: string;
51 Status: string;
52 SandboxName: string;
53 SandboxInfoId: string;
54 LicenseType: string;
55 CreatedDate: string;
56 SandboxOrganization?: string;
57 CopyProgress?: number;
58 SourceId?: string;
59 Description?: string;
60 ApexClassId?: string;
61 EndDate?: string;
62};
63export type SandboxRequest = {
64 SandboxName: string;
65 LicenseType?: string;
66 /** Should match a SandboxInfoId, not a SandboxProcessId */
67 SourceId?: string;
68 Description?: string;
69};
70export type ResumeSandboxRequest = {
71 SandboxName?: string;
72 SandboxProcessObjId?: string;
73};
74export type SandboxInfo = {
75 Id: string;
76 IsDeleted: boolean;
77 CreatedDate: string;
78 CreatedById: string;
79 LastModifiedDate: string;
80 LastModifiedById: string;
81 SandboxName: string;
82 LicenseType: 'DEVELOPER' | 'DEVELOPER PRO' | 'PARTIAL' | 'FULL';
83 TemplateId?: string;
84 HistoryDays: -1 | 0 | 10 | 20 | 30 | 60 | 90 | 120 | 150 | 180;
85 CopyChatter: boolean;
86 AutoActivate: boolean;
87 ApexClassId?: string;
88 Description?: string;
89 SourceId?: string;
90 CopyArchivedActivities?: boolean;
91};
92export type ScratchOrgRequest = Omit<ScratchOrgCreateOptions, 'hubOrg'>;
93export type SandboxFields = {
94 sandboxOrgId: string;
95 prodOrgUsername: string;
96 sandboxName?: string;
97 sandboxUsername?: string;
98 sandboxProcessId?: string;
99 sandboxInfoId?: string;
100 timestamp?: string;
101};
102/**
103 * Provides a way to manage a locally authenticated Org.
104 *
105 * **See** {@link AuthInfo}
106 *
107 * **See** {@link Connection}
108 *
109 * **See** {@link Aliases}
110 *
111 * **See** {@link Config}
112 *
113 * ```
114 * // Email username
115 * const org1: Org = await Org.create({ aliasOrUsername: 'foo@example.com' });
116 * // The target-org config property
117 * const org2: Org = await Org.create();
118 * // Full Connection
119 * const org3: Org = await Org.create({
120 * connection: await Connection.create({
121 * authInfo: await AuthInfo.create({ username: 'username' })
122 * })
123 * });
124 * ```
125 *
126 * **See** https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_cli_usernames_orgs.htm
127 */
128export declare class Org extends AsyncOptionalCreatable<Org.Options> {
129 private status;
130 private configAggregator;
131 private logger;
132 private connection;
133 private options;
134 private orgId?;
135 /**
136 * @ignore
137 */
138 constructor(options?: Org.Options);
139 /**
140 * create a sandbox from a production org
141 * 'this' needs to be a production org with sandbox licenses available
142 *
143 * @param sandboxReq SandboxRequest options to create the sandbox with
144 * @param options Wait: The amount of time to wait before timing out, Interval: The time interval between polling
145 */
146 createSandbox(sandboxReq: SandboxRequest, options?: {
147 wait?: Duration;
148 interval?: Duration;
149 async?: boolean;
150 }): Promise<SandboxProcessObject>;
151 /**
152 * Refresh (update) a sandbox from a production org.
153 * 'this' needs to be a production org with sandbox licenses available
154 *
155 * @param sandboxInfo SandboxInfo to update the sandbox with
156 * @param options Wait: The amount of time to wait before timing out, Interval: The time interval between polling
157 */
158 refreshSandbox(sandboxInfo: SandboxInfo, options?: {
159 wait?: Duration;
160 interval?: Duration;
161 async?: boolean;
162 }): Promise<SandboxProcessObject>;
163 /**
164 *
165 * @param sandboxReq SandboxRequest options to create the sandbox with
166 * @param sourceSandboxName the name of the sandbox that your new sandbox will be based on
167 * @param options Wait: The amount of time to wait before timing out, defaults to 0, Interval: The time interval between polling defaults to 30 seconds
168 * @returns {SandboxProcessObject} the newly created sandbox process object
169 */
170 cloneSandbox(sandboxReq: SandboxRequest, sourceSandboxName: string, options: {
171 wait?: Duration;
172 interval?: Duration;
173 }): Promise<SandboxProcessObject>;
174 /**
175 * Resume a sandbox create or refresh from a production org.
176 * `this` needs to be a production org with sandbox licenses available.
177 *
178 * @param resumeSandboxRequest SandboxRequest options to create/refresh the sandbox with
179 * @param options Wait: The amount of time to wait (default: 0 minutes) before timing out,
180 * Interval: The time interval (default: 30 seconds) between polling
181 */
182 resumeSandbox(resumeSandboxRequest: ResumeSandboxRequest, options?: {
183 wait?: Duration;
184 interval?: Duration;
185 async?: boolean;
186 }): Promise<SandboxProcessObject>;
187 /**
188 * Creates a scratchOrg
189 * 'this' needs to be a valid dev-hub
190 *
191 * @param {options} ScratchOrgCreateOptions
192 * @returns {ScratchOrgCreateResult}
193 */
194 scratchOrgCreate(options: ScratchOrgRequest): Promise<ScratchOrgCreateResult>;
195 /**
196 * Reports sandbox org creation status. If the org is ready, authenticates to the org.
197 *
198 * @param {sandboxname} string the sandbox name
199 * @param options Wait: The amount of time to wait before timing out, Interval: The time interval between polling
200 * @returns {SandboxProcessObject} the sandbox process object
201 */
202 sandboxStatus(sandboxname: string, options: {
203 wait?: Duration;
204 interval?: Duration;
205 }): Promise<SandboxProcessObject>;
206 /**
207 * Clean all data files in the org's data path. Usually <workspace>/.sfdx/orgs/<username>.
208 *
209 * @param orgDataPath A relative path other than "orgs/".
210 * @param throwWhenRemoveFails Should the remove org operations throw an error on failure?
211 */
212 cleanLocalOrgData(orgDataPath?: string, throwWhenRemoveFails?: boolean): Promise<void>;
213 /**
214 * @ignore
215 */
216 retrieveOrgUsersConfig(): Promise<OrgUsersConfig>;
217 /**
218 * Cleans up all org related artifacts including users, sandbox config (if a sandbox), source tracking files, and auth file.
219 *
220 * @param throwWhenRemoveFails Determines if the call should throw an error or fail silently.
221 */
222 remove(throwWhenRemoveFails?: boolean): Promise<void>;
223 /**
224 * Check if org is a sandbox org by checking its SandboxOrgConfig.
225 *
226 */
227 isSandbox(): Promise<boolean>;
228 /**
229 * Check that this org is a scratch org by asking the dev hub if it knows about it.
230 *
231 * **Throws** *{@link SfError}{ name: 'NotADevHubError' }* Not a Dev Hub.
232 *
233 * **Throws** *{@link SfError}{ name: 'NoResultsError' }* No results.
234 *
235 * @param devHubUsernameOrAlias The username or alias of the dev hub org.
236 */
237 checkScratchOrg(devHubUsernameOrAlias?: string): Promise<Partial<AuthFields>>;
238 /**
239 * Returns the Org object or null if this org is not affiliated with a Dev Hub (according to the local config).
240 */
241 getDevHubOrg(): Promise<Org | undefined>;
242 /**
243 * Returns `true` if the org is a Dev Hub.
244 *
245 * **Note** This relies on a cached value in the auth file. If that property
246 * is not cached, this method will **always return false even if the org is a
247 * dev hub**. If you need accuracy, use the {@link Org.determineIfDevHubOrg} method.
248 */
249 isDevHubOrg(): boolean;
250 /**
251 * Will delete 'this' instance remotely and any files locally
252 *
253 * @param controllingOrg username or Org that 'this.devhub' or 'this.production' refers to. AKA a DevHub for a scratch org, or a Production Org for a sandbox
254 */
255 deleteFrom(controllingOrg: string | Org): Promise<void>;
256 /**
257 * Will delete 'this' instance remotely and any files locally
258 */
259 delete(): Promise<void>;
260 /**
261 * Returns `true` if the org is a Dev Hub.
262 *
263 * Use a cached value. If the cached value is not set, then check access to the
264 * ScratchOrgInfo object to determine if the org is a dev hub.
265 *
266 * @param forceServerCheck Ignore the cached value and go straight to the server
267 * which will be required if the org flips on the dev hub after the value is already
268 * cached locally.
269 */
270 determineIfDevHubOrg(forceServerCheck?: boolean): Promise<boolean>;
271 /**
272 * Returns `true` if the org is a scratch org.
273 *
274 * **Note** This relies on a cached value in the auth file. If that property
275 * is not cached, this method will **always return false even if the org is a
276 * scratch org**. If you need accuracy, use the {@link Org.determineIfScratch} method.
277 */
278 isScratch(): boolean;
279 /**
280 * Returns `true` if the org uses source tracking.
281 * Side effect: updates files where the property doesn't currently exist
282 */
283 tracksSource(): Promise<boolean>;
284 /**
285 * Set the tracking property on the org's auth file
286 *
287 * @param value true or false (whether the org should use source tracking or not)
288 */
289 setTracksSource(value: boolean): Promise<void>;
290 /**
291 * Returns `true` if the org is a scratch org.
292 *
293 * Use a cached value. If the cached value is not set, then check
294 * `Organization.IsSandbox == true && Organization.TrialExpirationDate != null`
295 * using {@link Org.retrieveOrganizationInformation}.
296 */
297 determineIfScratch(): Promise<boolean>;
298 /**
299 * Returns `true` if the org is a sandbox.
300 *
301 * Use a cached value. If the cached value is not set, then check
302 * `Organization.IsSandbox == true && Organization.TrialExpirationDate == null`
303 * using {@link Org.retrieveOrganizationInformation}.
304 */
305 determineIfSandbox(): Promise<boolean>;
306 /**
307 * Retrieve a handful of fields from the Organization table in Salesforce. If this does not have the
308 * data you need, just use {@link Connection.singleRecordQuery} with `SELECT <needed fields> FROM Organization`.
309 *
310 * @returns org information
311 */
312 retrieveOrganizationInformation(): Promise<OrganizationInformation>;
313 /**
314 * Some organization information is locally cached, such as if the org name or if it is a scratch org.
315 * This method populates/updates the filesystem from information retrieved from the org.
316 */
317 updateLocalInformation(): Promise<Pick<AuthFields, Org.Fields.NAME | Org.Fields.INSTANCE_NAME | Org.Fields.NAMESPACE_PREFIX | Org.Fields.IS_SANDBOX | Org.Fields.IS_SCRATCH | Org.Fields.TRIAL_EXPIRATION_DATE> | undefined>;
318 /**
319 * Refreshes the auth for this org's instance by calling HTTP GET on the baseUrl of the connection object.
320 */
321 refreshAuth(): Promise<void>;
322 /**
323 * Reads and returns the content of all user auth files for this org as an array.
324 */
325 readUserAuthFiles(): Promise<AuthInfo[]>;
326 /**
327 * Adds a username to the user config for this org. For convenience `this` object is returned.
328 *
329 * ```
330 * const org: Org = await Org.create({
331 * connection: await Connection.create({
332 * authInfo: await AuthInfo.create('foo@example.com')
333 * })
334 * });
335 * const userAuth: AuthInfo = await AuthInfo.create({
336 * username: 'bar@example.com'
337 * });
338 * await org.addUsername(userAuth);
339 * ```
340 *
341 * @param {AuthInfo | string} auth The AuthInfo for the username to add.
342 */
343 addUsername(auth: AuthInfo | string): Promise<Org>;
344 /**
345 * Removes a username from the user config for this object. For convenience `this` object is returned.
346 *
347 * **Throws** *{@link SfError}{ name: 'MissingAuthInfoError' }* Auth info is missing.
348 *
349 * @param {AuthInfo | string} auth The AuthInfo containing the username to remove.
350 */
351 removeUsername(auth: AuthInfo | string): Promise<Org>;
352 /**
353 * set the sandbox config related to this given org
354 *
355 * @param orgId {string} orgId of the sandbox
356 * @param config {SandboxFields} config of the sandbox
357 */
358 setSandboxConfig(orgId: string, config: SandboxFields): Promise<Org>;
359 /**
360 * get the sandbox config for the given orgId
361 *
362 * @param orgId {string} orgId of the sandbox
363 */
364 getSandboxConfig(orgId: string): Promise<Nullable<SandboxFields>>;
365 /**
366 * Retrieves the highest api version that is supported by the target server instance. If the apiVersion configured for
367 * Sfdx is greater than the one returned in this call an api version mismatch occurs. In the case of the CLI that
368 * results in a warning.
369 */
370 retrieveMaxApiVersion(): Promise<string>;
371 /**
372 * Returns the admin username used to create the org.
373 */
374 getUsername(): string | undefined;
375 /**
376 * Returns the orgId for this org.
377 */
378 getOrgId(): string;
379 /**
380 * Returns for the config aggregator.
381 */
382 getConfigAggregator(): ConfigAggregator;
383 /**
384 * Returns an org field. Returns undefined if the field is not set or invalid.
385 */
386 getField<T = AnyJson>(key: Org.Fields): T;
387 /**
388 * Returns a map of requested fields.
389 */
390 getFields(keys: Org.Fields[]): JsonMap;
391 /**
392 * Returns the JSForce connection for the org.
393 * side effect: If you pass it an apiVersion, it will set it on the Org
394 * so that future calls to getConnection() will also use that version.
395 *
396 * @param apiVersion The API version to use for the connection.
397 */
398 getConnection(apiVersion?: string): Connection;
399 supportsSourceTracking(): Promise<boolean>;
400 /**
401 * query SandboxProcess via sandbox name
402 *
403 * @param name SandboxName to query for
404 */
405 querySandboxProcessBySandboxName(name: string): Promise<SandboxProcessObject>;
406 /**
407 * query SandboxProcess via SandboxInfoId
408 *
409 * @param id SandboxInfoId to query for
410 */
411 querySandboxProcessBySandboxInfoId(id: string): Promise<SandboxProcessObject>;
412 /**
413 * query SandboxProcess via Id
414 *
415 * @param id SandboxProcessId to query for
416 */
417 querySandboxProcessById(id: string): Promise<SandboxProcessObject>;
418 /**
419 * query SandboxProcess via SandboxOrganization (sandbox Org ID)
420 *
421 * @param sandboxOrgId SandboxOrganization ID to query for
422 */
423 querySandboxProcessByOrgId(sandboxOrgId: string): Promise<SandboxProcessObject>;
424 /**
425 * Initialize async components.
426 */
427 protected init(): Promise<void>;
428 /**
429 * **Throws** *{@link SfError}{ name: 'NotSupportedError' }* Throws an unsupported error.
430 */
431 protected getDefaultOptions(): Org.Options;
432 private getLocalDataDir;
433 /**
434 * Gets the sandboxProcessObject and then polls for it to complete.
435 *
436 * @param sandboxProcessName sanbox process name
437 * @param options { wait?: Duration; interval?: Duration }
438 * @returns {SandboxProcessObject} The SandboxProcessObject for the sandbox
439 */
440 private authWithRetriesByName;
441 /**
442 * Polls the sandbox org for the sandboxProcessObject.
443 *
444 * @param sandboxProcessObj: The in-progress sandbox signup request
445 * @param options { wait?: Duration; interval?: Duration }
446 * @returns {SandboxProcessObject}
447 */
448 private authWithRetries;
449 /**
450 * Query the sandbox for the SandboxProcessObject by sandbox name
451 *
452 * @param sandboxName The name of the sandbox to query
453 * @returns {SandboxProcessObject} The SandboxProcessObject for the sandbox
454 */
455 private queryLatestSandboxProcessBySandboxName;
456 private queryProduction;
457 private destroySandbox;
458 private destroyScratchOrg;
459 /**
460 * this method will delete the sandbox org from the production org and clean up any local files
461 *
462 * @param prodOrg - Production org associated with this sandbox
463 * @private
464 */
465 private deleteSandbox;
466 /**
467 * If this Org is a scratch org, calling this method will delete the scratch org from the DevHub and clean up any local files
468 *
469 * @param devHub - optional DevHub Org of the to-be-deleted scratch org
470 * @private
471 */
472 private deleteScratchOrg;
473 /**
474 * Delete an auth info file from the local file system and any related cache information for
475 * this Org. You don't want to call this method directly. Instead consider calling Org.remove()
476 */
477 private removeAuth;
478 /**
479 * Deletes the users config file
480 */
481 private removeUsersConfig;
482 private manageDelete;
483 /**
484 * Remove the org users auth file.
485 *
486 * @param throwWhenRemoveFails true if manageDelete should throw or not if the deleted fails.
487 */
488 private removeUsers;
489 private removeSandboxConfig;
490 private writeSandboxAuthFile;
491 private pollStatusAndAuth;
492 /**
493 * query SandboxProcess using supplied where clause
494 *
495 * @param where clause to query for
496 * @private
497 */
498 private querySandboxProcess;
499 /**
500 * determines if the sandbox has successfully been created
501 *
502 * @param sandboxProcessObj sandbox signup progress
503 * @private
504 */
505 private sandboxSignupComplete;
506 private validateWaitOptions;
507 /**
508 * removes source tracking files hosted in the project/.sf/orgs/<org id>/
509 *
510 * @private
511 */
512 private removeSourceTrackingFiles;
513}
514export declare namespace Org {
515 /**
516 * Constructor Options for and Org.
517 */
518 type Options = {
519 aliasOrUsername?: string;
520 connection?: Connection;
521 aggregator?: ConfigAggregator;
522 isDevHub?: boolean;
523 };
524 /**
525 * Scratch Org status.
526 */
527 enum Status {
528 /**
529 * The scratch org is active.
530 */
531 ACTIVE = "ACTIVE",
532 /**
533 * The scratch org has expired.
534 */
535 EXPIRED = "EXPIRED",
536 /**
537 * The org is a scratch Org but no dev hub is indicated.
538 */
539 UNKNOWN = "UNKNOWN",
540 /**
541 * The dev hub configuration is reporting an active Scratch org but the AuthInfo cannot be found.
542 */
543 MISSING = "MISSING"
544 }
545 /**
546 * Org Fields.
547 */
548 enum Fields {
549 /**
550 * The org alias.
551 */
552 ALIAS = "alias",
553 CREATED = "created",
554 NAME = "name",
555 NAMESPACE_PREFIX = "namespacePrefix",
556 INSTANCE_NAME = "instanceName",
557 TRIAL_EXPIRATION_DATE = "trailExpirationDate",
558 /**
559 * The Salesforce instance the org was created on. e.g. `cs42`.
560 */
561 CREATED_ORG_INSTANCE = "createdOrgInstance",
562 /**
563 * The username of the dev hub org that created this org. Only populated for scratch orgs.
564 */
565 DEV_HUB_USERNAME = "devHubUsername",
566 /**
567 * The full url of the instance the org lives on.
568 */
569 INSTANCE_URL = "instanceUrl",
570 /**
571 * Is the current org a dev hub org. e.g. They have access to the `ScratchOrgInfo` object.
572 */
573 IS_DEV_HUB = "isDevHub",
574 /**
575 * Is the current org a scratch org. e.g. Organization has IsSandbox == true and TrialExpirationDate != null.
576 */
577 IS_SCRATCH = "isScratch",
578 /**
579 * Is the current org a sandbox (not a scratch org on a non-prod instance), but an actual Sandbox org). e.g. Organization has IsSandbox == true and TrialExpirationDate == null.
580 */
581 IS_SANDBOX = "isSandbox",
582 /**
583 * The login url of the org. e.g. `https://login.salesforce.com` or `https://test.salesforce.com`.
584 */
585 LOGIN_URL = "loginUrl",
586 /**
587 * The org ID.
588 */
589 ORG_ID = "orgId",
590 /**
591 * The `OrgStatus` of the org.
592 */
593 STATUS = "status",
594 /**
595 * The snapshot used to create the scratch org.
596 */
597 SNAPSHOT = "snapshot",
598 /**
599 * true: the org supports and wants source tracking
600 * false: the org opted out of tracking or can't support it
601 */
602 TRACKS_SOURCE = "tracksSource"
603 }
604}