Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Restructure custom-object commands to match design guidelines #1261

Merged
merged 8 commits into from
Nov 26, 2024
6 changes: 1 addition & 5 deletions commands/customObject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-nocheck
const {
addConfigOptions,
addAccountOptions,
addGlobalOptions,
} = require('../lib/commonOpts');
const schemaCommand = require('./customObject/schema');
Expand All @@ -12,7 +10,7 @@ const { uiBetaTag, uiLink } = require('../lib/ui');

const i18nKey = 'commands.customObject';

exports.command = ['custom-object', 'custom', 'co'];
exports.command = ['custom-object', 'custom-objects', 'co'];
exports.describe = uiBetaTag(i18n(`${i18nKey}.describe`), false);

const logBetaMessage = () => {
Expand All @@ -27,8 +25,6 @@ const logBetaMessage = () => {
};

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

yargs
Expand Down
36 changes: 23 additions & 13 deletions commands/customObject/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-nocheck
import { inputPrompt } from '../../lib/prompts/promptUtils';

const { logger } = require('@hubspot/local-dev-lib/logger');
const { logError } = require('../../lib/errorHandlers/index');
const { getAbsoluteFilePath } = require('@hubspot/local-dev-lib/path');
Expand All @@ -15,44 +17,52 @@ const { i18n } = require('../../lib/lang');
const i18nKey = 'commands.customObject.subcommands.create';
const { EXIT_CODES } = require('../../lib/enums/exitCodes');

exports.command = 'create <name> <definition>';
exports.command = 'create [name]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
const { definition, name, derivedAccountId } = options;
const { path, name: providedName, derivedAccountId } = options;
let definitionPath = path;

await loadAndValidateOptions(options);

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

const filePath = getAbsoluteFilePath(definition);
if (!definitionPath) {
definitionPath = await inputPrompt(i18n(`${i18nKey}.inputPath`));
}

const filePath = getAbsoluteFilePath(definitionPath);
const objectJson = checkAndConvertToJson(filePath);

if (!objectJson) {
process.exit(EXIT_CODES.ERROR);
}

const name =
providedName || (await inputPrompt(i18n(`${i18nKey}.inputSchema`)));

try {
await batchCreateObjects(derivedAccountId, name, objectJson);
logger.success(i18n(`${i18nKey}.success.objectsCreated`));
} catch (e) {
logError(e, { accountId: derivedAccountId });
logger.error(
i18n(`${i18nKey}.errors.creationFailed`, {
definition,
definition: definitionPath,
})
);
}
};

exports.builder = yargs => {
yargs.positional('name', {
describe: i18n(`${i18nKey}.positionals.name.describe`),
type: 'string',
});

yargs.positional('definition', {
describe: i18n(`${i18nKey}.positionals.definition.describe`),
type: 'string',
});
yargs
.positional('name', {
describe: i18n(`${i18nKey}.positionals.name.describe`),
type: 'string',
})
.option('path', {
joe-yeager marked this conversation as resolved.
Show resolved Hide resolved
describe: i18n(`${i18nKey}.options.path.describe`),
type: 'string',
});
};
2 changes: 1 addition & 1 deletion commands/customObject/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { i18n } = require('../../lib/lang');

const i18nKey = 'commands.customObject.subcommands.schema';

