1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | const kit_1 = require("@salesforce/kit");
|
10 | const ts_types_1 = require("@salesforce/ts-types");
|
11 | const path_1 = require("path");
|
12 | const authInfo_1 = require("./authInfo");
|
13 | const aliases_1 = require("./config/aliases");
|
14 | const authInfoConfig_1 = require("./config/authInfoConfig");
|
15 | const config_1 = require("./config/config");
|
16 | const configAggregator_1 = require("./config/configAggregator");
|
17 | const orgUsersConfig_1 = require("./config/orgUsersConfig");
|
18 | const sandboxOrgConfig_1 = require("./config/sandboxOrgConfig");
|
19 | const connection_1 = require("./connection");
|
20 | const global_1 = require("./global");
|
21 | const logger_1 = require("./logger");
|
22 | const sfdxError_1 = require("./sfdxError");
|
23 | const fs_1 = require("./util/fs");
|
24 | const sfdc_1 = require("./util/sfdc");
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | class Org extends kit_1.AsyncCreatable {
|
52 | |
53 |
|
54 |
|
55 | constructor(options) {
|
56 | super(options);
|
57 |
|
58 | this.status = Org.Status.UNKNOWN;
|
59 | this.options = options;
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | async cleanLocalOrgData(orgDataPath, throwWhenRemoveFails = false) {
|
67 | let dataPath;
|
68 | try {
|
69 | const rootFolder = await config_1.Config.resolveRootFolder(false);
|
70 | dataPath = path_1.join(rootFolder, global_1.Global.STATE_FOLDER, orgDataPath ? orgDataPath : 'orgs');
|
71 | this.logger.debug(`cleaning data for path: ${dataPath}`);
|
72 | }
|
73 | catch (err) {
|
74 | if (err.name === 'InvalidProjectWorkspace') {
|
75 |
|
76 |
|
77 |
|
78 | return;
|
79 | }
|
80 | throw err;
|
81 | }
|
82 | return this.manageDelete(async () => await fs_1.fs.remove(dataPath), dataPath, throwWhenRemoveFails);
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | async retrieveOrgUsersConfig() {
|
88 | return await orgUsersConfig_1.OrgUsersConfig.create(orgUsersConfig_1.OrgUsersConfig.getOptions(this.getOrgId()));
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | async remove(throwWhenRemoveFails = false) {
|
97 |
|
98 |
|
99 | if (this.getConnection().isUsingAccessToken()) {
|
100 | return Promise.resolve();
|
101 | }
|
102 | await this.removeSandboxConfig(throwWhenRemoveFails);
|
103 | await this.removeUsers(throwWhenRemoveFails);
|
104 | await this.removeUsersConfig();
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | await this.removeAuth();
|
110 | }
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | async checkScratchOrg(devHubUsernameOrAlias) {
|
121 | let aliasOrUsername = devHubUsernameOrAlias;
|
122 | if (!aliasOrUsername) {
|
123 | aliasOrUsername = ts_types_1.asString(this.configAggregator.getPropertyValue(config_1.Config.DEFAULT_DEV_HUB_USERNAME));
|
124 | }
|
125 | const devHubConnection = (await Org.create({ aliasOrUsername })).getConnection();
|
126 | const thisOrgAuthConfig = this.getConnection().getAuthInfoFields();
|
127 | const trimmedId = sfdc_1.sfdc.trimTo15(thisOrgAuthConfig.orgId);
|
128 | const DEV_HUB_SOQL = `SELECT CreatedDate,Edition,ExpirationDate FROM ActiveScratchOrg WHERE ScratchOrg=\'${trimmedId}\'`;
|
129 | let results;
|
130 | try {
|
131 | results = await devHubConnection.query(DEV_HUB_SOQL);
|
132 | }
|
133 | catch (err) {
|
134 | if (err.name === 'INVALID_TYPE') {
|
135 | throw sfdxError_1.SfdxError.create('@salesforce/core', 'org', 'NotADevHub', [devHubConnection.getUsername()]);
|
136 | }
|
137 | throw err;
|
138 | }
|
139 | if (ts_types_1.getNumber(results, 'records.length') !== 1) {
|
140 | throw new sfdxError_1.SfdxError('No results', 'NoResults');
|
141 | }
|
142 | return thisOrgAuthConfig;
|
143 | }
|
144 | |
145 |
|
146 |
|
147 | async getDevHubOrg() {
|
148 | if (this.isDevHubOrg()) {
|
149 | return this;
|
150 | }
|
151 | else if (this.getField(Org.Fields.DEV_HUB_USERNAME)) {
|
152 | const devHubUsername = ts_types_1.ensureString(this.getField(Org.Fields.DEV_HUB_USERNAME));
|
153 | return Org.create({
|
154 | connection: await connection_1.Connection.create({
|
155 | authInfo: await authInfo_1.AuthInfo.create({ username: devHubUsername })
|
156 | })
|
157 | });
|
158 | }
|
159 | }
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | isDevHubOrg() {
|
168 | const isDevHub = this.getField(Org.Fields.IS_DEV_HUB);
|
169 | if (ts_types_1.isBoolean(isDevHub)) {
|
170 | return isDevHub;
|
171 | }
|
172 | else {
|
173 | return false;
|
174 | }
|
175 | }
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | async determineIfDevHubOrg(forceServerCheck = false) {
|
187 | const cachedIsDevHub = this.getField(Org.Fields.IS_DEV_HUB);
|
188 | if (!forceServerCheck && ts_types_1.isBoolean(cachedIsDevHub)) {
|
189 | return cachedIsDevHub;
|
190 | }
|
191 | if (this.isDevHubOrg()) {
|
192 | return true;
|
193 | }
|
194 | this.logger.debug('isDevHub is not cached - querying server...');
|
195 | const conn = this.getConnection();
|
196 | let isDevHub = false;
|
197 | try {
|
198 | await conn.query('SELECT Id FROM ScratchOrgInfo');
|
199 | isDevHub = true;
|
200 | }
|
201 | catch (err) {
|
202 |
|
203 | }
|
204 | const username = ts_types_1.ensure(this.getUsername());
|
205 | const auth = await authInfo_1.AuthInfo.create({ username });
|
206 | await auth.save({ isDevHub });
|
207 | authInfo_1.AuthInfo.clearCache(username);
|
208 |
|
209 | this.connection = await connection_1.Connection.create({
|
210 | authInfo: await authInfo_1.AuthInfo.create({ username })
|
211 | });
|
212 | return isDevHub;
|
213 | }
|
214 | |
215 |
|
216 |
|
217 | async refreshAuth() {
|
218 | this.logger.debug('Refreshing auth for org.');
|
219 | const requestInfo = {
|
220 | url: this.getConnection().baseUrl(),
|
221 | method: 'GET'
|
222 | };
|
223 | const conn = this.getConnection();
|
224 | await conn.request(requestInfo);
|
225 | }
|
226 | |
227 |
|
228 |
|
229 | async readUserAuthFiles() {
|
230 | const config = await this.retrieveOrgUsersConfig();
|
231 | const contents = await config.read();
|
232 | const thisUsername = ts_types_1.ensure(this.getUsername());
|
233 | const usernames = ts_types_1.ensureJsonArray(contents.usernames || [thisUsername]);
|
234 | return Promise.all(usernames.map(username => {
|
235 | if (username === thisUsername) {
|
236 | return authInfo_1.AuthInfo.create({
|
237 | username: this.getConnection().getUsername()
|
238 | });
|
239 | }
|
240 | else {
|
241 | return authInfo_1.AuthInfo.create({ username: ts_types_1.ensureString(username) });
|
242 | }
|
243 | }));
|
244 | }
|
245 | |
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | async addUsername(auth) {
|
263 | if (!auth) {
|
264 | throw new sfdxError_1.SfdxError('Missing auth info', 'MissingAuthInfo');
|
265 | }
|
266 | const _auth = ts_types_1.isString(auth) ? await authInfo_1.AuthInfo.create({ username: auth }) : auth;
|
267 | this.logger.debug(`adding username ${_auth.getFields().username}`);
|
268 | const orgConfig = await this.retrieveOrgUsersConfig();
|
269 | const contents = await orgConfig.read();
|
270 |
|
271 |
|
272 | const usernames = contents.usernames || [];
|
273 | if (!ts_types_1.isArray(usernames)) {
|
274 | throw new sfdxError_1.SfdxError('Usernames is not an array', 'UnexpectedDataFormat');
|
275 | }
|
276 | let shouldUpdate = false;
|
277 | const thisUsername = ts_types_1.ensure(this.getUsername());
|
278 | if (!usernames.includes(thisUsername)) {
|
279 | usernames.push(thisUsername);
|
280 | shouldUpdate = true;
|
281 | }
|
282 | const username = _auth.getFields().username;
|
283 | if (username) {
|
284 | usernames.push(username);
|
285 | shouldUpdate = true;
|
286 | }
|
287 | if (shouldUpdate) {
|
288 | orgConfig.set('usernames', usernames);
|
289 | await orgConfig.write();
|
290 | }
|
291 | return this;
|
292 | }
|
293 | |
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | async removeUsername(auth) {
|
301 | if (!auth) {
|
302 | throw new sfdxError_1.SfdxError('Missing auth info', 'MissingAuthInfo');
|
303 | }
|
304 | const _auth = ts_types_1.isString(auth) ? await authInfo_1.AuthInfo.create({ username: auth }) : auth;
|
305 | this.logger.debug(`removing username ${_auth.getFields().username}`);
|
306 | const orgConfig = await this.retrieveOrgUsersConfig();
|
307 | const contents = await orgConfig.read();
|
308 | const targetUser = _auth.getFields().username;
|
309 | const usernames = (contents.usernames || []);
|
310 | contents.usernames = usernames.filter(username => username !== targetUser);
|
311 | await orgConfig.write();
|
312 | return this;
|
313 | }
|
314 | |
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | async setSandboxOrgConfigField(field, value) {
|
322 | const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
|
323 | sandboxOrgConfig.set(field, value);
|
324 | await sandboxOrgConfig.write();
|
325 | return this;
|
326 | }
|
327 | |
328 |
|
329 |
|
330 | async getSandboxOrgConfigField(field) {
|
331 | const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
|
332 | return sandboxOrgConfig.get(field);
|
333 | }
|
334 | |
335 |
|
336 |
|
337 |
|
338 |
|
339 | async retrieveMaxApiVersion() {
|
340 | return await this.getConnection().retrieveMaxApiVersion();
|
341 | }
|
342 | |
343 |
|
344 |
|
345 | getUsername() {
|
346 | return this.getConnection().getUsername();
|
347 | }
|
348 | |
349 |
|
350 |
|
351 | getOrgId() {
|
352 | return this.getField(Org.Fields.ORG_ID);
|
353 | }
|
354 | |
355 |
|
356 |
|
357 | getConfigAggregator() {
|
358 | return this.configAggregator;
|
359 | }
|
360 | |
361 |
|
362 |
|
363 | getField(key) {
|
364 |
|
365 | return this[key] || this.getConnection().getAuthInfoFields()[key];
|
366 | }
|
367 | |
368 |
|
369 |
|
370 | getFields(keys) {
|
371 | const json = {};
|
372 | return keys.reduce((map, key) => {
|
373 | map[key] = this.getField(key);
|
374 | return map;
|
375 | }, json);
|
376 | }
|
377 | |
378 |
|
379 |
|
380 | getConnection() {
|
381 | return this.connection;
|
382 | }
|
383 | |
384 |
|
385 |
|
386 | async init() {
|
387 | this.logger = await logger_1.Logger.child('Org');
|
388 | this.configAggregator = this.options.aggregator ? this.options.aggregator : await configAggregator_1.ConfigAggregator.create();
|
389 | if (!this.options.connection) {
|
390 | if (this.options.aliasOrUsername == null) {
|
391 | this.configAggregator = this.getConfigAggregator();
|
392 | const aliasOrUsername = this.options.isDevHub
|
393 | ? ts_types_1.getString(this.configAggregator.getInfo(config_1.Config.DEFAULT_DEV_HUB_USERNAME), 'value')
|
394 | : ts_types_1.getString(this.configAggregator.getInfo(config_1.Config.DEFAULT_USERNAME), 'value');
|
395 | this.options.aliasOrUsername = aliasOrUsername || undefined;
|
396 | }
|
397 | const username = this.options.aliasOrUsername;
|
398 | this.connection = await connection_1.Connection.create({
|
399 |
|
400 | authInfo: await authInfo_1.AuthInfo.create({
|
401 | username: (username != null && (await aliases_1.Aliases.fetch(username))) || username
|
402 | })
|
403 | });
|
404 | }
|
405 | else {
|
406 | this.connection = this.options.connection;
|
407 | }
|
408 | }
|
409 | |
410 |
|
411 |
|
412 | getDefaultOptions() {
|
413 | throw new sfdxError_1.SfdxError('Not Supported');
|
414 | }
|
415 | |
416 |
|
417 |
|
418 |
|
419 | async removeAuth() {
|
420 | const username = ts_types_1.ensure(this.getUsername());
|
421 | this.logger.debug(`Removing auth for user: ${username}`);
|
422 | const config = await authInfoConfig_1.AuthInfoConfig.create(Object.assign({}, authInfoConfig_1.AuthInfoConfig.getOptions(username), { throwOnNotFound: false }));
|
423 | this.logger.debug(`Clearing auth cache for user: ${username}`);
|
424 | authInfo_1.AuthInfo.clearCache(username);
|
425 | if (await config.exists()) {
|
426 | await config.unlink();
|
427 | }
|
428 | }
|
429 | |
430 |
|
431 |
|
432 | async removeUsersConfig() {
|
433 | const config = await this.retrieveOrgUsersConfig();
|
434 | if (await config.exists()) {
|
435 | this.logger.debug(`Removing org users config at: ${config.getPath()}`);
|
436 | await config.unlink();
|
437 | }
|
438 | }
|
439 | |
440 |
|
441 |
|
442 | async retrieveSandboxOrgConfig() {
|
443 | return await sandboxOrgConfig_1.SandboxOrgConfig.create(sandboxOrgConfig_1.SandboxOrgConfig.getOptions(this.getOrgId()));
|
444 | }
|
445 | manageDelete(cb, dirPath, throwWhenRemoveFails) {
|
446 | return cb().catch(e => {
|
447 | if (throwWhenRemoveFails) {
|
448 | throw e;
|
449 | }
|
450 | else {
|
451 | this.logger.warn(`failed to read directory ${dirPath}`);
|
452 | return;
|
453 | }
|
454 | });
|
455 | }
|
456 | |
457 |
|
458 |
|
459 |
|
460 | async removeUsers(throwWhenRemoveFails) {
|
461 | this.logger.debug(`Removing users associate with org: ${this.getOrgId()}`);
|
462 | const config = await this.retrieveOrgUsersConfig();
|
463 | this.logger.debug(`using path for org users: ${config.getPath()}`);
|
464 | if (await config.exists()) {
|
465 | const _auths = await this.readUserAuthFiles();
|
466 | const aliases = await aliases_1.Aliases.create(aliases_1.Aliases.getDefaultOptions());
|
467 | this.logger.info(`Cleaning up usernames in org: ${this.getOrgId()}`);
|
468 | for (const auth of _auths) {
|
469 | const username = auth.getFields().username;
|
470 | const aliasKeys = (username && aliases.getKeysByValue(username)) || [];
|
471 | aliases.unsetAll(aliasKeys);
|
472 | let orgForUser;
|
473 | if (username === this.getUsername()) {
|
474 | orgForUser = this;
|
475 | }
|
476 | else {
|
477 | const _info = await authInfo_1.AuthInfo.create({ username });
|
478 | const connection = await connection_1.Connection.create({ authInfo: _info });
|
479 | orgForUser = await Org.create({ connection });
|
480 | }
|
481 | const orgType = this.isDevHubOrg() ? config_1.Config.DEFAULT_DEV_HUB_USERNAME : config_1.Config.DEFAULT_USERNAME;
|
482 | const configInfo = await orgForUser.configAggregator.getInfo(orgType);
|
483 | if ((configInfo.value === username || aliasKeys.includes(configInfo.value)) &&
|
484 | (configInfo.isGlobal() || configInfo.isLocal())) {
|
485 | await config_1.Config.update(configInfo.isGlobal(), orgType, undefined);
|
486 | }
|
487 | await orgForUser.removeAuth();
|
488 | }
|
489 | await aliases.write();
|
490 | }
|
491 | }
|
492 | |
493 |
|
494 |
|
495 |
|
496 | async removeSandboxConfig(throwWhenRemoveFails) {
|
497 | const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
|
498 | if (await sandboxOrgConfig.exists()) {
|
499 | await this.manageDelete(async () => await sandboxOrgConfig.unlink(), sandboxOrgConfig.getPath(), throwWhenRemoveFails);
|
500 | }
|
501 | }
|
502 | }
|
503 | exports.Org = Org;
|
504 | (function (Org) {
|
505 | |
506 |
|
507 |
|
508 | let Status;
|
509 | (function (Status) {
|
510 | |
511 |
|
512 |
|
513 | Status["ACTIVE"] = "ACTIVE";
|
514 | |
515 |
|
516 |
|
517 | Status["EXPIRED"] = "EXPIRED";
|
518 | |
519 |
|
520 |
|
521 | Status["UNKNOWN"] = "UNKNOWN";
|
522 | |
523 |
|
524 |
|
525 | Status["MISSING"] = "MISSING";
|
526 | })(Status = Org.Status || (Org.Status = {}));
|
527 | |
528 |
|
529 |
|
530 |
|
531 |
|
532 | let Fields;
|
533 | (function (Fields) {
|
534 | |
535 |
|
536 |
|
537 |
|
538 | Fields["ALIAS"] = "alias";
|
539 | Fields["CREATED"] = "created";
|
540 | |
541 |
|
542 |
|
543 | Fields["CREATED_ORG_INSTANCE"] = "createdOrgInstance";
|
544 | |
545 |
|
546 |
|
547 | Fields["DEV_HUB_USERNAME"] = "devHubUsername";
|
548 | |
549 |
|
550 |
|
551 | Fields["INSTANCE_URL"] = "instanceUrl";
|
552 | |
553 |
|
554 |
|
555 | Fields["IS_DEV_HUB"] = "isDevHub";
|
556 | |
557 |
|
558 |
|
559 | Fields["LOGIN_URL"] = "loginUrl";
|
560 | |
561 |
|
562 |
|
563 | Fields["ORG_ID"] = "orgId";
|
564 | |
565 |
|
566 |
|
567 | Fields["STATUS"] = "status";
|
568 | |
569 |
|
570 |
|
571 | Fields["SNAPSHOT"] = "snapshot";
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 | })(Fields = Org.Fields || (Org.Fields = {}));
|
584 | })(Org = exports.Org || (exports.Org = {}));
|
585 |
|
\ | No newline at end of file |