Skip to content

Commit

Permalink
Merge pull request #464 from /issues/27-hs-config
Browse files Browse the repository at this point in the history
Added hs config commands
  • Loading branch information
miketalley authored May 26, 2021
2 parents ac9e57f + 73a34ce commit 146d82d
Show file tree
Hide file tree
Showing 25 changed files with 812 additions and 135 deletions.
18 changes: 18 additions & 0 deletions packages/cli-lib/lib/__tests__/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,40 @@ describe('lib/config', () => {
portals: PORTALS,
});
});

it('returns portalId from config when a name is passed', () => {
expect(getAccountId(OAUTH2_CONFIG.name)).toEqual(OAUTH2_CONFIG.portalId);
});

it('returns portalId from config when a string id is passed', () => {
expect(getAccountId(OAUTH2_CONFIG.portalId.toString())).toEqual(
OAUTH2_CONFIG.portalId
);
});

it('returns portalId from config when a numeric id is passed', () => {
expect(getAccountId(OAUTH2_CONFIG.portalId)).toEqual(
OAUTH2_CONFIG.portalId
);
});

it('returns defaultPortal from config', () => {
expect(getAccountId()).toEqual(PERSONAL_ACCESS_KEY_CONFIG.portalId);
});

describe('when defaultPortal is a portalId', () => {
beforeEach(() => {
process.env = {};
setConfig({
defaultPortal: PERSONAL_ACCESS_KEY_CONFIG.portalId,
portals: PORTALS,
});
});

it('returns defaultPortal from config', () => {
expect(getAccountId()).toEqual(PERSONAL_ACCESS_KEY_CONFIG.portalId);
});
});
});