exports.command = 'schema';
exports.command = ['schema', 'schemas'];
exports.describe = i18n(`${i18nKey}.describe`);
exports.builder = yargs => {
yargs
Expand Down
13 changes: 7 additions & 6 deletions commands/customObject/schema/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ const { i18n } = require('../../../lib/lang');
const i18nKey = 'commands.customObject.subcommands.schema.subcommands.create';
const { EXIT_CODES } = require('../../../lib/enums/exitCodes');

exports.command = 'create <definition>';
exports.command = 'create';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
const { definition, derivedAccountId } = options;
const { path, derivedAccountId } = options;

await loadAndValidateOptions(options);

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

const filePath = getAbsoluteFilePath(definition);
const filePath = getAbsoluteFilePath(path);
const schemaJson = checkAndConvertToJson(filePath);
if (!schemaJson) {
process.exit(EXIT_CODES.ERROR);
Expand Down Expand Up @@ -66,7 +66,7 @@ exports.handler = async options => {
logError(e, { accountId: derivedAccountId });
logger.error(
i18n(`${i18nKey}.errors.creationFailed`, {
definition,
definition: path,
})
);
}
Expand All @@ -75,8 +75,9 @@ exports.handler = async options => {
exports.builder = yargs => {
addTestingOptions(yargs);

yargs.positional('definition', {
describe: i18n(`${i18nKey}.positionals.definition.describe`),
yargs.option('path', {
describe: i18n(`${i18nKey}.options.definition.describe`),
type: 'string',
required: true,
});
};
48 changes: 38 additions & 10 deletions commands/customObject/schema/delete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// @ts-nocheck
import { EXIT_CODES } from '../../../lib/enums/exitCodes';
import { confirmPrompt, listPrompt } from '../../../lib/prompts/promptUtils';
import { fetchObjectSchemas } from '@hubspot/local-dev-lib/api/customObjects';

const { logger } = require('@hubspot/local-dev-lib/logger');
const { loadAndValidateOptions } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
Expand All @@ -10,17 +14,37 @@ const { logError } = require('../../../lib/errorHandlers');

const i18nKey = 'commands.customObject.subcommands.schema.subcommands.delete';

exports.command = 'delete <name>';
exports.command = 'delete [name]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
const { name, derivedAccountId } = options;
const { name: providedName, force, derivedAccountId } = options;

await loadAndValidateOptions(options);

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

let name;
try {
const {
data: { results },
} = await fetchObjectSchemas(derivedAccountId);
const schemaNames = results?.map(({ name: schemaName }) => schemaName);
name =
providedName ||
(await listPrompt(i18n(`${i18nKey}.selectSchema`), {
choices: schemaNames,
}));

const shouldDelete =
force ||
(await confirmPrompt(i18n(`${i18nKey}.confirmDelete`, { name })));

if (!shouldDelete) {
logger.info(i18n(`${i18nKey}.deleteCancelled`, { name }));
return process.exit(EXIT_CODES.SUCCESS);
}

await deleteObjectSchema(derivedAccountId, name);
logger.success(
i18n(`${i18nKey}.success.delete`, {
Expand All @@ -38,12 +62,16 @@ exports.handler = async options => {
};

exports.builder = yargs => {
yargs.example([
['$0 schema delete schemaName', i18n(`${i18nKey}.examples.default`)],
]);

yargs.positional('name', {
describe: i18n(`${i18nKey}.positionals.name.describe`),
type: 'string',
});
yargs
.example([
['$0 schema delete schemaName', i18n(`${i18nKey}.examples.default`)],
])
.positional('name', {
describe: i18n(`${i18nKey}.positionals.name.describe`),
type: 'string',
})
.option('force', {
describe: i18n(`${i18nKey}.options.force.describe`),
type: 'boolean',
});
};
37 changes: 22 additions & 15 deletions commands/customObject/schema/fetch-all.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-nocheck
import { inputPrompt } from '../../../lib/prompts/promptUtils';

const { logger } = require('@hubspot/local-dev-lib/logger');
const { loadAndValidateOptions } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
Expand All @@ -18,16 +20,18 @@ exports.describe = i18n(`${i18nKey}.describe`);
exports.handler = async options => {
await loadAndValidateOptions(options);

const { derivedAccountId } = options;
const { derivedAccountId, dest: providedDest } = options;

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

try {
const schemas = await downloadSchemas(derivedAccountId, options.dest);
const dest =
providedDest || (await inputPrompt(i18n(`${i18nKey}.inputDest`)));
const schemas = await downloadSchemas(derivedAccountId, dest);
logSchemas(schemas);
logger.success(
i18n(`${i18nKey}.success.fetch`, {
path: getResolvedPath(options.dest),
path: getResolvedPath(dest),
})
);
} catch (e) {
Expand All @@ -37,16 +41,19 @@ exports.handler = async options => {
};

exports.builder = yargs => {
yargs.example([
['$0 custom-object schema fetch-all', i18n(`${i18nKey}.examples.default`)],
[
'$0 custom-object schema fetch-all my/folder',
i18n(`${i18nKey}.examples.specifyPath`),
],
]);

yargs.positional('dest', {
describe: i18n(`${i18nKey}.positionals.dest.describe`),
type: 'string',
});
yargs
.example([
[
'$0 custom-object schema fetch-all',
i18n(`${i18nKey}.examples.default`),
],
[
'$0 custom-object schema fetch-all my/folder',
i18n(`${i18nKey}.examples.specifyPath`),
],
])
.positional('dest', {
describe: i18n(`${i18nKey}.positionals.dest.describe`),
type: 'string',
});
};
61 changes: 39 additions & 22 deletions commands/customObject/schema/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// @ts-nocheck
import { inputPrompt, listPrompt } from '../../../lib/prompts/promptUtils';
import { fetchObjectSchemas } from '@hubspot/local-dev-lib/api/customObjects';

const path = require('path');
const { isConfigFlagEnabled } = require('@hubspot/local-dev-lib/config');
const { logger } = require('@hubspot/local-dev-lib/logger');
Expand All @@ -17,17 +20,32 @@ const { logError } = require('../../../lib/errorHandlers');

const i18nKey = 'commands.customObject.subcommands.schema.subcommands.fetch';

exports.command = 'fetch <name> [dest]';
exports.command = 'fetch [name] [dest]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
const { name, dest, derivedAccountId } = options;
const { name: providedName, dest: providedDest, derivedAccountId } = options;

await loadAndValidateOptions(options);

trackCommandUsage('custom-object-schema-fetch', null, derivedAccountId);
let name;

try {
const {
data: { results },
} = await fetchObjectSchemas(derivedAccountId);
const schemaNames = results?.map(({ name: schemaName }) => schemaName);

name =
providedName ||
(await listPrompt(i18n(`${i18nKey}.selectSchema`), {
choices: schemaNames,
}));

const dest =
providedDest || (await inputPrompt(i18n(`${i18nKey}.inputDest`)));

if (isConfigFlagEnabled(CONFIG_FLAGS.USE_CUSTOM_OBJECT_HUBFILE)) {
const fullpath = path.resolve(getCwd(), dest);
await fetchSchema(derivedAccountId, name, fullpath);
Expand Down Expand Up @@ -56,24 +74,23 @@ exports.handler = async options => {
};

exports.builder = yargs => {
yargs.example([
[
'$0 custom-object schema fetch schemaName',
i18n(`${i18nKey}.examples.default`),
],
[
'$0 custom-object schema fetch schemaName my/folder',
i18n(`${i18nKey}.examples.specifyPath`),
],
]);

yargs.positional('name', {
describe: i18n(`${i18nKey}.positionals.name.describe`),
type: 'string',
});

yargs.positional('dest', {
describe: i18n(`${i18nKey}.positionals.dest.describe`),
type: 'string',
});
yargs
.example([
[
'$0 custom-object schema fetch schemaName',
i18n(`${i18nKey}.examples.default`),
],
[
'$0 custom-object schema fetch schemaName my/folder',
i18n(`${i18nKey}.examples.specifyPath`),
],
])
.positional('name', {
describe: i18n(`${i18nKey}.positionals.name.describe`),
type: 'string',
})
.positional('dest', {
describe: i18n(`${i18nKey}.positionals.dest.describe`),
type: 'string',
});
};
Loading