Skip to content

Commit

Permalink
WIP - Migrating Portal/Account
Browse files Browse the repository at this point in the history
  • Loading branch information
drewjenkins committed Oct 9, 2020
1 parent 6af8af4 commit fd031f0
Show file tree
Hide file tree
Showing 84 changed files with 1,085 additions and 1,051 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ yarn lerna bootstrap

* To test a command

First, you will need to create a config for the portals that you want to be able to test using.
First, you will need to create a config for the accounts that you want to be able to test using.

Once the config is created, commands can be run by providing a path to the executable file:

```
yarn hscms upload --portalId [src] [dest]`
yarn hscms upload --accountId [src] [dest]`
```
* To test a command when `@hubspot/cms-cli` is a dependency in another package like `cms-theme-boilerplate`.
Expand Down
2 changes: 1 addition & 1 deletion docs/TechnicalDesign.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Write programs to handle text streams, because that is a universal interface.

2. Don't prescribe a particular node task runner (Gulp, Grunt, etc...). Instead work to separate the cli from the underlying functionality so that we can easily create a library separate from the CLI for use in the creation of tasks.

3. Support multiple portals. It should be easy to run the commands against multiple portals.
3. Support multiple accounts. It should be easy to run the commands against multiple accounts.

4. Minimize dependencies.

Expand Down
30 changes: 15 additions & 15 deletions packages/cms-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ hs help
Upload a file or directory to the Design Manager

```bash
hs upload --portal=DEV [src] [dest]
hs upload --account=DEV [src] [dest]
```

Fetch a file or directory by path from the Design Manager

```bash
hs fetch --portal=DEV [path] [dest]
hs fetch --account=DEV [path] [dest]

# Overwrite existing files
hs fetch --portal=DEV --overwrite [path] [dest]
hs fetch --account=DEV --overwrite [path] [dest]
```

Watch a directory of files and automatically upload changes to the Design Manager

```bash
hs watch --portal=DEV [src] [dest]
hs watch --account=DEV [src] [dest]
```

Create a new asset locally
Expand All @@ -76,10 +76,10 @@ hs create [type] [dest]
Delete a file or directory from the Design Manager

```bash
hs remove --portal=DEV [path]
hs remove --account=DEV [path]
```

Authenticate against a portal using either `personalaccesskey` or `oauth2`
Authenticate against a account using either `personalaccesskey` or `oauth2`

```bash
hs auth personalaccesskey
Expand All @@ -90,16 +90,16 @@ hs auth personalaccesskey
Upload a file or directory to the File Manager

```bash
hs filemanager upload --portal=DEV [src] [dest]
hs filemanager upload --account=DEV [src] [dest]
```

Fetch a file or directory from the File Manager

```bash
hs filemanager fetch --portal=DEV [src] [dest]
hs filemanager fetch --account=DEV [src] [dest]