describe('updateDefaultAccount method', () => {
Expand Down
105 changes: 97 additions & 8 deletions packages/cli-lib/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
logFileSystemErrorInstance,
} = require('../errorHandlers/fileSystemErrors');
const { logErrorInstance } = require('../errorHandlers/standardErrors');
const { commaSeparatedValues } = require('./text');
const { getCwd } = require('../path');
const {
DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME,
Expand All @@ -20,9 +21,11 @@ const {
PERSONAL_ACCESS_KEY_AUTH_METHOD,
OAUTH_SCOPES,
ENVIRONMENT_VARIABLES,
MIN_HTTP_TIMEOUT,
} = require('./constants');
const { getValidEnv } = require('./environment');

const ALL_MODES = Object.values(Mode);
let _config;
let _configPath;
let environmentVariableConfigLoaded = false;
Expand Down Expand Up @@ -421,20 +424,24 @@ const getAccountId = nameOrId => {
let accountId;
let account;

const setNameOrAccountFromSuppliedValue = suppliedValue => {
if (typeof suppliedValue === 'number') {
accountId = suppliedValue;
} else if (/^\d+$/.test(suppliedValue)) {
accountId = parseInt(suppliedValue, 10);
} else {
name = suppliedValue;
}
};

if (!nameOrId) {
const defaultAccount = getConfigDefaultAccount(config);

if (defaultAccount) {
name = defaultAccount;
setNameOrAccountFromSuppliedValue(defaultAccount);
}
} else {
if (typeof nameOrId === 'number') {
accountId = nameOrId;
} else if (/^\d+$/.test(nameOrId)) {
accountId = parseInt(nameOrId, 10);
} else {
name = nameOrId;
}
setNameOrAccountFromSuppliedValue(nameOrId);
}

const accounts = getConfigAccounts(config);
Expand Down Expand Up @@ -540,6 +547,84 @@ const updateDefaultAccount = defaultAccount => {
writeConfig();
};

/**
* @throws {Error}
*/
const updateDefaultMode = defaultMode => {
if (!defaultMode || !ALL_MODES.find(m => m === defaultMode)) {
throw new Error(
`The mode ${defaultMode} is invalid. Valid values are ${commaSeparatedValues(
ALL_MODES
)}.`
);
}

const config = getAndLoadConfigIfNeeded();
config.defaultMode = defaultMode;

setDefaultConfigPathIfUnset();
writeConfig();
};

/**
* @throws {Error}
*/
const updateHttpTimeout = timeout => {
const parsedTimeout = parseInt(timeout);
if (isNaN(parsedTimeout) || parsedTimeout < MIN_HTTP_TIMEOUT) {
throw new Error(
`The value ${timeout} is invalid. The value must be a number greater than ${MIN_HTTP_TIMEOUT}.`
);
}

const config = getAndLoadConfigIfNeeded();
config.httpTimeout = parsedTimeout;

setDefaultConfigPathIfUnset();
writeConfig();
};

/**
* @throws {Error}
*/
const updateAllowUsageTracking = isEnabled => {
if (typeof isEnabled !== 'boolean') {
throw new Error(
`Unable to update allowUsageTracking. The value ${isEnabled} is invalid. The value must be a boolean.`
);
}

const config = getAndLoadConfigIfNeeded();
config.allowUsageTracking = isEnabled;

setDefaultConfigPathIfUnset();
writeConfig();
};

/**
* @throws {Error}
*/
const renameAccount = async (currentName, newName) => {
const accountId = getAccountId(currentName);
const accountConfigToRename = getAccountConfig(accountId);
const defaultAccount = getConfigDefaultAccount();

if (!accountConfigToRename) {
throw new Error(`Cannot find account with identifier ${currentName}`);
}

await updateAccountConfig({
...accountConfigToRename,
name: newName,
});

if (accountConfigToRename.name === defaultAccount) {
updateDefaultAccount(newName);
}

return writeConfig();
};

const setDefaultConfigPathIfUnset = () => {
if (!_configPath) {
setDefaultConfigPath();
Expand Down Expand Up @@ -724,6 +809,10 @@ module.exports = {
getAccountId,
updateAccountConfig,
updateDefaultAccount,
updateDefaultMode,
updateHttpTimeout,
updateAllowUsageTracking,
renameAccount,
createEmptyConfigFile,
deleteEmptyConfigFile,
isTrackingAllowed,
Expand Down
3 changes: 3 additions & 0 deletions packages/cli-lib/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ const ConfigFlags = {
USE_CUSTOM_OBJECT_HUBFILE: 'useCustomObjectHubfile',
};

const MIN_HTTP_TIMEOUT = 3000;

module.exports = {
ConfigFlags,
Mode,
Expand All @@ -134,4 +136,5 @@ module.exports = {
FOLDER_DOT_EXTENSIONS,
POLLING_DELAY,
GITHUB_RELEASE_TYPES,
MIN_HTTP_TIMEOUT,
};
49 changes: 49 additions & 0 deletions packages/cli-lib/lib/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const chalk = require('chalk');
const { table } = require('table');
const { mergeDeep } = require('./utils');

const tableConfigDefaults = {
singleLine: true,
border: {
topBody: '',
topJoin: '',
topLeft: '',
topRight: '',

bottomBody: '',
bottomJoin: '',
bottomLeft: '',
bottomRight: '',

bodyLeft: '',
bodyRight: '',
bodyJoin: '',

joinBody: '',
joinLeft: '',
joinRight: '',
joinJoin: '',
},
columnDefault: {
paddingLeft: 0,
paddingRight: 1,
},
drawHorizontalLine: () => {
return false;
},
};

const getTableContents = (tableData = [], tableConfig = {}) => {
const mergedConfig = mergeDeep({}, tableConfigDefaults, tableConfig);

return table(tableData, mergedConfig);
};

const getTableHeader = headerItems => {
return headerItems.map(headerItem => chalk.bold(headerItem));
};

module.exports = {
getTableContents,
getTableHeader,
};
35 changes: 35 additions & 0 deletions packages/cli-lib/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Simple object check.
* @param item
* @returns {boolean}
*/
const isObject = item => {
return item && typeof item === 'object' && !Array.isArray(item);
};

/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
const mergeDeep = (target, ...sources) => {
if (!sources.length) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}

return mergeDeep(target, ...sources);
};

module.exports = {
mergeDeep,
};
2 changes: 1 addition & 1 deletion packages/cli-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"prettier": "^1.19.1",
"request": "^2.87.0",
"request-promise-native": "^1.0.7",
"table": "^6.0.3",
"table": "^6.6.0",
"unixify": "1.0.0"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { table, getBorderCharacters } = require('table');

const logSchemas = schemas => {
const data = schemas
.map(r => [r.labels.singular, r.name, r.objectTypeId])
.map(r => [r.labels.singular, r.name, r.objectTypeId || ''])
.sort((a, b) => (a[1] > b[1] ? 1 : -1));
data.unshift([
chalk.bold('Label'),
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const openCommand = require('../commands/open');
const mvCommand = require('../commands/mv');
const projectsCommand = require('../commands/projects');
const themeCommand = require('../commands/theme');
const configCommand = require('../commands/config');
const accountsCommand = require('../commands/accounts');

const notifier = updateNotifier({ pkg: { ...pkg, name: '@hubspot/cli' } });

Expand Down Expand Up @@ -94,6 +96,8 @@ const argv = yargs
.command(mvCommand)
.command(projectsCommand)
.command(themeCommand)
.command(configCommand)
.command(accountsCommand)
.help()
.recommendCommands()
.demandCommand(1, '')
Expand Down
21 changes: 21 additions & 0 deletions packages/cli/commands/accounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { addConfigOptions, addAccountOptions } = require('../lib/commonOpts');
const list = require('./accounts/list');
const rename = require('./accounts/rename');

exports.command = 'accounts';
exports.describe = 'Commands for working with accounts';

exports.builder = yargs => {
addConfigOptions(yargs, true);
addAccountOptions(yargs, true);

yargs
.command({
...list,
aliases: 'ls',
})
.command(rename)
.demandCommand(1, '');

return yargs;
};
Loading

0 comments on commit 146d82d

Please sign in to comment.