@hubspot/local-dev-lib 3.21.2 → 4.0.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.
- package/config/defaultAccountOverride.d.ts +2 -0
- package/config/defaultAccountOverride.js +57 -0
- package/config/index.d.ts +30 -44
- package/config/index.js +332 -267
- package/config/migrate.d.ts +12 -16
- package/config/migrate.js +86 -160
- package/config/utils.d.ts +30 -0
- package/config/utils.js +373 -0
- package/constants/config.d.ts +40 -0
- package/constants/config.js +41 -1
- package/constants/environments.d.ts +0 -11
- package/constants/environments.js +1 -12
- package/errors/index.d.ts +2 -0
- package/errors/index.js +6 -1
- package/http/getAxiosConfig.js +7 -1
- package/http/index.js +23 -19
- package/lang/en.json +89 -61
- package/lib/cms/themes.js +3 -1
- package/lib/environment.d.ts +1 -1
- package/lib/oauth.d.ts +2 -2
- package/lib/oauth.js +8 -16
- package/lib/personalAccessKey.d.ts +2 -2
- package/lib/personalAccessKey.js +43 -30
- package/lib/trackUsage.js +6 -3
- package/models/HubSpotConfigError.d.ts +6 -0
- package/models/HubSpotConfigError.js +34 -0
- package/models/OAuth2Manager.d.ts +3 -4
- package/models/OAuth2Manager.js +20 -29
- package/package.json +5 -5
- package/types/Accounts.d.ts +20 -109
- package/types/Config.d.ts +17 -26
- package/config/CLIConfiguration.d.ts +0 -72
- package/config/CLIConfiguration.js +0 -551
- package/config/configFile.d.ts +0 -21
- package/config/configFile.js +0 -100
- package/config/configUtils.d.ts +0 -5
- package/config/configUtils.js +0 -87
- package/config/config_DEPRECATED.d.ts +0 -87
- package/config/config_DEPRECATED.js +0 -738
- package/config/environment.d.ts +0 -2
- package/config/environment.js +0 -60
- package/config/getAccountIdentifier.d.ts +0 -2
- package/config/getAccountIdentifier.js +0 -15
- package/utils/accounts.d.ts +0 -4
- package/utils/accounts.js +0 -28
|
@@ -1,738 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.removeLocalStateFlag = exports.addLocalStateFlag = exports.hasLocalStateFlag = exports.isConfigFlagEnabled = exports.loadConfigFromEnvironment = exports.deleteConfigFile = exports.deleteEmptyConfigFile = exports.createEmptyConfigFile = exports.deleteAccount = exports.renameAccount = exports.updateAutoOpenBrowser = exports.updateAllowUsageTracking = exports.updateAllowAutoUpdates = exports.updateHttpTimeout = exports.updateDefaultCmsPublishMode = exports.updateDefaultAccount = exports.updateAccountConfig = exports.removeSandboxAccountFromConfig = exports.getAccountId = exports.getAccountConfig = exports.getAccountType = exports.getEnv = exports.findConfig = exports.getAndLoadConfigIfNeeded = exports.isTrackingAllowed = exports.loadConfig = exports.writeConfig = exports.getOrderedConfig = exports.getOrderedAccount = exports.accountNameExistsInConfig = exports.validateConfig = exports.getConfigPath = exports.setConfigPath = exports.getConfigAccountId = exports.getConfigDefaultAccount = exports.getConfigAccounts = exports.setConfig = exports.getConfig = void 0;
|
|
7
|
-
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
-
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
9
|
-
const findup_sync_1 = __importDefault(require("findup-sync"));
|
|
10
|
-
const path_1 = require("../lib/path");
|
|
11
|
-
const config_1 = require("../constants/config");
|
|
12
|
-
const environments_1 = require("../constants/environments");
|
|
13
|
-
const auth_1 = require("../constants/auth");
|
|
14
|
-
const files_1 = require("../constants/files");
|
|
15
|
-
const environment_1 = require("../lib/environment");
|
|
16
|
-
const logger_1 = require("../lib/logger");
|
|
17
|
-
const errors_DEPRECATED_1 = require("../errors/errors_DEPRECATED");
|
|
18
|
-
const ALL_CMS_PUBLISH_MODES = Object.values(files_1.CMS_PUBLISH_MODE);
|
|
19
|
-
let _config;
|
|
20
|
-
let _configPath;
|
|
21
|
-
let environmentVariableConfigLoaded = false;
|
|
22
|
-
const commaSeparatedValues = (arr, conjunction = 'and', ifempty = '') => {
|
|
23
|
-
const l = arr.length;
|
|
24
|
-
if (!l)
|
|
25
|
-
return ifempty;
|
|
26
|
-
if (l < 2)
|
|
27
|
-
return arr[0];
|
|
28
|
-
if (l < 3)
|
|
29
|
-
return arr.join(` ${conjunction} `);
|
|
30
|
-
arr = arr.slice();
|
|
31
|
-
arr[l - 1] = `${conjunction} ${arr[l - 1]}`;
|
|
32
|
-
return arr.join(', ');
|
|
33
|
-
};
|
|
34
|
-
const getConfig = () => _config;
|
|
35
|
-
exports.getConfig = getConfig;
|
|
36
|
-
function setConfig(updatedConfig) {
|
|
37
|
-
_config = updatedConfig || null;
|
|
38
|
-
return _config;
|
|
39
|
-
}
|
|
40
|
-
exports.setConfig = setConfig;
|
|
41
|
-
function getConfigAccounts(config) {
|
|
42
|
-
const __config = config || (0, exports.getConfig)();
|
|
43
|
-
if (!__config)
|
|
44
|
-
return;
|
|
45
|
-
return __config.portals;
|
|
46
|
-
}
|
|
47
|
-
exports.getConfigAccounts = getConfigAccounts;
|
|
48
|
-
function getConfigDefaultAccount(config) {
|
|
49
|
-
const __config = config || (0, exports.getConfig)();
|
|
50
|
-
if (!__config)
|
|
51
|
-
return;
|
|
52
|
-
return __config.defaultPortal;
|
|
53
|
-
}
|
|
54
|
-
exports.getConfigDefaultAccount = getConfigDefaultAccount;
|
|
55
|
-
function getConfigAccountId(account) {
|
|
56
|
-
if (!account)
|
|
57
|
-
return;
|
|
58
|
-
return account.portalId;
|
|
59
|
-
}
|
|
60
|
-
exports.getConfigAccountId = getConfigAccountId;
|
|
61
|
-
function setConfigPath(path) {
|
|
62
|
-
return (_configPath = path);
|
|
63
|
-
}
|
|
64
|
-
exports.setConfigPath = setConfigPath;
|
|
65
|
-
function getConfigPath(path) {
|
|
66
|
-
return path || (configFileExists() && _configPath) || findConfig((0, path_1.getCwd)());
|
|
67
|
-
}
|
|
68
|
-
exports.getConfigPath = getConfigPath;
|
|
69
|
-
function validateConfig() {
|
|
70
|
-
const config = (0, exports.getConfig)();
|
|
71
|
-
if (!config) {
|
|
72
|
-
logger_1.logger.error('No config was found');
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
const accounts = getConfigAccounts();
|
|
76
|
-
if (!Array.isArray(accounts)) {
|
|
77
|
-
logger_1.logger.error('config.portals[] is not defined');
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
if (accounts.length === 0) {
|
|
81
|
-
logger_1.logger.error('There are no accounts defined in the configuration file');
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
const accountIdsHash = {};
|
|
85
|
-
const accountNamesHash = {};
|
|
86
|
-
return accounts.every(cfg => {
|
|
87
|
-
if (!cfg) {
|
|
88
|
-
logger_1.logger.error('config.portals[] has an empty entry');
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
const accountId = getConfigAccountId(cfg);
|
|
92
|
-
if (!accountId) {
|
|
93
|
-
logger_1.logger.error('config.portals[] has an entry missing portalId');
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
if (accountIdsHash[accountId]) {
|
|
97
|
-
logger_1.logger.error(`config.portals[] has multiple entries with portalId=${accountId}`);
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
if (cfg.name) {
|
|
101
|
-
if (accountNamesHash[cfg.name]) {
|
|
102
|
-
logger_1.logger.error(`config.name has multiple entries with portalId=${accountId}`);
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
if (/\s+/.test(cfg.name)) {
|
|
106
|
-
logger_1.logger.error(`config.name '${cfg.name}' cannot contain spaces`);
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
accountNamesHash[cfg.name] = cfg;
|
|
110
|
-
}
|
|
111
|
-
if (!cfg.accountType) {
|
|
112
|
-
updateAccountConfig({
|
|
113
|
-
...cfg,
|
|
114
|
-
portalId: accountId,
|
|
115
|
-
accountType: getAccountType(undefined, cfg.sandboxAccountType),
|
|
116
|
-
});
|
|
117
|
-
writeConfig();
|
|
118
|
-
}
|
|
119
|
-
accountIdsHash[accountId] = cfg;
|
|
120
|
-
return true;
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
exports.validateConfig = validateConfig;
|
|
124
|
-
function accountNameExistsInConfig(name) {
|
|
125
|
-
const config = (0, exports.getConfig)();
|
|
126
|
-
const accounts = getConfigAccounts();
|
|
127
|
-
if (!config || !Array.isArray(accounts)) {
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
return accounts.some(cfg => cfg.name && cfg.name === name);
|
|
131
|
-
}
|
|
132
|
-
exports.accountNameExistsInConfig = accountNameExistsInConfig;
|
|
133
|
-
function getOrderedAccount(unorderedAccount) {
|
|
134
|
-
const { name, portalId, env, authType, ...rest } = unorderedAccount;
|
|
135
|
-
return {
|
|
136
|
-
name,
|
|
137
|
-
...(portalId && { portalId }),
|
|
138
|
-
env,
|
|
139
|
-
authType,
|
|
140
|
-
...rest,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
exports.getOrderedAccount = getOrderedAccount;
|
|
144
|
-
function getOrderedConfig(unorderedConfig) {
|
|
145
|
-
const { defaultPortal, defaultCmsPublishMode, httpTimeout, allowUsageTracking, portals, ...rest } = unorderedConfig;
|
|
146
|
-
return {
|
|
147
|
-
...(defaultPortal && { defaultPortal }),
|
|
148
|
-
defaultCmsPublishMode,
|
|
149
|
-
httpTimeout,
|
|
150
|
-
allowUsageTracking,
|
|
151
|
-
...rest,
|
|
152
|
-
portals: portals.map(getOrderedAccount),
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
exports.getOrderedConfig = getOrderedConfig;
|
|
156
|
-
function writeConfig(options = {}) {
|
|
157
|
-
if (environmentVariableConfigLoaded) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
let source;
|
|
161
|
-
try {
|
|
162
|
-
source =
|
|
163
|
-
typeof options.source === 'string'
|
|
164
|
-
? options.source
|
|
165
|
-
: js_yaml_1.default.dump(JSON.parse(JSON.stringify(getOrderedConfig((0, exports.getConfig)()), null, 2)));
|
|
166
|
-
}
|
|
167
|
-
catch (err) {
|
|
168
|
-
(0, errors_DEPRECATED_1.logErrorInstance)(err);
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
const configPath = options.path || _configPath;
|
|
172
|
-
try {
|
|
173
|
-
logger_1.logger.debug(`Writing current config to ${configPath}`);
|
|
174
|
-
fs_extra_1.default.ensureFileSync(configPath || '');
|
|
175
|
-
fs_extra_1.default.writeFileSync(configPath || '', source);
|
|
176
|
-
setConfig(parseConfig(source).parsed);
|
|
177
|
-
}
|
|
178
|
-
catch (err) {
|
|
179
|
-
(0, errors_DEPRECATED_1.logFileSystemErrorInstance)(err, {
|
|
180
|
-
filepath: configPath || '',
|
|
181
|
-
operation: 'write',
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
exports.writeConfig = writeConfig;
|
|
186
|
-
function readConfigFile() {
|
|
187
|
-
let source;
|
|
188
|
-
let error;
|
|
189
|
-
if (!_configPath) {
|
|
190
|
-
return { source, error };
|
|
191
|
-
}
|
|
192
|
-
try {
|
|
193
|
-
source = fs_extra_1.default.readFileSync(_configPath);
|
|
194
|
-
}
|
|
195
|
-
catch (err) {
|
|
196
|
-
error = err;
|
|
197
|
-
logger_1.logger.error(`Config file could not be read: ${_configPath}`);
|
|
198
|
-
(0, errors_DEPRECATED_1.logFileSystemErrorInstance)(error, {
|
|
199
|
-
filepath: _configPath,
|
|
200
|
-
operation: 'read',
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
return { source: source && source.toString(), error };
|
|
204
|
-
}
|
|
205
|
-
function parseConfig(configSource) {
|
|
206
|
-
let parsed = undefined;
|
|
207
|
-
let error = undefined;
|
|
208
|
-
if (!configSource) {
|
|
209
|
-
return { parsed, error };
|
|
210
|
-
}
|
|
211
|
-
try {
|
|
212
|
-
parsed = js_yaml_1.default.load(configSource);
|
|
213
|
-
}
|
|
214
|
-
catch (err) {
|
|
215
|
-
error = err;
|
|
216
|
-
logger_1.logger.error(`Config file could not be parsed: ${_configPath}`);
|
|
217
|
-
(0, errors_DEPRECATED_1.logErrorInstance)(err);
|
|
218
|
-
}
|
|
219
|
-
return { parsed, error };
|
|
220
|
-
}
|
|
221
|
-
function loadConfigFromFile(path, options = {}) {
|
|
222
|
-
setConfigPath(getConfigPath(path));
|
|
223
|
-
if (!_configPath) {
|
|
224
|
-
if (!options.silenceErrors) {
|
|
225
|
-
logger_1.logger.error(`A ${config_1.DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME} file could not be found. To create a new config file, use the "hs init" command.`);
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
logger_1.logger.debug(`A ${config_1.DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME} file could not be found`);
|
|
229
|
-
}
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
logger_1.logger.debug(`Reading config from ${_configPath}`);
|
|
233
|
-
const { source, error: sourceError } = readConfigFile();
|
|
234
|
-
if (sourceError)
|
|
235
|
-
return;
|
|
236
|
-
const { parsed, error: parseError } = parseConfig(source);
|
|
237
|
-
if (parseError)
|
|
238
|
-
return;
|
|
239
|
-
setConfig(handleLegacyCmsPublishMode(parsed));
|
|
240
|
-
if (!(0, exports.getConfig)()) {
|
|
241
|
-
logger_1.logger.debug('The config file was empty config');
|
|
242
|
-
logger_1.logger.debug('Initializing an empty config');
|
|
243
|
-
setConfig({ portals: [] });
|
|
244
|
-
}
|
|
245
|
-
return (0, exports.getConfig)();
|
|
246
|
-
}
|
|
247
|
-
function loadConfig(path, options = {
|
|
248
|
-
useEnv: false,
|
|
249
|
-
}) {
|
|
250
|
-
if (options.useEnv && loadEnvironmentVariableConfig(options)) {
|
|
251
|
-
logger_1.logger.debug('Loaded environment variable config');
|
|
252
|
-
environmentVariableConfigLoaded = true;
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
path && logger_1.logger.debug(`Loading config from ${path}`);
|
|
256
|
-
loadConfigFromFile(path, options);
|
|
257
|
-
environmentVariableConfigLoaded = false;
|
|
258
|
-
}
|
|
259
|
-
return (0, exports.getConfig)();
|
|
260
|
-
}
|
|
261
|
-
exports.loadConfig = loadConfig;
|
|
262
|
-
function isTrackingAllowed() {
|
|
263
|
-
if (!configFileExists() || configFileIsBlank()) {
|
|
264
|
-
return true;
|
|
265
|
-
}
|
|
266
|
-
const { allowUsageTracking } = getAndLoadConfigIfNeeded();
|
|
267
|
-
return allowUsageTracking !== false;
|
|
268
|
-
}
|
|
269
|
-
exports.isTrackingAllowed = isTrackingAllowed;
|
|
270
|
-
function getAndLoadConfigIfNeeded(options = {}) {
|
|
271
|
-
if (!(0, exports.getConfig)()) {
|
|
272
|
-
loadConfig('', {
|
|
273
|
-
silenceErrors: true,
|
|
274
|
-
...options,
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
return (0, exports.getConfig)() || { allowUsageTracking: undefined };
|
|
278
|
-
}
|
|
279
|
-
exports.getAndLoadConfigIfNeeded = getAndLoadConfigIfNeeded;
|
|
280
|
-
function findConfig(directory) {
|
|
281
|
-
return (0, findup_sync_1.default)([
|
|
282
|
-
config_1.DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME,
|
|
283
|
-
config_1.DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME.replace('.yml', '.yaml'),
|
|
284
|
-
], { cwd: directory });
|
|
285
|
-
}
|
|
286
|
-
exports.findConfig = findConfig;
|
|
287
|
-
function getEnv(nameOrId) {
|
|
288
|
-
let env = environments_1.ENVIRONMENTS.PROD;
|
|
289
|
-
const config = getAndLoadConfigIfNeeded();
|
|
290
|
-
const accountId = getAccountId(nameOrId);
|
|
291
|
-
if (accountId) {
|
|
292
|
-
const accountConfig = getAccountConfig(accountId);
|
|
293
|
-
if (accountConfig && accountConfig.env) {
|
|
294
|
-
env = accountConfig.env;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
else if (config && config.env) {
|
|
298
|
-
env = config.env;
|
|
299
|
-
}
|
|
300
|
-
return env;
|
|
301
|
-
}
|
|
302
|
-
exports.getEnv = getEnv;
|
|
303
|
-
// Deprecating sandboxAccountType in favor of accountType
|
|
304
|
-
function getAccountType(accountType, sandboxAccountType) {
|
|
305
|
-
if (accountType) {
|
|
306
|
-
return accountType;
|
|
307
|
-
}
|
|
308
|
-
if (typeof sandboxAccountType === 'string') {
|
|
309
|
-
if (sandboxAccountType.toUpperCase() === 'DEVELOPER') {
|
|
310
|
-
return config_1.HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX;
|
|
311
|
-
}
|
|
312
|
-
if (sandboxAccountType.toUpperCase() === 'STANDARD') {
|
|
313
|
-
return config_1.HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
return config_1.HUBSPOT_ACCOUNT_TYPES.STANDARD;
|
|
317
|
-
}
|
|
318
|
-
exports.getAccountType = getAccountType;
|
|
319
|
-
function getAccountConfig(accountId) {
|
|
320
|
-
return getConfigAccounts(getAndLoadConfigIfNeeded())?.find(account => account.portalId === accountId);
|
|
321
|
-
}
|
|
322
|
-
exports.getAccountConfig = getAccountConfig;
|
|
323
|
-
/*
|
|
324
|
-
* Returns a portalId from the config if it exists, else returns null
|
|
325
|
-
*/
|
|
326
|
-
function getAccountId(nameOrId) {
|
|
327
|
-
const config = getAndLoadConfigIfNeeded();
|
|
328
|
-
let name = undefined;
|
|
329
|
-
let accountId = undefined;
|
|
330
|
-
let account = undefined;
|
|
331
|
-
function setNameOrAccountFromSuppliedValue(suppliedValue) {
|
|
332
|
-
if (typeof suppliedValue === 'string') {
|
|
333
|
-
name = suppliedValue;
|
|
334
|
-
if (/^\d+$/.test(suppliedValue)) {
|
|
335
|
-
accountId = parseInt(suppliedValue, 10);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
else if (typeof suppliedValue === 'number') {
|
|
339
|
-
accountId = suppliedValue;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
if (!nameOrId) {
|
|
343
|
-
const defaultAccount = getConfigDefaultAccount(config);
|
|
344
|
-
if (defaultAccount) {
|
|
345
|
-
setNameOrAccountFromSuppliedValue(defaultAccount);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
else {
|
|
349
|
-
setNameOrAccountFromSuppliedValue(nameOrId);
|
|
350
|
-
}
|
|
351
|
-
const accounts = getConfigAccounts(config);
|
|
352
|
-
if (accounts) {
|
|
353
|
-
if (name) {
|
|
354
|
-
account = accounts.find(p => p.name === name);
|
|
355
|
-
}
|
|
356
|
-
if (accountId && !account) {
|
|
357
|
-
account = accounts.find(p => accountId === p.portalId);
|
|
358
|
-
}
|
|
359
|
-
if (account) {
|
|
360
|
-
return account.portalId;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
return undefined;
|
|
364
|
-
}
|
|
365
|
-
exports.getAccountId = getAccountId;
|
|
366
|
-
/**
|
|
367
|
-
* @throws {Error}
|
|
368
|
-
*/
|
|
369
|
-
function removeSandboxAccountFromConfig(nameOrId) {
|
|
370
|
-
const config = getAndLoadConfigIfNeeded();
|
|
371
|
-
const accountId = getAccountId(nameOrId);
|
|
372
|
-
let promptDefaultAccount = false;
|
|
373
|
-
if (!accountId) {
|
|
374
|
-
throw new Error(`Unable to find account for ${nameOrId}.`);
|
|
375
|
-
}
|
|
376
|
-
const accountConfig = getAccountConfig(accountId);
|
|
377
|
-
const accountType = getAccountType(accountConfig?.accountType, accountConfig?.sandboxAccountType);
|
|
378
|
-
const isSandboxAccount = accountType === config_1.HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX ||
|
|
379
|
-
accountType === config_1.HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX;
|
|
380
|
-
if (!isSandboxAccount)
|
|
381
|
-
return promptDefaultAccount;
|
|
382
|
-
if (config.defaultPortal === accountConfig?.name) {
|
|
383
|
-
promptDefaultAccount = true;
|
|
384
|
-
}
|
|
385
|
-
const accounts = getConfigAccounts(config);
|
|
386
|
-
if (accountConfig && accounts) {
|
|
387
|
-
logger_1.logger.debug(`Deleting config for ${accountId}`);
|
|
388
|
-
const index = accounts.indexOf(accountConfig);
|
|
389
|
-
accounts.splice(index, 1);
|
|
390
|
-
}
|
|
391
|
-
writeConfig();
|
|
392
|
-
return promptDefaultAccount;
|
|
393
|
-
}
|
|
394
|
-
exports.removeSandboxAccountFromConfig = removeSandboxAccountFromConfig;
|
|
395
|
-
/**
|
|
396
|
-
* @throws {Error}
|
|
397
|
-
*/
|
|
398
|
-
function updateAccountConfig(configOptions) {
|
|
399
|
-
const { accountType, apiKey, authType, clientId, clientSecret, defaultCmsPublishMode, environment, name, parentAccountId, personalAccessKey, portalId, sandboxAccountType, scopes, tokenInfo, } = configOptions;
|
|
400
|
-
if (!portalId) {
|
|
401
|
-
throw new Error('A portalId is required to update the config');
|
|
402
|
-
}
|
|
403
|
-
const config = getAndLoadConfigIfNeeded();
|
|
404
|
-
const accountConfig = getAccountConfig(portalId);
|
|
405
|
-
let auth = accountConfig && accountConfig.auth;
|
|
406
|
-
if (clientId || clientSecret || scopes || tokenInfo) {
|
|
407
|
-
auth = {
|
|
408
|
-
...(accountConfig ? accountConfig.auth : {}),
|
|
409
|
-
clientId,
|
|
410
|
-
clientSecret,
|
|
411
|
-
scopes,
|
|
412
|
-
tokenInfo,
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
const env = (0, environment_1.getValidEnv)(environment ||
|
|
416
|
-
(configOptions && configOptions.env) ||
|
|
417
|
-
(accountConfig && accountConfig.env));
|
|
418
|
-
const cmsPublishMode = defaultCmsPublishMode?.toLowerCase();
|
|
419
|
-
const nextAccountConfig = {
|
|
420
|
-
...accountConfig,
|
|
421
|
-
name: name || (accountConfig && accountConfig.name),
|
|
422
|
-
env,
|
|
423
|
-
...(portalId && { portalId }),
|
|
424
|
-
authType,
|
|
425
|
-
auth,
|
|
426
|
-
accountType: getAccountType(accountType, sandboxAccountType),
|
|
427
|
-
apiKey,
|
|
428
|
-
defaultCmsPublishMode: cmsPublishMode && Object.hasOwn(files_1.CMS_PUBLISH_MODE, cmsPublishMode)
|
|
429
|
-
? cmsPublishMode
|
|
430
|
-
: undefined,
|
|
431
|
-
personalAccessKey,
|
|
432
|
-
sandboxAccountType,
|
|
433
|
-
parentAccountId,
|
|
434
|
-
};
|
|
435
|
-
let accounts = getConfigAccounts(config);
|
|
436
|
-
if (accountConfig && accounts) {
|
|
437
|
-
logger_1.logger.debug(`Updating config for ${portalId}`);
|
|
438
|
-
const index = accounts.indexOf(accountConfig);
|
|
439
|
-
accounts[index] = nextAccountConfig;
|
|
440
|
-
}
|
|
441
|
-
else {
|
|
442
|
-
logger_1.logger.debug(`Adding config entry for ${portalId}`);
|
|
443
|
-
if (accounts) {
|
|
444
|
-
accounts.push(nextAccountConfig);
|
|
445
|
-
}
|
|
446
|
-
else {
|
|
447
|
-
accounts = [nextAccountConfig];
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
return nextAccountConfig;
|
|
451
|
-
}
|
|
452
|
-
exports.updateAccountConfig = updateAccountConfig;
|
|
453
|
-
/**
|
|
454
|
-
* @throws {Error}
|
|
455
|
-
*/
|
|
456
|
-
function updateDefaultAccount(defaultAccount) {
|
|
457
|
-
if (!defaultAccount ||
|
|
458
|
-
(typeof defaultAccount !== 'number' && typeof defaultAccount !== 'string')) {
|
|
459
|
-
throw new Error(`A 'defaultPortal' with value of number or string is required to update the config`);
|
|
460
|
-
}
|
|
461
|
-
const config = getAndLoadConfigIfNeeded();
|
|
462
|
-
config.defaultPortal = defaultAccount;
|
|
463
|
-
setDefaultConfigPathIfUnset();
|
|
464
|
-
writeConfig();
|
|
465
|
-
}
|
|
466
|
-
exports.updateDefaultAccount = updateDefaultAccount;
|
|
467
|
-
/**
|
|
468
|
-
* @throws {Error}
|
|
469
|
-
*/
|
|
470
|
-
function updateDefaultCmsPublishMode(defaultCmsPublishMode) {
|
|
471
|
-
if (!defaultCmsPublishMode ||
|
|
472
|
-
!ALL_CMS_PUBLISH_MODES.find(m => m === defaultCmsPublishMode)) {
|
|
473
|
-
throw new Error(`The mode ${defaultCmsPublishMode} is invalid. Valid values are ${commaSeparatedValues(ALL_CMS_PUBLISH_MODES)}.`);
|
|
474
|
-
}
|
|
475
|
-
const config = getAndLoadConfigIfNeeded();
|
|
476
|
-
config.defaultCmsPublishMode = defaultCmsPublishMode;
|
|
477
|
-
setDefaultConfigPathIfUnset();
|
|
478
|
-
writeConfig();
|
|
479
|
-
}
|
|
480
|
-
exports.updateDefaultCmsPublishMode = updateDefaultCmsPublishMode;
|
|
481
|
-
/**
|
|
482
|
-
* @throws {Error}
|
|
483
|
-
*/
|
|
484
|
-
function updateHttpTimeout(timeout) {
|
|
485
|
-
const parsedTimeout = parseInt(timeout);
|
|
486
|
-
if (isNaN(parsedTimeout) || parsedTimeout < config_1.MIN_HTTP_TIMEOUT) {
|
|
487
|
-
throw new Error(`The value ${timeout} is invalid. The value must be a number greater than ${config_1.MIN_HTTP_TIMEOUT}.`);
|
|
488
|
-
}
|
|
489
|
-
const config = getAndLoadConfigIfNeeded();
|
|
490
|
-
config.httpTimeout = parsedTimeout;
|
|
491
|
-
setDefaultConfigPathIfUnset();
|
|
492
|
-
writeConfig();
|
|
493
|
-
}
|
|
494
|
-
exports.updateHttpTimeout = updateHttpTimeout;
|
|
495
|
-
/**
|
|
496
|
-
* @throws {Error}
|
|
497
|
-
*/
|
|
498
|
-
function updateAllowAutoUpdates(enabled) {
|
|
499
|
-
const config = getAndLoadConfigIfNeeded();
|
|
500
|
-
config.allowAutoUpdates = enabled;
|
|
501
|
-
setDefaultConfigPathIfUnset();
|
|
502
|
-
writeConfig();
|
|
503
|
-
}
|
|
504
|
-
exports.updateAllowAutoUpdates = updateAllowAutoUpdates;
|
|
505
|
-
/**
|
|
506
|
-
* @throws {Error}
|
|
507
|
-
*/
|
|
508
|
-
function updateAllowUsageTracking(isEnabled) {
|
|
509
|
-
if (typeof isEnabled !== 'boolean') {
|
|
510
|
-
throw new Error(`Unable to update allowUsageTracking. The value ${isEnabled} is invalid. The value must be a boolean.`);
|
|
511
|
-
}
|
|
512
|
-
const config = getAndLoadConfigIfNeeded();
|
|
513
|
-
config.allowUsageTracking = isEnabled;
|
|
514
|
-
setDefaultConfigPathIfUnset();
|
|
515
|
-
writeConfig();
|
|
516
|
-
}
|
|
517
|
-
exports.updateAllowUsageTracking = updateAllowUsageTracking;
|
|
518
|
-
function updateAutoOpenBrowser(isEnabled) {
|
|
519
|
-
if (typeof isEnabled !== 'boolean') {
|
|
520
|
-
throw new Error(`Unable to update autoOpenBrowser. The value ${isEnabled} is invalid. The value must be a boolean.`);
|
|
521
|
-
}
|
|
522
|
-
const config = getAndLoadConfigIfNeeded();
|
|
523
|
-
config.autoOpenBrowser = isEnabled;
|
|
524
|
-
setDefaultConfigPathIfUnset();
|
|
525
|
-
writeConfig();
|
|
526
|
-
}
|
|
527
|
-
exports.updateAutoOpenBrowser = updateAutoOpenBrowser;
|
|
528
|
-
/**
|
|
529
|
-
* @throws {Error}
|
|
530
|
-
*/
|
|
531
|
-
async function renameAccount(currentName, newName) {
|
|
532
|
-
const accountId = getAccountId(currentName);
|
|
533
|
-
const accountConfigToRename = getAccountConfig(accountId);
|
|
534
|
-
const defaultAccount = getConfigDefaultAccount();
|
|
535
|
-
if (!accountConfigToRename) {
|
|
536
|
-
throw new Error(`Cannot find account with identifier ${currentName}`);
|
|
537
|
-
}
|
|
538
|
-
await updateAccountConfig({
|
|
539
|
-
...accountConfigToRename,
|
|
540
|
-
name: newName,
|
|
541
|
-
});
|
|
542
|
-
if (accountConfigToRename.name === defaultAccount) {
|
|
543
|
-
updateDefaultAccount(newName);
|
|
544
|
-
}
|
|
545
|
-
return writeConfig();
|
|
546
|
-
}
|
|
547
|
-
exports.renameAccount = renameAccount;
|
|
548
|
-
/**
|
|
549
|
-
* @throws {Error}
|
|
550
|
-
*/
|
|
551
|
-
async function deleteAccount(accountName) {
|
|
552
|
-
const config = getAndLoadConfigIfNeeded();
|
|
553
|
-
const accounts = getConfigAccounts(config);
|
|
554
|
-
const accountIdToDelete = getAccountId(accountName);
|
|
555
|
-
if (!accountIdToDelete || !accounts) {
|
|
556
|
-
throw new Error(`Cannot find account with identifier ${accountName}`);
|
|
557
|
-
}
|
|
558
|
-
setConfig({
|
|
559
|
-
...config,
|
|
560
|
-
defaultPortal: config.defaultPortal === accountName ||
|
|
561
|
-
config.defaultPortal === accountIdToDelete
|
|
562
|
-
? undefined
|
|
563
|
-
: config.defaultPortal,
|
|
564
|
-
portals: accounts.filter(account => account.portalId !== accountIdToDelete),
|
|
565
|
-
});
|
|
566
|
-
return writeConfig();
|
|
567
|
-
}
|
|
568
|
-
exports.deleteAccount = deleteAccount;
|
|
569
|
-
function setDefaultConfigPathIfUnset() {
|
|
570
|
-
if (!_configPath) {
|
|
571
|
-
setDefaultConfigPath();
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
function setDefaultConfigPath() {
|
|
575
|
-
setConfigPath(`${(0, path_1.getCwd)()}/${config_1.DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME}`);
|
|
576
|
-
}
|
|
577
|
-
function configFileExists() {
|
|
578
|
-
return Boolean(_configPath && fs_extra_1.default.existsSync(_configPath));
|
|
579
|
-
}
|
|
580
|
-
function configFileIsBlank() {
|
|
581
|
-
return Boolean(_configPath && fs_extra_1.default.readFileSync(_configPath).length === 0);
|
|
582
|
-
}
|
|
583
|
-
function createEmptyConfigFile({ path } = {}) {
|
|
584
|
-
if (!path) {
|
|
585
|
-
setDefaultConfigPathIfUnset();
|
|
586
|
-
if (configFileExists()) {
|
|
587
|
-
return;
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
else {
|
|
591
|
-
setConfigPath(path);
|
|
592
|
-
}
|
|
593
|
-
writeConfig({ source: '', path });
|
|
594
|
-
}
|
|
595
|
-
exports.createEmptyConfigFile = createEmptyConfigFile;
|
|
596
|
-
function deleteEmptyConfigFile() {
|
|
597
|
-
configFileExists() && configFileIsBlank() && fs_extra_1.default.unlinkSync(_configPath || '');
|
|
598
|
-
}
|
|
599
|
-
exports.deleteEmptyConfigFile = deleteEmptyConfigFile;
|
|
600
|
-
function deleteConfigFile() {
|
|
601
|
-
configFileExists() && fs_extra_1.default.unlinkSync(_configPath || '');
|
|
602
|
-
}
|
|
603
|
-
exports.deleteConfigFile = deleteConfigFile;
|
|
604
|
-
function getConfigVariablesFromEnv() {
|
|
605
|
-
const env = process.env;
|
|
606
|
-
return {
|
|
607
|
-
apiKey: env[environments_1.ENVIRONMENT_VARIABLES.HUBSPOT_API_KEY],
|
|
608
|
-
clientId: env[environments_1.ENVIRONMENT_VARIABLES.HUBSPOT_CLIENT_ID],
|
|
609
|
-
clientSecret: env[environments_1.ENVIRONMENT_VARIABLES.HUBSPOT_CLIENT_SECRET],
|
|
610
|
-
personalAccessKey: env[environments_1.ENVIRONMENT_VARIABLES.HUBSPOT_PERSONAL_ACCESS_KEY],
|
|
611
|
-
portalId: parseInt(env[environments_1.ENVIRONMENT_VARIABLES.HUBSPOT_ACCOUNT_ID] || '', 10) ||
|
|
612
|
-
parseInt(env[environments_1.ENVIRONMENT_VARIABLES.HUBSPOT_PORTAL_ID] || '', 10),
|
|
613
|
-
refreshToken: env[environments_1.ENVIRONMENT_VARIABLES.HUBSPOT_REFRESH_TOKEN],
|
|
614
|
-
httpTimeout: env[environments_1.ENVIRONMENT_VARIABLES.HTTP_TIMEOUT]
|
|
615
|
-
? parseInt(env[environments_1.ENVIRONMENT_VARIABLES.HTTP_TIMEOUT])
|
|
616
|
-
: undefined,
|
|
617
|
-
env: (0, environment_1.getValidEnv)(env[environments_1.ENVIRONMENT_VARIABLES.HUBSPOT_ENVIRONMENT]),
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
|
-
function generatePersonalAccessKeyConfig(portalId, personalAccessKey, env, httpTimeout) {
|
|
621
|
-
return {
|
|
622
|
-
portals: [
|
|
623
|
-
{
|
|
624
|
-
authType: auth_1.PERSONAL_ACCESS_KEY_AUTH_METHOD.value,
|
|
625
|
-
portalId,
|
|
626
|
-
personalAccessKey,
|
|
627
|
-
env,
|
|
628
|
-
},
|
|
629
|
-
],
|
|
630
|
-
httpTimeout,
|
|
631
|
-
};
|
|
632
|
-
}
|
|
633
|
-
function generateOauthConfig(portalId, clientId, clientSecret, refreshToken, scopes, env, httpTimeout) {
|
|
634
|
-
return {
|
|
635
|
-
portals: [
|
|
636
|
-
{
|
|
637
|
-
authType: auth_1.OAUTH_AUTH_METHOD.value,
|
|
638
|
-
portalId,
|
|
639
|
-
auth: {
|
|
640
|
-
clientId,
|
|
641
|
-
clientSecret,
|
|
642
|
-
scopes,
|
|
643
|
-
tokenInfo: {
|
|
644
|
-
refreshToken,
|
|
645
|
-
},
|
|
646
|
-
},
|
|
647
|
-
env,
|
|
648
|
-
},
|
|
649
|
-
],
|
|
650
|
-
httpTimeout,
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
function generateApiKeyConfig(portalId, apiKey, env) {
|
|
654
|
-
return {
|
|
655
|
-
portals: [
|
|
656
|
-
{
|
|
657
|
-
authType: auth_1.API_KEY_AUTH_METHOD.value,
|
|
658
|
-
portalId,
|
|
659
|
-
apiKey,
|
|
660
|
-
env,
|
|
661
|
-
},
|
|
662
|
-
],
|
|
663
|
-
};
|
|
664
|
-
}
|
|
665
|
-
function loadConfigFromEnvironment({ useEnv = false, } = {}) {
|
|
666
|
-
const { apiKey, clientId, clientSecret, personalAccessKey, portalId, refreshToken, env, httpTimeout, } = getConfigVariablesFromEnv();
|
|
667
|
-
const unableToLoadEnvConfigError = 'Unable to load config from environment variables.';
|
|
668
|
-
if (!portalId) {
|
|
669
|
-
useEnv && logger_1.logger.error(unableToLoadEnvConfigError);
|
|
670
|
-
return;
|
|
671
|
-
}
|
|
672
|
-
if (httpTimeout && httpTimeout < config_1.MIN_HTTP_TIMEOUT) {
|
|
673
|
-
throw new Error(`The HTTP timeout value ${httpTimeout} is invalid. The value must be a number greater than ${config_1.MIN_HTTP_TIMEOUT}.`);
|
|
674
|
-
}
|
|
675
|
-
if (personalAccessKey) {
|
|
676
|
-
return generatePersonalAccessKeyConfig(portalId, personalAccessKey, env, httpTimeout);
|
|
677
|
-
}
|
|
678
|
-
else if (clientId && clientSecret && refreshToken) {
|
|
679
|
-
return generateOauthConfig(portalId, clientId, clientSecret, refreshToken, auth_1.OAUTH_SCOPES.map(scope => scope.value), env, httpTimeout);
|
|
680
|
-
}
|
|
681
|
-
else if (apiKey) {
|
|
682
|
-
return generateApiKeyConfig(portalId, apiKey, env);
|
|
683
|
-
}
|
|
684
|
-
else {
|
|
685
|
-
useEnv && logger_1.logger.error(unableToLoadEnvConfigError);
|
|
686
|
-
return;
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
exports.loadConfigFromEnvironment = loadConfigFromEnvironment;
|
|
690
|
-
function loadEnvironmentVariableConfig(options) {
|
|
691
|
-
const envConfig = loadConfigFromEnvironment(options);
|
|
692
|
-
if (!envConfig) {
|
|
693
|
-
return null;
|
|
694
|
-
}
|
|
695
|
-
const { portalId } = getConfigVariablesFromEnv();
|
|
696
|
-
logger_1.logger.debug(`Loaded config from environment variables for account ${portalId}`);
|
|
697
|
-
return setConfig(handleLegacyCmsPublishMode(envConfig));
|
|
698
|
-
}
|
|
699
|
-
function isConfigFlagEnabled(flag, defaultValue = false) {
|
|
700
|
-
if (!configFileExists() || configFileIsBlank()) {
|
|
701
|
-
return defaultValue;
|
|
702
|
-
}
|
|
703
|
-
const config = getAndLoadConfigIfNeeded();
|
|
704
|
-
return Boolean(config[flag] || defaultValue);
|
|
705
|
-
}
|
|
706
|
-
exports.isConfigFlagEnabled = isConfigFlagEnabled;
|
|
707
|
-
function handleLegacyCmsPublishMode(config) {
|
|
708
|
-
if (config?.defaultMode) {
|
|
709
|
-
config.defaultCmsPublishMode = config.defaultMode;
|
|
710
|
-
delete config.defaultMode;
|
|
711
|
-
}
|
|
712
|
-
return config;
|
|
713
|
-
}
|
|
714
|
-
function hasLocalStateFlag(flag) {
|
|
715
|
-
if (!_config) {
|
|
716
|
-
return false;
|
|
717
|
-
}
|
|
718
|
-
return _config.flags?.includes(flag) || false;
|
|
719
|
-
}
|
|
720
|
-
exports.hasLocalStateFlag = hasLocalStateFlag;
|
|
721
|
-
function addLocalStateFlag(flag) {
|
|
722
|
-
if (!_config) {
|
|
723
|
-
throw new Error('No config loaded');
|
|
724
|
-
}
|
|
725
|
-
if (!hasLocalStateFlag(flag)) {
|
|
726
|
-
_config.flags = [...(_config.flags || []), flag];
|
|
727
|
-
}
|
|
728
|
-
writeConfig();
|
|
729
|
-
}
|
|
730
|
-
exports.addLocalStateFlag = addLocalStateFlag;
|
|
731
|
-
function removeLocalStateFlag(flag) {
|
|
732
|
-
if (!_config) {
|
|
733
|
-
throw new Error('No config loaded');
|
|
734
|
-
}
|
|
735
|
-
_config.flags = _config.flags?.filter(f => f !== flag) || [];
|
|
736
|
-
writeConfig();
|
|
737
|
-
}
|
|
738
|
-
exports.removeLocalStateFlag = removeLocalStateFlag;
|