@salesforce/core 4.0.0-v3.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/LICENSE.txt +1 -1
  2. package/README.md +93 -44
  3. package/lib/config/aliasesConfig.d.ts +12 -0
  4. package/lib/config/aliasesConfig.js +28 -0
  5. package/lib/config/authInfoConfig.d.ts +19 -0
  6. package/lib/config/authInfoConfig.js +35 -0
  7. package/lib/config/config.d.ts +181 -48
  8. package/lib/config/config.js +382 -159
  9. package/lib/config/configAggregator.d.ts +59 -42
  10. package/lib/config/configAggregator.js +135 -82
  11. package/lib/config/configFile.d.ts +2 -2
  12. package/lib/config/configFile.js +40 -31
  13. package/lib/config/configGroup.d.ts +9 -9
  14. package/lib/config/configGroup.js +13 -11
  15. package/lib/config/configStore.d.ts +9 -9
  16. package/lib/config/configStore.js +29 -26
  17. package/lib/config/envVars.d.ts +102 -0
  18. package/lib/config/envVars.js +457 -0
  19. package/lib/config/orgUsersConfig.d.ts +8 -0
  20. package/lib/config/orgUsersConfig.js +12 -0
  21. package/lib/config/sandboxOrgConfig.d.ts +8 -0
  22. package/lib/config/sandboxOrgConfig.js +12 -0
  23. package/lib/config/sandboxProcessCache.d.ts +16 -0
  24. package/lib/config/sandboxProcessCache.js +38 -0
  25. package/lib/config/tokensConfig.d.ts +10 -0
  26. package/lib/config/tokensConfig.js +29 -0
  27. package/lib/config/ttlConfig.d.ts +34 -0
  28. package/lib/config/ttlConfig.js +50 -0
  29. package/lib/crypto/crypto.js +29 -16
  30. package/lib/crypto/keyChain.js +4 -4
  31. package/lib/crypto/keyChainImpl.d.ts +5 -3
  32. package/lib/crypto/keyChainImpl.js +65 -66
  33. package/lib/crypto/secureBuffer.d.ts +1 -1
  34. package/lib/crypto/secureBuffer.js +1 -1
  35. package/lib/deviceOauthService.d.ts +5 -5
  36. package/lib/deviceOauthService.js +37 -33
  37. package/lib/exported.d.ts +22 -15
  38. package/lib/exported.js +49 -25
  39. package/lib/global.d.ts +17 -3
  40. package/lib/global.js +46 -13
  41. package/lib/lifecycleEvents.d.ts +39 -2
  42. package/lib/lifecycleEvents.js +77 -3
  43. package/lib/logger.d.ts +21 -11
  44. package/lib/logger.js +121 -105
  45. package/lib/messages.d.ts +53 -36
  46. package/lib/messages.js +89 -97
  47. package/lib/org/authInfo.d.ts +84 -72
  48. package/lib/org/authInfo.js +326 -320
  49. package/lib/org/authRemover.d.ts +16 -23
  50. package/lib/org/authRemover.js +62 -60
  51. package/lib/org/connection.d.ts +33 -59
  52. package/lib/org/connection.js +129 -190
  53. package/lib/org/index.js +6 -2
  54. package/lib/org/org.d.ts +263 -38
  55. package/lib/org/org.js +734 -149
  56. package/lib/org/orgConfigProperties.d.ts +69 -0
  57. package/lib/org/orgConfigProperties.js +121 -0
  58. package/lib/org/permissionSetAssignment.js +6 -15
  59. package/lib/org/scratchOrgCache.d.ts +20 -0
  60. package/lib/org/scratchOrgCache.js +33 -0
  61. package/lib/org/scratchOrgCreate.d.ts +54 -0
  62. package/lib/org/scratchOrgCreate.js +214 -0
  63. package/lib/org/scratchOrgErrorCodes.d.ts +10 -0
  64. package/lib/org/scratchOrgErrorCodes.js +79 -0
  65. package/lib/org/scratchOrgFeatureDeprecation.d.ts +26 -0
  66. package/lib/org/scratchOrgFeatureDeprecation.js +105 -0
  67. package/lib/org/scratchOrgInfoApi.d.ts +68 -0
  68. package/lib/org/scratchOrgInfoApi.js +416 -0
  69. package/lib/org/scratchOrgInfoGenerator.d.ts +64 -0
  70. package/lib/org/scratchOrgInfoGenerator.js +237 -0
  71. package/lib/org/scratchOrgLifecycleEvents.d.ts +10 -0
  72. package/lib/org/scratchOrgLifecycleEvents.js +41 -0
  73. package/lib/org/scratchOrgSettingsGenerator.d.ts +79 -0
  74. package/lib/org/scratchOrgSettingsGenerator.js +277 -0
  75. package/lib/org/scratchOrgTypes.d.ts +43 -0
  76. package/lib/{status/client.js → org/scratchOrgTypes.js} +1 -1
  77. package/lib/org/user.d.ts +7 -2
  78. package/lib/org/user.js +78 -54
  79. package/lib/schema/printer.d.ts +6 -0
  80. package/lib/schema/printer.js +49 -46
  81. package/lib/schema/validator.d.ts +12 -10
  82. package/lib/schema/validator.js +56 -76
  83. package/lib/{sfdxError.d.ts → sfError.d.ts} +12 -15
  84. package/lib/{sfdxError.js → sfError.js} +42 -24
  85. package/lib/{sfdxProject.d.ts → sfProject.d.ts} +75 -35
  86. package/lib/sfProject.js +651 -0
  87. package/lib/stateAggregator/accessors/aliasAccessor.d.ts +98 -0
  88. package/lib/stateAggregator/accessors/aliasAccessor.js +146 -0
  89. package/lib/stateAggregator/accessors/orgAccessor.d.ts +101 -0
  90. package/lib/stateAggregator/accessors/orgAccessor.js +240 -0
  91. package/lib/stateAggregator/accessors/sandboxAccessor.d.ts +8 -0
  92. package/lib/stateAggregator/accessors/sandboxAccessor.js +28 -0
  93. package/lib/stateAggregator/accessors/tokenAccessor.d.ts +63 -0
  94. package/lib/stateAggregator/accessors/tokenAccessor.js +80 -0
  95. package/lib/stateAggregator/index.d.ts +4 -0
  96. package/lib/stateAggregator/index.js +27 -0
  97. package/lib/stateAggregator/stateAggregator.d.ts +25 -0
  98. package/lib/stateAggregator/stateAggregator.js +46 -0
  99. package/lib/status/myDomainResolver.d.ts +1 -1
  100. package/lib/status/myDomainResolver.js +10 -10
  101. package/lib/status/pollingClient.d.ts +2 -6
  102. package/lib/status/pollingClient.js +38 -64
  103. package/lib/status/streamingClient.d.ts +5 -80
  104. package/lib/status/streamingClient.js +74 -94
  105. package/lib/status/types.d.ts +89 -0
  106. package/lib/status/types.js +18 -0
  107. package/lib/testSetup.d.ts +212 -79
  108. package/lib/testSetup.js +478 -182
  109. package/lib/util/cache.d.ts +11 -0
  110. package/lib/util/cache.js +70 -0
  111. package/lib/util/checkLightningDomain.d.ts +1 -0
  112. package/lib/util/checkLightningDomain.js +29 -0
  113. package/lib/util/directoryWriter.d.ts +12 -0
  114. package/lib/util/directoryWriter.js +54 -0
  115. package/lib/util/getJwtAudienceUrl.d.ts +4 -0
  116. package/lib/util/getJwtAudienceUrl.js +19 -0
  117. package/lib/util/internal.d.ts +28 -2
  118. package/lib/util/internal.js +65 -8
  119. package/lib/util/jsonXmlTools.d.ts +14 -0
  120. package/lib/util/jsonXmlTools.js +39 -0
  121. package/lib/util/mapKeys.d.ts +14 -0
  122. package/lib/util/mapKeys.js +52 -0
  123. package/lib/util/sfdc.d.ts +51 -63
  124. package/lib/util/sfdc.js +75 -127
  125. package/lib/util/sfdcUrl.d.ts +64 -0
  126. package/lib/util/sfdcUrl.js +197 -0
  127. package/lib/util/structuredWriter.d.ts +9 -0
  128. package/lib/util/structuredWriter.js +3 -0
  129. package/lib/util/zipWriter.d.ts +16 -0
  130. package/lib/util/zipWriter.js +68 -0
  131. package/lib/webOAuthServer.d.ts +20 -7
  132. package/lib/webOAuthServer.js +107 -60
  133. package/messageTransformer/messageTransformer.ts +93 -0
  134. package/messages/auth.md +11 -3
  135. package/messages/config.md +94 -6
  136. package/messages/connection.md +8 -0
  137. package/messages/core.json +3 -3
  138. package/messages/core.md +11 -1
  139. package/messages/envVars.md +313 -0
  140. package/messages/org.md +64 -0
  141. package/messages/scratchOrgCreate.md +23 -0
  142. package/messages/scratchOrgErrorCodes.md +115 -0
  143. package/messages/scratchOrgFeatureDeprecation.md +11 -0
  144. package/messages/scratchOrgInfoApi.md +20 -0
  145. package/messages/scratchOrgInfoGenerator.md +27 -0
  146. package/messages/user.md +12 -0
  147. package/package.json +138 -66
  148. package/CHANGELOG.md +0 -699
  149. package/lib/config/aliases.d.ts +0 -56
  150. package/lib/config/aliases.js +0 -96
  151. package/lib/config/globalInfoConfig.d.ts +0 -74
  152. package/lib/config/globalInfoConfig.js +0 -144
  153. package/lib/config/keychainConfig.d.ts +0 -19
  154. package/lib/config/keychainConfig.js +0 -43
  155. package/lib/config/sfdxDataHandler.d.ts +0 -36
  156. package/lib/config/sfdxDataHandler.js +0 -165
  157. package/lib/sfdxProject.js +0 -546
  158. package/lib/status/client.d.ts +0 -15
  159. package/lib/util/fs.d.ts +0 -198
  160. package/lib/util/fs.js +0 -374
