@salesforce/core 4.0.0 → 4.1.0

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 (151) hide show
  1. package/LICENSE.txt +1 -1
  2. package/README.md +93 -44
  3. package/lib/config/authInfoConfig.d.ts +19 -0
  4. package/lib/config/authInfoConfig.js +35 -0
  5. package/lib/config/config.d.ts +87 -22
  6. package/lib/config/config.js +117 -65
  7. package/lib/config/configAggregator.d.ts +41 -35
  8. package/lib/config/configAggregator.js +102 -73
  9. package/lib/config/configFile.d.ts +2 -2
  10. package/lib/config/configFile.js +38 -29
  11. package/lib/config/configStore.d.ts +9 -9
  12. package/lib/config/configStore.js +17 -15
  13. package/lib/config/envVars.d.ts +15 -9
  14. package/lib/config/envVars.js +71 -47
  15. package/lib/config/orgUsersConfig.js +2 -0
  16. package/lib/config/sandboxOrgConfig.js +2 -0
  17. package/lib/config/sandboxProcessCache.d.ts +16 -0
  18. package/lib/config/sandboxProcessCache.js +38 -0
  19. package/lib/config/tokensConfig.d.ts +10 -0
  20. package/lib/config/tokensConfig.js +29 -0
  21. package/lib/config/ttlConfig.d.ts +34 -0
  22. package/lib/config/ttlConfig.js +50 -0
  23. package/lib/crypto/crypto.js +15 -22
  24. package/lib/crypto/keyChain.js +2 -3
  25. package/lib/crypto/keyChainImpl.d.ts +5 -3
  26. package/lib/crypto/keyChainImpl.js +58 -61
  27. package/lib/crypto/secureBuffer.d.ts +1 -1
  28. package/lib/deviceOauthService.d.ts +3 -3
  29. package/lib/deviceOauthService.js +27 -25
  30. package/lib/exported.d.ts +15 -12
  31. package/lib/exported.js +28 -16
  32. package/lib/global.d.ts +11 -3
  33. package/lib/global.js +39 -12
  34. package/lib/lifecycleEvents.d.ts +1 -1
  35. package/lib/lifecycleEvents.js +3 -0
  36. package/lib/logger.d.ts +19 -9
  37. package/lib/logger.js +112 -86
  38. package/lib/messages.d.ts +53 -36
  39. package/lib/messages.js +81 -91
  40. package/lib/org/authInfo.d.ts +56 -20
  41. package/lib/org/authInfo.js +232 -131
  42. package/lib/org/authRemover.d.ts +8 -7
  43. package/lib/org/authRemover.js +32 -28
  44. package/lib/org/connection.d.ts +13 -37
  45. package/lib/org/connection.js +78 -124
  46. package/lib/org/index.js +5 -1
  47. package/lib/org/org.d.ts +151 -48
  48. package/lib/org/org.js +468 -225
  49. package/lib/org/orgConfigProperties.d.ts +64 -3
  50. package/lib/org/orgConfigProperties.js +96 -4
  51. package/lib/org/permissionSetAssignment.js +4 -13
  52. package/lib/org/scratchOrgCache.d.ts +20 -0
  53. package/lib/org/scratchOrgCache.js +33 -0
  54. package/lib/org/scratchOrgCreate.d.ts +28 -17
  55. package/lib/org/scratchOrgCreate.js +125 -53
  56. package/lib/org/scratchOrgErrorCodes.d.ts +9 -3
  57. package/lib/org/scratchOrgErrorCodes.js +34 -17
  58. package/lib/org/scratchOrgFeatureDeprecation.js +1 -6
  59. package/lib/org/scratchOrgInfoApi.d.ts +21 -47
  60. package/lib/org/scratchOrgInfoApi.js +129 -63
  61. package/lib/org/scratchOrgInfoGenerator.d.ts +6 -5
  62. package/lib/org/scratchOrgInfoGenerator.js +76 -62
  63. package/lib/org/scratchOrgLifecycleEvents.d.ts +10 -0
  64. package/lib/org/scratchOrgLifecycleEvents.js +41 -0
  65. package/lib/org/scratchOrgSettingsGenerator.d.ts +44 -21
  66. package/lib/org/scratchOrgSettingsGenerator.js +165 -98
  67. package/lib/org/scratchOrgTypes.d.ts +43 -0
  68. package/lib/org/scratchOrgTypes.js +9 -0
  69. package/lib/org/user.d.ts +1 -1
  70. package/lib/org/user.js +25 -34
  71. package/lib/schema/printer.d.ts +6 -0
  72. package/lib/schema/printer.js +34 -31
  73. package/lib/schema/validator.d.ts +12 -10
  74. package/lib/schema/validator.js +56 -76
  75. package/lib/{sfdxError.d.ts → sfError.d.ts} +12 -20
  76. package/lib/{sfdxError.js → sfError.js} +40 -30
  77. package/lib/{sfdxProject.d.ts → sfProject.d.ts} +75 -35
  78. package/lib/sfProject.js +651 -0
  79. package/lib/stateAggregator/accessors/aliasAccessor.d.ts +129 -0
  80. package/lib/stateAggregator/accessors/aliasAccessor.js +263 -0
  81. package/lib/stateAggregator/accessors/orgAccessor.d.ts +101 -0
  82. package/lib/stateAggregator/accessors/orgAccessor.js +240 -0
  83. package/lib/stateAggregator/accessors/sandboxAccessor.d.ts +8 -0
  84. package/lib/stateAggregator/accessors/sandboxAccessor.js +28 -0
  85. package/lib/stateAggregator/accessors/tokenAccessor.d.ts +63 -0
  86. package/lib/stateAggregator/accessors/tokenAccessor.js +80 -0
  87. package/lib/stateAggregator/index.d.ts +4 -0
  88. package/lib/stateAggregator/index.js +27 -0
  89. package/lib/stateAggregator/stateAggregator.d.ts +25 -0
  90. package/lib/stateAggregator/stateAggregator.js +46 -0
  91. package/lib/status/myDomainResolver.d.ts +1 -1
  92. package/lib/status/myDomainResolver.js +4 -4
  93. package/lib/status/pollingClient.js +4 -4
  94. package/lib/status/streamingClient.d.ts +2 -2
  95. package/lib/status/streamingClient.js +58 -63
  96. package/lib/status/types.d.ts +2 -2
  97. package/lib/testSetup.d.ts +204 -75
  98. package/lib/testSetup.js +468 -164
  99. package/lib/util/cache.d.ts +2 -2
  100. package/lib/util/cache.js +6 -6
  101. package/lib/util/checkLightningDomain.js +3 -4
  102. package/lib/util/directoryWriter.d.ts +12 -0
  103. package/lib/util/directoryWriter.js +54 -0
  104. package/lib/util/getJwtAudienceUrl.js +1 -1
  105. package/lib/util/internal.d.ts +28 -2
  106. package/lib/util/internal.js +65 -8
  107. package/lib/util/jsonXmlTools.js +2 -4
  108. package/lib/util/mapKeys.d.ts +9 -9
  109. package/lib/util/mapKeys.js +13 -9
  110. package/lib/util/sfdc.d.ts +51 -51
  111. package/lib/util/sfdc.js +74 -79
  112. package/lib/util/sfdcUrl.d.ts +5 -19
  113. package/lib/util/sfdcUrl.js +40 -49
  114. package/lib/util/structuredWriter.d.ts +9 -0
  115. package/lib/util/structuredWriter.js +3 -0
  116. package/lib/util/zipWriter.d.ts +8 -6
  117. package/lib/util/zipWriter.js +13 -13
  118. package/lib/webOAuthServer.d.ts +20 -6
  119. package/lib/webOAuthServer.js +102 -56
  120. package/messageTransformer/messageTransformer.ts +93 -0
  121. package/messages/auth.md +9 -1
  122. package/messages/config.md +42 -6
  123. package/messages/connection.md +8 -0
  124. package/messages/core.md +10 -0
  125. package/messages/envVars.md +37 -3
  126. package/messages/org.md +21 -1
  127. package/messages/scratchOrgCreate.md +2 -6
  128. package/messages/scratchOrgErrorCodes.md +17 -1
  129. package/messages/scratchOrgInfoApi.md +9 -0
  130. package/messages/scratchOrgInfoGenerator.md +9 -1
  131. package/package.json +123 -46
  132. package/CHANGELOG.md +0 -1244
  133. package/lib/config/keychainConfig.d.ts +0 -19
  134. package/lib/config/keychainConfig.js +0 -43
  135. package/lib/globalInfo/accessors/aliasAccessor.d.ts +0 -83
  136. package/lib/globalInfo/accessors/aliasAccessor.js +0 -130
  137. package/lib/globalInfo/accessors/orgAccessor.d.ts +0 -13
  138. package/lib/globalInfo/accessors/orgAccessor.js +0 -45
  139. package/lib/globalInfo/accessors/tokenAccessor.d.ts +0 -13
  140. package/lib/globalInfo/accessors/tokenAccessor.js +0 -35
  141. package/lib/globalInfo/globalInfoConfig.d.ts +0 -36
  142. package/lib/globalInfo/globalInfoConfig.js +0 -105
  143. package/lib/globalInfo/index.d.ts +0 -6
  144. package/lib/globalInfo/index.js +0 -29
  145. package/lib/globalInfo/sfdxDataHandler.d.ts +0 -43
  146. package/lib/globalInfo/sfdxDataHandler.js +0 -217
  147. package/lib/globalInfo/types.d.ts +0 -39
  148. package/lib/globalInfo/types.js +0 -10
  149. package/lib/sfdxProject.js +0 -557
  150. package/lib/util/fs.d.ts +0 -201
  151. package/lib/util/fs.js +0 -378