# Overwrite existing files
hs filemanager fetch --portal=DEV --overwrite [path] [dest]
hs filemanager fetch --account=DEV --overwrite [path] [dest]
```

### HubDB Commands
Expand Down Expand Up @@ -143,18 +143,18 @@ There are three ways that the tools can authenticate with HubSpot.
2. Run `hs auth oauth2`
3. Select `OAuth2` and follow the steps

_**Note:** The Portal ID used should be the CMS Portal ID (not the developer app ID). Client ID and Client Secret are from the developer app._
_**Note:** The Account ID used should be the CMS Account ID (not the developer app ID). Client ID and Client Secret are from the developer app._

### HubSpot API Key

1. [Set up an API Key for the CMS Portal](https://knowledge.hubspot.com/articles/kcs_article/integrations/how-do-i-get-my-hubspot-api-key)
2. Edit the `hubspot.config.yml` file to set the `authType` for the portal to `apikey` and add `apiKey` as shown below:
1. [Set up an API Key for the CMS Account](https://knowledge.hubspot.com/articles/kcs_article/integrations/how-do-i-get-my-hubspot-api-key)
2. Edit the `hubspot.config.yml` file to set the `authType` for the account to `apikey` and add `apiKey` as shown below:

```yaml
defaultPortal: DEV
portals:
defaultAccount: DEV
accounts:
- name: DEV
portalId: 123
accountId: 123
authType: apikey
apiKey: d1234567-123e-7890-b123-aaa80164b4cb
```
22 changes: 11 additions & 11 deletions packages/cms-cli/commands/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const {
updateConfigWithPersonalAccessKey,
} = require('@hubspot/cms-lib/personalAccessKey');
const {
updatePortalConfig,
portalNameExistsInConfig,
updateAccountConfig,
accountNameExistsInConfig,
writeConfig,
} = require('@hubspot/cms-lib/lib/config');
const {
promptUser,
personalAccessKeyPrompt,
OAUTH_FLOW,
API_KEY_FLOW,
PORTAL_NAME,
ACCOUNT_NAME,
} = require('../lib/prompts');
const {
addConfigOptions,
Expand All @@ -44,14 +44,14 @@ const SUPPORTED_AUTHENTICATION_PROTOCOLS_TEXT = commaSeparatedValues(
ALLOWED_AUTH_METHODS
);

const promptForPortalNameIfNotSet = async updatedConfig => {
const promptForAccountNameIfNotSet = async updatedConfig => {
if (!updatedConfig.name) {
let promptAnswer;
let validName = null;
while (!validName) {
promptAnswer = await promptUser([PORTAL_NAME]);
promptAnswer = await promptUser([ACCOUNT_NAME]);

if (!portalNameExistsInConfig(promptAnswer.name)) {
if (!accountNameExistsInConfig(promptAnswer.name)) {
validName = promptAnswer.name;
} else {
logger.log(
Expand Down Expand Up @@ -90,10 +90,10 @@ exports.handler = async options => {
switch (authType) {
case API_KEY_AUTH_METHOD.value:
configData = await promptUser(API_KEY_FLOW);
updatedConfig = await updatePortalConfig(configData);
validName = await promptForPortalNameIfNotSet(updatedConfig);
updatedConfig = await updateAccountConfig(configData);
validName = await promptForAccountNameIfNotSet(updatedConfig);

updatePortalConfig({
updateAccountConfig({
...updatedConfig,
environment: updatedConfig.env,
name: validName,
Expand All @@ -115,9 +115,9 @@ exports.handler = async options => {
case PERSONAL_ACCESS_KEY_AUTH_METHOD.value:
configData = await personalAccessKeyPrompt({ env });
updatedConfig = await updateConfigWithPersonalAccessKey(configData);
validName = await promptForPortalNameIfNotSet(updatedConfig);
validName = await promptForAccountNameIfNotSet(updatedConfig);

updatePortalConfig({
updateAccountConfig({
...updatedConfig,
environment: updatedConfig.env,
tokenInfo: updatedConfig.auth.tokenInfo,
Expand Down
4 changes: 2 additions & 2 deletions packages/cms-cli/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { logger } = require('@hubspot/cms-lib/logger');
const { createProject } = require('@hubspot/cms-lib/projects');
const { createFunction } = require('@hubspot/cms-lib/functions');

const { setLogLevel, getPortalId } = require('../lib/commonOpts');
const { setLogLevel, getAccountId } = require('../lib/commonOpts');
const { logDebugInfo } = require('../lib/debugInfo');
const { resolveLocalPath } = require('../lib/filesystem');
const { trackCommandUsage } = require('../lib/usageTracking');
Expand Down Expand Up @@ -223,7 +223,7 @@ exports.handler = async options => {
break;
}

trackCommandUsage('create', commandTrackingContext, getPortalId(options));
trackCommandUsage('create', commandTrackingContext, getAccountId(options));
};

exports.builder = yargs => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cms-cli/commands/customObject.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { addConfigOptions, addPortalOptions } = require('../lib/commonOpts');
const { addConfigOptions, addAccountOptions } = require('../lib/commonOpts');
const schemaCommand = require('./customObject/schema');
const createCommand = require('./customObject/create');
const chalk = require('chalk');
Expand All @@ -9,7 +9,7 @@ exports.describe =

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

yargs
.command(schemaCommand)
Expand Down
14 changes: 7 additions & 7 deletions packages/cms-cli/commands/customObject/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const {
const { logger } = require('@hubspot/cms-lib/logger');
const { logErrorInstance } = require('@hubspot/cms-lib/errorHandlers');
const { getAbsoluteFilePath } = require('@hubspot/cms-lib/path');
const { validatePortal, isFileValidJSON } = require('../../lib/validation');
const { validateAccount, isFileValidJSON } = require('../../lib/validation');
const { trackCommandUsage } = require('../../lib/usageTracking');
const { setLogLevel, getPortalId } = require('../../lib/commonOpts');
const { setLogLevel, getAccountId } = require('../../lib/commonOpts');
const { logDebugInfo } = require('../../lib/debugInfo');
const { batchCreateObjects } = require('@hubspot/cms-lib/api/customObject');

Expand All @@ -23,23 +23,23 @@ exports.handler = async options => {
loadConfig(configPath);
checkAndWarnGitInclusion();

if (!(validateConfig() && (await validatePortal(options)))) {
if (!(validateConfig() && (await validateAccount(options)))) {
process.exit(1);
}
const portalId = getPortalId(options);
const accountId = getAccountId(options);

trackCommandUsage('custom-object-batch-create', null, portalId);
trackCommandUsage('custom-object-batch-create', null, accountId);

const filePath = getAbsoluteFilePath(definition);
if (!isFileValidJSON(filePath)) {
process.exit(1);
}

try {
await batchCreateObjects(portalId, name, filePath);
await batchCreateObjects(accountId, name, filePath);
logger.success(`Objects created`);
} catch (e) {
logErrorInstance(e, { portalId });
logErrorInstance(e, { accountId });
logger.error(`Object creation from ${definition} failed`);
}
};
Expand Down
16 changes: 8 additions & 8 deletions packages/cms-cli/commands/customObject/schema/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const {
const { logger } = require('@hubspot/cms-lib/logger');
const { logErrorInstance } = require('@hubspot/cms-lib/errorHandlers');
const { getAbsoluteFilePath } = require('@hubspot/cms-lib/path');
const { validatePortal, isFileValidJSON } = require('../../../lib/validation');
const { validateAccount, isFileValidJSON } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const {
addTestingOptions,
setLogLevel,
getPortalId,
getAccountId,
} = require('../../../lib/commonOpts');
const { getEnv } = require('@hubspot/cms-lib/lib/config');
const { ENVIRONMENTS } = require('@hubspot/cms-lib/lib/constants');
Expand All @@ -30,27 +30,27 @@ exports.handler = async options => {
loadConfig(configPath);
checkAndWarnGitInclusion();

if (!(validateConfig() && (await validatePortal(options)))) {
if (!(validateConfig() && (await validateAccount(options)))) {
process.exit(1);
}
const portalId = getPortalId(options);
const accountId = getAccountId(options);

trackCommandUsage('custom-object-schema-create', null, portalId);
trackCommandUsage('custom-object-schema-create', null, accountId);

const filePath = getAbsoluteFilePath(definition);
if (!isFileValidJSON(filePath)) {
process.exit(1);
}

try {
const res = await createSchema(portalId, filePath);
const res = await createSchema(accountId, filePath);
logger.success(
`Schema can be viewed at ${getHubSpotWebsiteOrigin(
getEnv() === 'qa' ? ENVIRONMENTS.QA : ENVIRONMENTS.PROD
)}/contacts/${portalId}/objects/${res.objectTypeId}`
)}/contacts/${accountId}/objects/${res.objectTypeId}`
);
} catch (e) {
logErrorInstance(e, { portalId });
logErrorInstance(e, { accountId });
logger.error(`Schema creation from ${definition} failed`);
}
};
Expand Down
12 changes: 6 additions & 6 deletions packages/cms-cli/commands/customObject/schema/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const {
const { logger } = require('@hubspot/cms-lib/logger');
const { logErrorInstance } = require('@hubspot/cms-lib/errorHandlers');

const { validatePortal } = require('../../../lib/validation');
const { validateAccount } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const { setLogLevel, getPortalId } = require('../../../lib/commonOpts');
const { setLogLevel, getAccountId } = require('../../../lib/commonOpts');
const { logDebugInfo } = require('../../../lib/debugInfo');
const { deleteSchema } = require('@hubspot/cms-lib/api/schema');

Expand All @@ -23,15 +23,15 @@ exports.handler = async options => {
loadConfig(options.config);
checkAndWarnGitInclusion();

if (!(validateConfig() && (await validatePortal(options)))) {
if (!(validateConfig() && (await validateAccount(options)))) {
process.exit(1);
}
const portalId = getPortalId(options);
const accountId = getAccountId(options);

trackCommandUsage('custom-object-schema-delete', null, portalId);
trackCommandUsage('custom-object-schema-delete', null, accountId);

try {
await deleteSchema(portalId, name);
await deleteSchema(accountId, name);
logger.success(`Successfully initiated deletion of ${name}`);
} catch (e) {
logErrorInstance(e);
Expand Down
18 changes: 9 additions & 9 deletions packages/cms-cli/commands/customObject/schema/fetch-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ const {
const { logger } = require('@hubspot/cms-lib/logger');
const { logErrorInstance } = require('@hubspot/cms-lib/errorHandlers');

const { validatePortal } = require('../../../lib/validation');
const { validateAccount } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const { setLogLevel, getPortalId } = require('../../../lib/commonOpts');
const { setLogLevel, getAccountId } = require('../../../lib/commonOpts');
const { logDebugInfo } = require('../../../lib/debugInfo');
const { downloadSchemas, getResolvedPath } = require('@hubspot/cms-lib/schema');

exports.command = 'fetch-all [dest]';
exports.describe = 'Fetch all custom object schemas for a portal';
exports.describe = 'Fetch all custom object schemas for a account';

exports.handler = async options => {
setLogLevel(options);
logDebugInfo(options);
loadConfig(options.config);
checkAndWarnGitInclusion();

if (!(validateConfig() && (await validatePortal(options)))) {
if (!(validateConfig() && (await validateAccount(options)))) {
process.exit(1);
}
const portalId = getPortalId(options);
const accountId = getAccountId(options);

trackCommandUsage('custom-object-schema-fetch-all', null, portalId);
trackCommandUsage('custom-object-schema-fetch-all', null, accountId);

try {
await downloadSchemas(portalId, options.dest);
await downloadSchemas(accountId, options.dest);
logger.success(`Saved schemas to ${getResolvedPath(options.dest)}`);
} catch (e) {
logErrorInstance(e);
Expand All @@ -41,11 +41,11 @@ exports.builder = yargs => {
yargs.example([
[
'$0 custom-object schema fetch-all',
'Fetch all schemas for a portal and put them in the current working directory',
'Fetch all schemas for a account and put them in the current working directory',
],
[
'$0 custom-object schema fetch-all my/folder',
'Fetch all schemas for a portal and put them in a directory named my/folder',
'Fetch all schemas for a account and put them in a directory named my/folder',
],
]);

Expand Down
Loading

0 comments on commit fd031f0

Please sign in to comment.