@@ -0,0 +1,98 @@
1
+ import { AsyncOptionalCreatable } from '@salesforce/kit';
2
+ import { Nullable } from '@salesforce/ts-types';
3
+ import { AuthFields, ConfigContents } from '../../exported';
4
+ import { SfToken } from './tokenAccessor';
5
+ export type Aliasable = string | (Partial<AuthFields> & Partial<SfToken>);
6
+ export declare class AliasAccessor extends AsyncOptionalCreatable {
7
+ private config;
8
+ /**
9
+ * Returns all the aliases for all the values
10
+ */
11
+ getAll(): ConfigContents<string>;
12
+ /**
13
+ * Returns all the aliases for a given entity
14
+ *
15
+ * @param entity the aliasable entity that you want to get the aliases of
16
+ */
17
+ getAll(entity: Aliasable): string[];
18
+ /**
19
+ * Returns the first alias found for a given entity
20
+ *
21
+ * @param entity the aliasable entity that you want to get the alias of
22
+ */
23
+ get(entity: Aliasable): Nullable<string>;
24
+ /**
25
+ * Returns the value that corresponds to the given alias if it exists
26
+ *
27
+ * @param alias the alias that corresponds to a value
28
+ */
29
+ getValue(alias: string): Nullable<string>;
30
+ /**
31
+ * Returns the username that corresponds to the given alias if it exists
32
+ *
33
+ * @param alias the alias that corresponds to a username
34
+ */
35
+ getUsername(alias: string): Nullable<string>;
36
+ /**
37
+ * If the provided string is an alias, it returns the corresponding value.
38
+ * If the provided string is not an alias, we assume that the provided string
39
+ * is the value and return it.
40
+ *
41
+ * This method is helpful when you don't know if the string you have is a value
42
+ * or an alias.
43
+ *
44
+ * @param valueOrAlias a string that might be a value or might be an alias
45
+ */
46
+ resolveValue(valueOrAlias: string): string;
47
+ /**
48
+ * If the provided string is an alias, it returns the corresponding username.
49
+ * If the provided string is not an alias, we assume that the provided string
50
+ * is the username and return it.
51
+ *
52
+ * This method is helpful when you don't know if the string you have is a username
53
+ * or an alias.
54
+ *
55
+ * @param usernameOrAlias a string that might be a username or might be an alias
56
+ */
57
+ resolveUsername(usernameOrAlias: string): string;
58
+ /**
59
+ * If the provided string is an alias, return it.
60
+ * If the provided string is not an alias, return the username of the provided alias
61
+ *
62
+ * This method is helpful when you don't know if the string you have is a username
63
+ * or an alias.
64
+ *
65
+ * @param usernameOrAlias a string that might be a username or might be an alias
66
+ */
67
+ resolveAlias(usernameOrAlias: string): Nullable<string>;
68
+ /**
69
+ * Set an alias for the given aliasable entity
70
+ *
71
+ * @param alias the alias you want to set
72
+ * @param entity the aliasable entity that's being aliased
73
+ */
74
+ set(alias: string, entity: Aliasable): void;
75
+ /**
76
+ * Unset the given alias.
77
+ *
78
+ */
79
+ unset(alias: string): void;
80
+ /**
81
+ * Unsets all the aliases for the given entity.
82
+ *
83
+ * @param entity the aliasable entity for which you want to unset all aliases
84
+ */
85
+ unsetAll(entity: Aliasable): void;
86
+ write(): Promise<ConfigContents>;
87
+ /**
88
+ * Returns true if the provided alias exists
89
+ *
90
+ * @param alias the alias you want to check
91
+ */
92
+ has(alias: string): boolean;
93
+ protected init(): Promise<void>;
94
+ /**
95
+ * Returns the username of given aliasable entity
96
+ */
97
+ private getNameOf;
98
+ }
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2021, 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
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.AliasAccessor = void 0;
10
+ const kit_1 = require("@salesforce/kit");
11
+ const aliasesConfig_1 = require("../../config/aliasesConfig");
12
+ const sfError_1 = require("../../sfError");
13
+ class AliasAccessor extends kit_1.AsyncOptionalCreatable {
14
+ getAll(entity) {
15
+ // This will only return aliases under "orgs". This will need to be modified
16
+ // if/when we want to support more aliases groups.
17
+ const all = (this.config.getGroup() ?? {});
18
+ if (entity) {
19
+ const value = this.getNameOf(entity);
20
+ return Object.entries(all)
21
+ .filter((entry) => entry[1] === value)
22
+ .map((entry) => entry[0]);
23
+ }
24
+ else {
25
+ return all;
26
+ }
27
+ }
28
+ /**
29
+ * Returns the first alias found for a given entity
30
+ *
31
+ * @param entity the aliasable entity that you want to get the alias of
32
+ */
33
+ get(entity) {
34
+ return this.getAll(entity).find((alias) => alias) ?? null;
35
+ }
36
+ /**
37
+ * Returns the value that corresponds to the given alias if it exists
38
+ *
39
+ * @param alias the alias that corresponds to a value
40
+ */
41
+ getValue(alias) {
42
+ return this.getAll()[alias] ?? null;
43
+ }
44
+ /**
45
+ * Returns the username that corresponds to the given alias if it exists
46
+ *
47
+ * @param alias the alias that corresponds to a username
48
+ */
49
+ getUsername(alias) {
50
+ return this.getAll()[alias] ?? null;
51
+ }
52
+ /**
53
+ * If the provided string is an alias, it returns the corresponding value.
54
+ * If the provided string is not an alias, we assume that the provided string
55
+ * is the value and return it.
56
+ *
57
+ * This method is helpful when you don't know if the string you have is a value
58
+ * or an alias.
59
+ *
60
+ * @param valueOrAlias a string that might be a value or might be an alias
61
+ */
62
+ resolveValue(valueOrAlias) {
63
+ return this.getValue(valueOrAlias) ?? valueOrAlias;
64
+ }
65
+ /**
66
+ * If the provided string is an alias, it returns the corresponding username.
67
+ * If the provided string is not an alias, we assume that the provided string
68
+ * is the username and return it.
69
+ *
70
+ * This method is helpful when you don't know if the string you have is a username
71
+ * or an alias.
72
+ *
73
+ * @param usernameOrAlias a string that might be a username or might be an alias
74
+ */
75
+ resolveUsername(usernameOrAlias) {
76
+ return this.getUsername(usernameOrAlias) ?? usernameOrAlias;
77
+ }
78
+ /**
79
+ * If the provided string is an alias, return it.
80
+ * If the provided string is not an alias, return the username of the provided alias
81
+ *
82
+ * This method is helpful when you don't know if the string you have is a username
83
+ * or an alias.
84
+ *
85
+ * @param usernameOrAlias a string that might be a username or might be an alias
86
+ */
87
+ resolveAlias(usernameOrAlias) {
88
+ if (this.has(usernameOrAlias))
89
+ return usernameOrAlias;
90
+ return Object.entries(this.getAll()).find(([, username]) => username === usernameOrAlias)?.[0];
91
+ }
92
+ /**
93
+ * Set an alias for the given aliasable entity
94
+ *
95
+ * @param alias the alias you want to set
96
+ * @param entity the aliasable entity that's being aliased
97
+ */
98
+ set(alias, entity) {
99
+ this.config.set(alias, this.getNameOf(entity));
100
+ }
101
+ /**
102
+ * Unset the given alias.
103
+ *
104
+ */
105
+ unset(alias) {
106
+ this.config.unset(alias);
107
+ }
108
+ /**
109
+ * Unsets all the aliases for the given entity.
110
+ *
111
+ * @param entity the aliasable entity for which you want to unset all aliases
112
+ */
113
+ unsetAll(entity) {
114
+ const aliases = this.getAll(entity);
115
+ aliases.forEach((alias) => this.unset(alias));
116
+ }
117
+ async write() {
118
+ return this.config.write();
119
+ }
120
+ /**
121
+ * Returns true if the provided alias exists
122
+ *
123
+ * @param alias the alias you want to check
124
+ */
125
+ has(alias) {
126
+ return this.config.has(alias);
127
+ }
128
+ async init() {
129
+ this.config = await aliasesConfig_1.AliasesConfig.create(aliasesConfig_1.AliasesConfig.getDefaultOptions());
130
+ }
131
+ /**
132
+ * Returns the username of given aliasable entity
133
+ */
134
+ // eslint-disable-next-line class-methods-use-this
135
+ getNameOf(entity) {
136
+ if (typeof entity === 'string')
137
+ return entity;
138
+ const aliaseeName = entity.username ?? entity.user;
139
+ if (!aliaseeName) {
140
+ throw new sfError_1.SfError(`Invalid aliasee, it must contain a user or username property: ${JSON.stringify(entity)}`);
141
+ }
142
+ return aliaseeName;
143
+ }
144
+ }
145
+ exports.AliasAccessor = AliasAccessor;
146
+ //# sourceMappingURL=aliasAccessor.js.map
@@ -0,0 +1,101 @@
1
+ /// <reference types="node" />
2
+ import * as fs from 'fs';
3
+ import { Nullable } from '@salesforce/ts-types';
4
+ import { AsyncOptionalCreatable } from '@salesforce/kit';
5
+ import { AuthInfoConfig } from '../../config/authInfoConfig';
6
+ import { AuthFields } from '../../org';
7
+ import { ConfigFile } from '../../config/configFile';
8
+ import { ConfigContents } from '../../config/configStore';
9
+ export declare abstract class BaseOrgAccessor<T extends ConfigFile, P extends ConfigContents> extends AsyncOptionalCreatable {
10
+ private configs;
11
+ private contents;
12
+ private logger;
13
+ /**
14
+ * Read the auth file for the given username. Once the file has been read, it can be re-accessed with the `get` method.
15
+ *
16
+ * @param username username to read
17
+ * @param decrypt if true, decrypt encrypted values
18
+ * @param throwOnNotFound throw if file is not found for username
19
+ */
20
+ read(username: string, decrypt?: boolean, throwOnNotFound?: boolean): Promise<Nullable<P>>;
21
+ /**
22
+ * Read all the auth files under the global state directory
23
+ *
24
+ * @param decrypt if true, decrypt encrypted values
25
+ */
26
+ readAll(decrypt?: boolean): Promise<P[]>;
27
+ get(username: string, decrypt?: boolean, throwOnNotFound?: true): P;
28
+ /**
29
+ * Return the contents of all the auth files from cache. The `read` or `readAll` methods must be called first in order to populate the cache.
30
+ *
31
+ * @param decrypt if true, decrypt encrypted values
32
+ * @returns
33
+ */
34
+ getAll(decrypt?: boolean): P[];
35
+ /**
36
+ * Returns true if the username has been cached.
37
+ *
38
+ * @param username
39
+ */
40
+ has(username: string): boolean;
41
+ /**
42
+ * Returns true if there is an auth file for the given username. The `read` or `readAll` methods must be called first in order to populate the cache.
43
+ *
44
+ * @param username
45
+ */
46
+ exists(username: string): Promise<boolean>;
47
+ /**
48
+ * Return the file stats for a given userame's auth file.
49
+ *
50
+ * @param username
51
+ */
52
+ stat(username: string): Promise<Nullable<fs.Stats>>;
53
+ /**
54
+ * Returns true if there is an auth file for the given username
55
+ *
56
+ * @param username
57
+ */
58
+ hasFile(username: string): Promise<boolean>;
59
+ /**
60
+ * Return all auth files under the global state directory.
61
+ */
62
+ list(): Promise<string[]>;
63
+ /**
64
+ * Set the contents for a given username.
65
+ *
66
+ * @param username
67
+ * @param org
68
+ */
69
+ set(username: string, org: P): void;
70
+ /**
71
+ * Update the contents for a given username.
72
+ *
73
+ * @param username
74
+ * @param org
75
+ */
76
+ update(username: string, org: Partial<P>): void;
77
+ /**
78
+ * Delete the auth file for a given username.
79
+ *
80
+ * @param username
81
+ */
82
+ remove(username: string): Promise<void>;
83
+ /**
84
+ * Write the contents of the auth file for a given username.
85
+ *
86
+ * @param username
87
+ */
88
+ write(username: string): Promise<Nullable<P>>;
89
+ protected init(): Promise<void>;
90
+ private getAllFiles;
91
+ private parseUsername;
92
+ private parseFilename;
93
+ protected abstract initAuthFile(username: string, throwOnNotFound?: boolean): Promise<T>;
94
+ protected abstract getFileRegex(): RegExp;
95
+ protected abstract getFileExtension(): string;
96
+ }
97
+ export declare class OrgAccessor extends BaseOrgAccessor<AuthInfoConfig, AuthFields> {
98
+ protected initAuthFile(username: string, throwOnNotFound?: boolean): Promise<AuthInfoConfig>;
99
+ protected getFileRegex(): RegExp;
100
+ protected getFileExtension(): string;
101
+ }
@@ -0,0 +1,240 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2021, 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
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.OrgAccessor = exports.BaseOrgAccessor = void 0;
10
+ const fs = require("fs");
11
+ const path = require("path");
12
+ const kit_1 = require("@salesforce/kit");
13
+ const authInfoConfig_1 = require("../../config/authInfoConfig");
14
+ const global_1 = require("../../global");
15
+ const logger_1 = require("../../logger");
16
+ const messages_1 = require("../../messages");
17
+ function chunk(array, chunkSize) {
18
+ const final = [];
19
+ for (let i = 0, len = array.length; i < len; i += chunkSize)
20
+ final.push(array.slice(i, i + chunkSize));
21
+ return final;
22
+ }
23
+ class BaseOrgAccessor extends kit_1.AsyncOptionalCreatable {
24
+ constructor() {
25
+ super(...arguments);
26
+ this.configs = new Map();
27
+ this.contents = new Map();
28
+ }
29
+ /**
30
+ * Read the auth file for the given username. Once the file has been read, it can be re-accessed with the `get` method.
31
+ *
32
+ * @param username username to read
33
+ * @param decrypt if true, decrypt encrypted values
34
+ * @param throwOnNotFound throw if file is not found for username
35
+ */
36
+ async read(username, decrypt = false, throwOnNotFound = true) {
37
+ try {
38
+ const config = await this.initAuthFile(username, throwOnNotFound);
39
+ this.configs.set(username, config);
40
+ return this.get(username, decrypt);
41
+ }
42
+ catch (err) {
43
+ return null;
44
+ }
45
+ }
46
+ /**
47
+ * Read all the auth files under the global state directory
48
+ *
49
+ * @param decrypt if true, decrypt encrypted values
50
+ */
51
+ async readAll(decrypt = false) {
52
+ const fileChunks = chunk(await this.getAllFiles(), 50);
53
+ for (const fileChunk of fileChunks) {
54
+ const promises = fileChunk.map(async (f) => {
55
+ const username = this.parseUsername(f);
56
+ const config = await this.initAuthFile(username);
57
+ this.configs.set(username, config);
58
+ });
59
+ // eslint-disable-next-line no-await-in-loop
60
+ await Promise.all(promises);
61
+ }
62
+ return this.getAll(decrypt);
63
+ }
64
+ /**
65
+ * Return the contents of the username's auth file from cache.
66
+ * The `read` or `readAll` methods must be called first in order to populate the cache.
67
+ * If throwOnNotFound is not true, an empty object {} is returned if the org is not found.
68
+ *
69
+ * @param username username to get
70
+ * @param decrypt if true, decrypt encrypted values
71
+ * @param throwOnNotFound if true, throw if the auth file does not already exist in the cache
72
+ */
73
+ get(username, decrypt = false, throwOnNotFound = false) {
74
+ const config = this.configs.get(username);
75
+ if (throwOnNotFound && config?.keys().length === 0) {
76
+ const messages = new messages_1.Messages('@salesforce/core', 'core', new Map([["authInfoCreationError", "Must pass a username and/or OAuth options when creating an AuthInfo instance."], ["authInfoOverwriteError", "Cannot create an AuthInfo instance that will overwrite existing auth data."], ["authInfoOverwriteError.actions", ["Create the AuthInfo instance using existing auth data by just passing the username. E.g., `AuthInfo.create({ username: 'my@user.org' });`."]], ["authCodeExchangeError", "Error authenticating with auth code due to: %s"], ["authCodeUsernameRetrievalError", "Could not retrieve the username after successful auth code exchange.\n\nDue to: %s"], ["jwtAuthError", "Error authenticating with JWT config due to: %s"], ["jwtAuthErrors", "Error authenticating with JWT.\nErrors encountered:\n%s"], ["refreshTokenAuthError", "Error authenticating with the refresh token due to: %s"], ["orgDataNotAvailableError", "An attempt to refresh the authentication token failed with a 'Data Not Found Error'. The org identified by username %s does not appear to exist. Likely cause is that the org was deleted by another user or has expired."], ["orgDataNotAvailableError.actions", ["Run `sfdx force:org:list --clean` to remove stale org authentications.", "Use `sfdx force:config:set` to update the defaultusername.", "Use `sfdx force:org:create` to create a new org.", "Use `sfdx auth` to authenticate an existing org."]], ["namedOrgNotFound", "No authorization information found for %s."], ["noAliasesFound", "Nothing to set."], ["invalidFormat", "Setting aliases must be in the format <key>=<value> but found: [%s]."], ["invalidJsonCasing", "All JSON input must have heads down camelcase keys. E.g., `{ sfdcLoginUrl: \"https://login.salesforce.com\" }`\nFound \"%s\" at %s"], ["missingClientId", "Client ID is required for JWT authentication."]]));
77
+ throw messages.createError('namedOrgNotFound', [username]);
78
+ }
79
+ if (config) {
80
+ this.contents.set(username, config.getContents(decrypt));
81
+ }
82
+ return this.contents.get(username);
83
+ }
84
+ /**
85
+ * Return the contents of all the auth files from cache. The `read` or `readAll` methods must be called first in order to populate the cache.
86
+ *
87
+ * @param decrypt if true, decrypt encrypted values
88
+ * @returns
89
+ */
90
+ getAll(decrypt = false) {
91
+ return [...this.configs.keys()].map((username) => this.get(username, decrypt)).filter((org) => !(0, kit_1.isEmpty)(org));
92
+ }
93
+ /**
94
+ * Returns true if the username has been cached.
95
+ *
96
+ * @param username
97
+ */
98
+ has(username) {
99
+ return this.contents.has(username);
100
+ }
101
+ /**
102
+ * Returns true if there is an auth file for the given username. The `read` or `readAll` methods must be called first in order to populate the cache.
103
+ *
104
+ * @param username
105
+ */
106
+ async exists(username) {
107
+ const config = this.configs.get(username);
108
+ return config ? config.exists() : false;
109
+ }
110
+ /**
111
+ * Return the file stats for a given userame's auth file.
112
+ *
113
+ * @param username
114
+ */
115
+ async stat(username) {
116
+ const config = this.configs.get(username);
117
+ return config ? config.stat() : null;
118
+ }
119
+ /**
120
+ * Returns true if there is an auth file for the given username
121
+ *
122
+ * @param username
123
+ */
124
+ async hasFile(username) {
125
+ try {
126
+ await fs.promises.access(this.parseFilename(username));
127
+ return true;
128
+ }
129
+ catch {
130
+ this.logger.debug(`No auth file found for ${username}`);
131
+ return false;
132
+ }
133
+ }
134
+ /**
135
+ * Return all auth files under the global state directory.
136
+ */
137
+ async list() {
138
+ return this.getAllFiles();
139
+ }
140
+ /**
141
+ * Set the contents for a given username.
142
+ *
143
+ * @param username
144
+ * @param org
145
+ */
146
+ set(username, org) {
147
+ const config = this.configs.get(username);
148
+ if (config) {
149
+ config.setContentsFromObject(org);
150
+ const contents = config.getContents();
151
+ contents.username ??= username;
152
+ this.contents.set(username, contents);
153
+ }
154
+ else {
155
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
156
+ // @ts-ignore
157
+ org.username ??= username;
158
+ this.contents.set(username, org);
159
+ }
160
+ }
161
+ /**
162
+ * Update the contents for a given username.
163
+ *
164
+ * @param username
165
+ * @param org
166
+ */
167
+ update(username, org) {
168
+ const existing = this.get(username) || {};
169
+ const merged = Object.assign({}, existing, org);
170
+ return this.set(username, merged);
171
+ }
172
+ /**
173
+ * Delete the auth file for a given username.
174
+ *
175
+ * @param username
176
+ */
177
+ async remove(username) {
178
+ await this.configs.get(username)?.unlink();
179
+ this.configs.delete(username);
180
+ this.contents.delete(username);
181
+ }
182
+ /**
183
+ * Write the contents of the auth file for a given username.
184
+ *
185
+ * @param username
186
+ */
187
+ async write(username) {
188
+ const config = this.configs.get(username);
189
+ if (config) {
190
+ return (await config.write());
191
+ }
192
+ else {
193
+ const contents = this.contents.get(username) ?? {};
194
+ await this.read(username, false, false);
195
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
196
+ const readConfig = this.configs.get(username);
197
+ readConfig.setContentsFromObject(contents);
198
+ return (await readConfig.write());
199
+ }
200
+ }
201
+ async init() {
202
+ this.logger = await logger_1.Logger.child(this.constructor.name);
203
+ }
204
+ async getAllFiles() {
205
+ const regex = this.getFileRegex();
206
+ try {
207
+ return (await fs.promises.readdir(global_1.Global.DIR)).filter((file) => regex.test(file));
208
+ }
209
+ catch {
210
+ return [];
211
+ }
212
+ }
213
+ parseUsername(filename) {
214
+ return filename.replace(this.getFileExtension(), '');
215
+ }
216
+ parseFilename(username) {
217
+ return path.join(global_1.Global.DIR, `${username}${this.getFileExtension()}`);
218
+ }
219
+ }
220
+ exports.BaseOrgAccessor = BaseOrgAccessor;
221
+ class OrgAccessor extends BaseOrgAccessor {
222
+ // eslint-disable-next-line class-methods-use-this
223
+ async initAuthFile(username, throwOnNotFound = false) {
224
+ return authInfoConfig_1.AuthInfoConfig.create({
225
+ ...authInfoConfig_1.AuthInfoConfig.getOptions(username),
226
+ throwOnNotFound,
227
+ });
228
+ }
229
+ // eslint-disable-next-line class-methods-use-this
230
+ getFileRegex() {
231
+ // The regular expression that filters files stored in $HOME/.sfdx
232
+ return /^[^.][^@]*@[^.]+(\.[^.\s]+)+\.json$/;
233
+ }
234
+ // eslint-disable-next-line class-methods-use-this
235
+ getFileExtension() {
236
+ return '.json';
237
+ }
238
+ }
239
+ exports.OrgAccessor = OrgAccessor;
240
+ //# sourceMappingURL=orgAccessor.js.map
@@ -0,0 +1,8 @@
1
+ import { SandboxOrgConfig } from '../../config/sandboxOrgConfig';
2
+ import { SandboxFields } from '../../org';
3
+ import { BaseOrgAccessor } from './orgAccessor';
4
+ export declare class SandboxAccessor extends BaseOrgAccessor<SandboxOrgConfig, SandboxFields> {
5
+ protected initAuthFile(username: string, throwOnNotFound?: boolean): Promise<SandboxOrgConfig>;
6
+ protected getFileRegex(): RegExp;
7
+ protected getFileExtension(): string;
8
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2021, 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
+ */
8
+ /* eslint-disable class-methods-use-this */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.SandboxAccessor = void 0;
11
+ const sandboxOrgConfig_1 = require("../../config/sandboxOrgConfig");
12
+ const orgAccessor_1 = require("./orgAccessor");
13
+ class SandboxAccessor extends orgAccessor_1.BaseOrgAccessor {
14
+ async initAuthFile(username, throwOnNotFound = false) {
15
+ return sandboxOrgConfig_1.SandboxOrgConfig.create({
16
+ ...sandboxOrgConfig_1.SandboxOrgConfig.getOptions(username),
17
+ throwOnNotFound,
18
+ });
19
+ }
20
+ getFileRegex() {
21
+ return /^(00D.*?)\.sandbox\.json$/;
22
+ }
23
+ getFileExtension() {
24
+ return '.sandbox.json';
25
+ }
26
+ }
27
+ exports.SandboxAccessor = SandboxAccessor;
28
+ //# sourceMappingURL=sandboxAccessor.js.map
@@ -0,0 +1,63 @@
1
+ import { AsyncOptionalCreatable } from '@salesforce/kit';
2
+ import { JsonMap, Optional } from '@salesforce/ts-types';
3
+ export type SfToken = {
4
+ token: string;
5
+ url: string;
6
+ user?: string;
7
+ timestamp: string;
8
+ } & JsonMap;
9
+ export type SfTokens = {
10
+ [key: string]: SfToken;
11
+ };
12
+ export declare class TokenAccessor extends AsyncOptionalCreatable {
13
+ private config;
14
+ /**
15
+ * Return all tokens.
16
+ *
17
+ * @param decrypt
18
+ * @returns {SfTokens}
19
+ */
20
+ getAll(decrypt?: boolean): SfTokens;
21
+ /**
22
+ * Return a token for the provided name.
23
+ *
24
+ * @param name
25
+ * @param decrypt
26
+ * @returns {Optional<SfToken>}
27
+ */
28
+ get(name: string, decrypt?: boolean): Optional<SfToken>;
29
+ /**
30
+ * Return true if a given name has a token associated with it.
31
+ *
32
+ * @param name
33
+ * @returns {boolean}
34
+ */
35
+ has(name: string): boolean;
36
+ /**
37
+ * Set the token for the provided name.
38
+ *
39
+ * @param name
40
+ * @param token
41
+ */
42
+ set(name: string, token: Partial<SfToken>): void;
43
+ /**
44
+ * Update the token for the provided name.
45
+ *
46
+ * @param name
47
+ * @param token
48
+ */
49
+ update(name: string, token: Partial<SfToken>): void;
50
+ /**
51
+ * Unet the token for the provided name.
52
+ *
53
+ * @param name
54
+ */
55
+ unset(name: string): void;
56
+ /**
57
+ * Write the contents to the token file.
58
+ *
59
+ * @returns {Promise<SfTokens>}
60
+ */
61
+ write(): Promise<SfTokens>;
62
+ protected init(): Promise<void>;
63
+ }