package/lib/util/fs.d.ts DELETED
@@ -1,201 +0,0 @@
1
- /// <reference types="node" />
2
- import { AnyJson, JsonMap, Optional } from '@salesforce/ts-types';
3
- import * as fsLib from 'graceful-fs';
4
- import * as mkdirpLib from 'mkdirp';
5
- declare type PerformFunction = (filePath: string, file?: string, dir?: string) => Promise<void>;
6
- declare type PerformFunctionSync = (filePath: string, file?: string, dir?: string) => void;
7
- export declare type WriteJsonOptions = {
8
- /**
9
- * The number of indent spaces
10
- */
11
- space?: number;
12
- };
13
- /**
14
- * @deprecated Use fs/promises instead
15
- */
16
- export declare const fs: typeof fsLib & {
17
- /**
18
- * The default file system mode to use when creating directories.
19
- */
20
- DEFAULT_USER_DIR_MODE: string;
21
- /**
22
- * The default file system mode to use when creating files.
23
- */
24
- DEFAULT_USER_FILE_MODE: string;
25
- /**
26
- * A convenience reference to {@link https://nodejs.org/api/fsLib.html#fs_fs_constants}
27
- * to reduce the need to import multiple `fs` modules.
28
- */
29
- constants: typeof fsLib.constants;
30
- /**
31
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_readfile_path_options_callback|fsLib.readFile}.
32
- */
33
- readFile: typeof fsLib.readFile.__promisify__;
34
- /**
35
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_readdir_path_options_callback|fsLib.readdir}.
36
- */
37
- readdir: typeof fsLib.readdir.__promisify__;
38
- /**
39
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_writefile_file_data_options_callback|fsLib.writeFile}.
40
- */
41
- writeFile: typeof fsLib.writeFile.__promisify__;
42
- /**
43
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_access_path_mode_callback|fsLib.access}.
44
- */
45
- access: typeof fsLib.access.__promisify__;
46
- /**
47
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_open_path_flags_mode_callback|fsLib.open}.
48
- */
49
- open: typeof fsLib.open.__promisify__;
50
- /**
51
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_unlink_path_callback|fsLib.unlink}.
52
- */
53
- unlink: typeof fsLib.unlink.__promisify__;
54
- /**
55
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_readdir_path_options_callback|fsLib.rmdir}.
56
- */
57
- rmdir: typeof fsLib.rmdir.__promisify__;
58
- /**
59
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_fstat_fd_callback|fsLib.stat}.
60
- */
61
- stat: typeof fsLib.stat.__promisify__;
62
- /**
63
- * Promisified version of {@link https://npmjs.com/package/mkdirp|mkdirp}.
64
- */
65
- mkdirp: (folderPath: string, mode?: string | object | undefined) => Promise<string | undefined>;
66
- mkdirpSync: typeof mkdirpLib.sync;
67
- /**
68
- * Deletes a folder recursively, removing all descending files and folders.
69
- *
70
- * **Throws** *PathIsNullOrUndefined* The path is not defined.
71
- * **Throws** *DirMissingOrNoAccess* The folder or any sub-folder is missing or has no access.
72
- *
73
- * @param {string} dirPath The path to remove.
74
- */
75
- remove: (dirPath: string) => Promise<void>;
76
- /**
77
- * Deletes a folder recursively, removing all descending files and folders.
78
- *
79
- * NOTE: It is recommended to call the asynchronous `remove` when possible as it will remove all files in parallel rather than serially.
80
- *
81
- * **Throws** *PathIsNullOrUndefined* The path is not defined.
82
- * **Throws** *DirMissingOrNoAccess* The folder or any sub-folder is missing or has no access.
83
- *
84
- * @param {string} dirPath The path to remove.
85
- */
86
- removeSync: (dirPath: string) => void;
87
- /**
88
- * Searches a file path in an ascending manner (until reaching the filesystem root) for the first occurrence a
89
- * specific file name. Resolves with the directory path containing the located file, or `null` if the file was
90
- * not found.
91
- *
92
- * @param dir The directory path in which to start the upward search.
93
- * @param file The file name to look for.
94
- */
95
- traverseForFile: (dir: string, file: string) => Promise<Optional<string>>;
96
- /**
97
- * Searches a file path synchronously in an ascending manner (until reaching the filesystem root) for the first occurrence a
98
- * specific file name. Resolves with the directory path containing the located file, or `null` if the file was
99
- * not found.
100
- *
101
- * @param dir The directory path in which to start the upward search.
102
- * @param file The file name to look for.
103
- */
104
- traverseForFileSync: (dir: string, file: string) => Optional<string>;
105
- /**
106
- * Read a file and convert it to JSON. Returns the contents of the file as a JSON object
107
- *
108
- * @param jsonPath The path of the file.
109
- * @param throwOnEmpty Whether to throw an error if the JSON file is empty.
110
- */
111
- readJson: (jsonPath: string, throwOnEmpty?: boolean | undefined) => Promise<AnyJson>;
112
- /**
113
- * Read a file and convert it to JSON. Returns the contents of the file as a JSON object
114
- *
115
- * @param jsonPath The path of the file.
116
- * @param throwOnEmpty Whether to throw an error if the JSON file is empty.
117
- */
118
- readJsonSync: (jsonPath: string, throwOnEmpty?: boolean | undefined) => AnyJson;
119
- /**
120
- * Read a file and convert it to JSON, throwing an error if the parsed contents are not a `JsonMap`.
121
- *
122
- * @param jsonPath The path of the file.
123
- * @param throwOnEmpty Whether to throw an error if the JSON file is empty.
124
- */
125
- readJsonMap: <T extends JsonMap = JsonMap>(jsonPath: string, throwOnEmpty?: boolean | undefined) => Promise<T>;
126
- /**
127
- * Read a file and convert it to JSON, throwing an error if the parsed contents are not a `JsonMap`.
128
- *
129
- * @param jsonPath The path of the file.
130
- * @param throwOnEmpty Whether to throw an error if the JSON file is empty.
131
- */
132
- readJsonMapSync: <T_1 extends JsonMap = JsonMap>(jsonPath: string, throwOnEmpty?: boolean | undefined) => T_1;
133
- /**
134
- * Convert a JSON-compatible object to a `string` and write it to a file.
135
- *
136
- * @param jsonPath The path of the file to write.
137
- * @param data The JSON object to write.
138
- */
139
- writeJson: (jsonPath: string, data: AnyJson, options?: WriteJsonOptions) => Promise<void>;
140
- /**
141
- * Convert a JSON-compatible object to a `string` and write it to a file.
142
- *
143
- * @param jsonPath The path of the file to write.
144
- * @param data The JSON object to write.
145
- */
146
- writeJsonSync: (jsonPath: string, data: AnyJson, options?: WriteJsonOptions) => void;
147
- /**
148
- * Checks if a file path exists
149
- *
150
- * @param filePath the file path to check the existence of
151
- */
152
- fileExists: (filePath: string) => Promise<boolean>;
153
- /**
154
- * Checks if a file path exists
155
- *
156
- * @param filePath the file path to check the existence of
157
- */
158
- fileExistsSync: (filePath: string) => boolean;
159
- /**
160
- * Recursively act on all files or directories in a directory
161
- *
162
- * @param dir path to directory
163
- * @param perform function to be run on contents of dir
164
- * @param onType optional parameter to specify type to actOn
165
- * @returns void
166
- */
167
- actOn: (dir: string, perform: PerformFunction, onType?: 'file' | 'dir' | 'all') => Promise<void>;
168
- /**
169
- * Recursively act on all files or directories in a directory
170
- *
171
- * @param dir path to directory
172
- * @param perform function to be run on contents of dir
173
- * @param onType optional parameter to specify type to actOn
174
- * @returns void
175
- */
176
- actOnSync: (dir: string, perform: PerformFunctionSync, onType?: 'file' | 'dir' | 'all') => void;
177
- /**
178
- * Checks if files are the same
179
- *
180
- * @param file1Path the first file path to check
181
- * @param file2Path the second file path to check
182
- * @returns boolean
183
- */
184
- areFilesEqual: (file1Path: string, file2Path: string) => Promise<boolean>;
185
- /**
186
- * Checks if files are the same
187
- *
188
- * @param file1Path the first file path to check
189
- * @param file2Path the second file path to check
190
- * @returns boolean
191
- */
192
- areFilesEqualSync: (file1Path: string, file2Path: string) => boolean;
193
- /**
194
- * Creates a hash for the string that's passed in
195
- *
196
- * @param contents The string passed into the function
197
- * @returns string
198
- */
199
- getContentHash(contents: string | Buffer): string;
200
- };
201
- export {};
package/lib/util/fs.js DELETED
@@ -1,378 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2020, 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.fs = void 0;
10
- const crypto = require("crypto");
11
- const path = require("path");
12
- const util_1 = require("util");
13
- const kit_1 = require("@salesforce/kit");
14
- const fsLib = require("graceful-fs");
15
- const mkdirpLib = require("mkdirp");
16
- const sfdxError_1 = require("../sfdxError");
17
- /**
18
- * @deprecated Use fs/promises instead
19
- */
20
- exports.fs = Object.assign({}, fsLib, {
21
- /**
22
- * The default file system mode to use when creating directories.
23
- */
24
- DEFAULT_USER_DIR_MODE: '700',
25
- /**
26
- * The default file system mode to use when creating files.
27
- */
28
- DEFAULT_USER_FILE_MODE: '600',
29
- /**
30
- * A convenience reference to {@link https://nodejs.org/api/fsLib.html#fs_fs_constants}
31
- * to reduce the need to import multiple `fs` modules.
32
- */
33
- constants: fsLib.constants,
34
- /**
35
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_readfile_path_options_callback|fsLib.readFile}.
36
- */
37
- readFile: (0, util_1.promisify)(fsLib.readFile),
38
- /**
39
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_readdir_path_options_callback|fsLib.readdir}.
40
- */
41
- readdir: (0, util_1.promisify)(fsLib.readdir),
42
- /**
43
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_writefile_file_data_options_callback|fsLib.writeFile}.
44
- */
45
- writeFile: (0, util_1.promisify)(fsLib.writeFile),
46
- /**
47
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_access_path_mode_callback|fsLib.access}.
48
- */
49
- access: (0, util_1.promisify)(fsLib.access),
50
- /**
51
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_open_path_flags_mode_callback|fsLib.open}.
52
- */
53
- open: (0, util_1.promisify)(fsLib.open),
54
- /**
55
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_unlink_path_callback|fsLib.unlink}.
56
- */
57
- unlink: (0, util_1.promisify)(fsLib.unlink),
58
- /**
59
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_readdir_path_options_callback|fsLib.rmdir}.
60
- */
61
- rmdir: (0, util_1.promisify)(fsLib.rmdir),
62
- /**
63
- * Promisified version of {@link https://nodejs.org/api/fsLib.html#fs_fs_fstat_fd_callback|fsLib.stat}.
64
- */
65
- stat: (0, util_1.promisify)(fsLib.stat),
66
- /**
67
- * Promisified version of {@link https://npmjs.com/package/mkdirp|mkdirp}.
68
- */
69
- // eslint-disable-next-line @typescript-eslint/ban-types
70
- mkdirp: (folderPath, mode) => mkdirpLib(folderPath, mode),
71
- mkdirpSync: mkdirpLib.sync,
72
- /**
73
- * Deletes a folder recursively, removing all descending files and folders.
74
- *
75
- * **Throws** *PathIsNullOrUndefined* The path is not defined.
76
- * **Throws** *DirMissingOrNoAccess* The folder or any sub-folder is missing or has no access.
77
- *
78
- * @param {string} dirPath The path to remove.
79
- */
80
- remove: async (dirPath) => {
81
- if (!dirPath) {
82
- throw new sfdxError_1.SfdxError('Path is null or undefined.', 'PathIsNullOrUndefined');
83
- }
84
- try {
85
- await exports.fs.access(dirPath, fsLib.constants.R_OK);
86
- }
87
- catch (err) {
88
- throw new sfdxError_1.SfdxError(`The path: ${dirPath} doesn't exist or access is denied.`, 'DirMissingOrNoAccess');
89
- }
90
- const files = await exports.fs.readdir(dirPath);
91
- const stats = await Promise.all(files.map((file) => exports.fs.stat(path.join(dirPath, file))));
92
- const metas = stats.map((value, index) => Object.assign(value, { path: path.join(dirPath, files[index]) }));
93
- await Promise.all(metas.map((meta) => (meta.isDirectory() ? exports.fs.remove(meta.path) : exports.fs.unlink(meta.path))));
94
- await exports.fs.rmdir(dirPath);
95
- },
96
- /**
97
- * Deletes a folder recursively, removing all descending files and folders.
98
- *
99
- * NOTE: It is recommended to call the asynchronous `remove` when possible as it will remove all files in parallel rather than serially.
100
- *
101
- * **Throws** *PathIsNullOrUndefined* The path is not defined.
102
- * **Throws** *DirMissingOrNoAccess* The folder or any sub-folder is missing or has no access.
103
- *
104
- * @param {string} dirPath The path to remove.
105
- */
106
- removeSync: (dirPath) => {
107
- if (!dirPath) {
108
- throw new sfdxError_1.SfdxError('Path is null or undefined.', 'PathIsNullOrUndefined');
109
- }
110
- try {
111
- exports.fs.accessSync(dirPath, fsLib.constants.R_OK);
112
- }
113
- catch (err) {
114
- throw new sfdxError_1.SfdxError(`The path: ${dirPath} doesn't exist or access is denied.`, 'DirMissingOrNoAccess');
115
- }
116
- exports.fs.actOnSync(dirPath, (fullPath, file) => {
117
- if (file) {
118
- exports.fs.unlinkSync(fullPath);
119
- }
120
- else {
121
- // All files in this directory will be acted on before the directory.
122
- exports.fs.rmdirSync(fullPath);
123
- }
124
- }, 'all');
125
- // Remove the top level
126
- exports.fs.rmdirSync(dirPath);
127
- },
128
- /**
129
- * Searches a file path in an ascending manner (until reaching the filesystem root) for the first occurrence a
130
- * specific file name. Resolves with the directory path containing the located file, or `null` if the file was
131
- * not found.
132
- *
133
- * @param dir The directory path in which to start the upward search.
134
- * @param file The file name to look for.
135
- */
136
- traverseForFile: async (dir, file) => {
137
- let foundProjectDir;
138
- try {
139
- await exports.fs.stat(path.join(dir, file));
140
- foundProjectDir = dir;
141
- }
142
- catch (err) {
143
- if (err && err.code === 'ENOENT') {
144
- const nextDir = path.resolve(dir, '..');
145
- if (nextDir !== dir) {
146
- // stop at root
147
- foundProjectDir = await exports.fs.traverseForFile(nextDir, file);
148
- }
149
- }
150
- }
151
- return foundProjectDir;
152
- },
153
- /**
154
- * Searches a file path synchronously in an ascending manner (until reaching the filesystem root) for the first occurrence a
155
- * specific file name. Resolves with the directory path containing the located file, or `null` if the file was
156
- * not found.
157
- *
158
- * @param dir The directory path in which to start the upward search.
159
- * @param file The file name to look for.
160
- */
161
- traverseForFileSync: (dir, file) => {
162
- let foundProjectDir;
163
- try {
164
- exports.fs.statSync(path.join(dir, file));
165
- foundProjectDir = dir;
166
- }
167
- catch (err) {
168
- if (err && err.code === 'ENOENT') {
169
- const nextDir = path.resolve(dir, '..');
170
- if (nextDir !== dir) {
171
- // stop at root
172
- foundProjectDir = exports.fs.traverseForFileSync(nextDir, file);
173
- }
174
- }
175
- }
176
- return foundProjectDir;
177
- },
178
- /**
179
- * Read a file and convert it to JSON. Returns the contents of the file as a JSON object
180
- *
181
- * @param jsonPath The path of the file.
182
- * @param throwOnEmpty Whether to throw an error if the JSON file is empty.
183
- */
184
- readJson: async (jsonPath, throwOnEmpty) => {
185
- const fileData = await exports.fs.readFile(jsonPath, 'utf8');
186
- return (0, kit_1.parseJson)(fileData, jsonPath, throwOnEmpty);
187
- },
188
- /**
189
- * Read a file and convert it to JSON. Returns the contents of the file as a JSON object
190
- *
191
- * @param jsonPath The path of the file.
192
- * @param throwOnEmpty Whether to throw an error if the JSON file is empty.
193
- */
194
- readJsonSync: (jsonPath, throwOnEmpty) => {
195
- const fileData = exports.fs.readFileSync(jsonPath, 'utf8');
196
- return (0, kit_1.parseJson)(fileData, jsonPath, throwOnEmpty);
197
- },
198
- /**
199
- * Read a file and convert it to JSON, throwing an error if the parsed contents are not a `JsonMap`.
200
- *
201
- * @param jsonPath The path of the file.
202
- * @param throwOnEmpty Whether to throw an error if the JSON file is empty.
203
- */
204
- readJsonMap: async (jsonPath, throwOnEmpty) => {
205
- const fileData = await exports.fs.readFile(jsonPath, 'utf8');
206
- return (0, kit_1.parseJsonMap)(fileData, jsonPath, throwOnEmpty);
207
- },
208
- /**
209
- * Read a file and convert it to JSON, throwing an error if the parsed contents are not a `JsonMap`.
210
- *
211
- * @param jsonPath The path of the file.
212
- * @param throwOnEmpty Whether to throw an error if the JSON file is empty.
213
- */
214
- readJsonMapSync: (jsonPath, throwOnEmpty) => {
215
- const fileData = exports.fs.readFileSync(jsonPath, 'utf8');
216
- return (0, kit_1.parseJsonMap)(fileData, jsonPath, throwOnEmpty);
217
- },
218
- /**
219
- * Convert a JSON-compatible object to a `string` and write it to a file.
220
- *
221
- * @param jsonPath The path of the file to write.
222
- * @param data The JSON object to write.
223
- */
224
- writeJson: async (jsonPath, data, options = {}) => {
225
- options = Object.assign({ space: 2 }, options);
226
- const fileData = JSON.stringify(data, null, options.space);
227
- await exports.fs.writeFile(jsonPath, fileData, {
228
- encoding: 'utf8',
229
- mode: exports.fs.DEFAULT_USER_FILE_MODE,
230
- });
231
- },
232
- /**
233
- * Convert a JSON-compatible object to a `string` and write it to a file.
234
- *
235
- * @param jsonPath The path of the file to write.
236
- * @param data The JSON object to write.
237
- */
238
- writeJsonSync: (jsonPath, data, options = {}) => {
239
- options = Object.assign({ space: 2 }, options);
240
- const fileData = JSON.stringify(data, null, options.space);
241
- exports.fs.writeFileSync(jsonPath, fileData, {
242
- encoding: 'utf8',
243
- mode: exports.fs.DEFAULT_USER_FILE_MODE,
244
- });
245
- },
246
- /**
247
- * Checks if a file path exists
248
- *
249
- * @param filePath the file path to check the existence of
250
- */
251
- fileExists: async (filePath) => {
252
- try {
253
- await exports.fs.access(filePath);
254
- return true;
255
- }
256
- catch (err) {
257
- return false;
258
- }
259
- },
260
- /**
261
- * Checks if a file path exists
262
- *
263
- * @param filePath the file path to check the existence of
264
- */
265
- fileExistsSync: (filePath) => {
266
- try {
267
- exports.fs.accessSync(filePath);
268
- return true;
269
- }
270
- catch (err) {
271
- return false;
272
- }
273
- },
274
- /**
275
- * Recursively act on all files or directories in a directory
276
- *
277
- * @param dir path to directory
278
- * @param perform function to be run on contents of dir
279
- * @param onType optional parameter to specify type to actOn
280
- * @returns void
281
- */
282
- actOn: async (dir, perform, onType = 'file') => {
283
- for (const file of await exports.fs.readdir(dir)) {
284
- const filePath = path.join(dir, file);
285
- const stat = await exports.fs.stat(filePath);
286
- if (stat) {
287
- if (stat.isDirectory()) {
288
- await exports.fs.actOn(filePath, perform, onType);
289
- if (onType === 'dir' || onType === 'all') {
290
- await perform(filePath);
291
- }
292
- }
293
- else if (stat.isFile() && (onType === 'file' || onType === 'all')) {
294
- await perform(filePath, file, dir);
295
- }
296
- }
297
- }
298
- },
299
- /**
300
- * Recursively act on all files or directories in a directory
301
- *
302
- * @param dir path to directory
303
- * @param perform function to be run on contents of dir
304
- * @param onType optional parameter to specify type to actOn
305
- * @returns void
306
- */
307
- actOnSync: (dir, perform, onType = 'file') => {
308
- for (const file of exports.fs.readdirSync(dir)) {
309
- const filePath = path.join(dir, file);
310
- const stat = exports.fs.statSync(filePath);
311
- if (stat) {
312
- if (stat.isDirectory()) {
313
- exports.fs.actOnSync(filePath, perform, onType);
314
- if (onType === 'dir' || onType === 'all') {
315
- perform(filePath);
316
- }
317
- }
318
- else if (stat.isFile() && (onType === 'file' || onType === 'all')) {
319
- perform(filePath, file, dir);
320
- }
321
- }
322
- }
323
- },
324
- /**
325
- * Checks if files are the same
326
- *
327
- * @param file1Path the first file path to check
328
- * @param file2Path the second file path to check
329
- * @returns boolean
330
- */
331
- areFilesEqual: async (file1Path, file2Path) => {
332
- try {
333
- const file1Size = (await exports.fs.stat(file1Path)).size;
334
- const file2Size = (await exports.fs.stat(file2Path)).size;
335
- if (file1Size !== file2Size) {
336
- return false;
337
- }
338
- const contentA = await exports.fs.readFile(file1Path);
339
- const contentB = await exports.fs.readFile(file2Path);
340
- return exports.fs.getContentHash(contentA) === exports.fs.getContentHash(contentB);
341
- }
342
- catch (err) {
343
- throw new sfdxError_1.SfdxError(`The path: ${err.path} doesn't exist or access is denied.`, 'DirMissingOrNoAccess');
344
- }
345
- },
346
- /**
347
- * Checks if files are the same
348
- *
349
- * @param file1Path the first file path to check
350
- * @param file2Path the second file path to check
351
- * @returns boolean
352
- */
353
- areFilesEqualSync: (file1Path, file2Path) => {
354
- try {
355
- const file1Size = exports.fs.statSync(file1Path).size;
356
- const file2Size = exports.fs.statSync(file2Path).size;
357
- if (file1Size !== file2Size) {
358
- return false;
359
- }
360
- const contentA = exports.fs.readFileSync(file1Path);
361
- const contentB = exports.fs.readFileSync(file2Path);
362
- return exports.fs.getContentHash(contentA) === exports.fs.getContentHash(contentB);
363
- }
364
- catch (err) {
365
- throw new sfdxError_1.SfdxError(`The path: ${err.path} doesn't exist or access is denied.`, 'DirMissingOrNoAccess');
366
- }
367
- },
368
- /**
369
- * Creates a hash for the string that's passed in
370
- *
371
- * @param contents The string passed into the function
372
- * @returns string
373
- */
374
- getContentHash(contents) {
375
- return crypto.createHash('sha1').update(contents).digest('hex');
376
- },
377
- });
378
- //# sourceMappingURL=fs.js.map