Skip to content

Commit

Permalink
Refactor delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
kemmerle committed Nov 22, 2024
1 parent 03fa05b commit 1ad3821
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
46 changes: 36 additions & 10 deletions commands/hubdb/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,57 @@ const { logError } = require('../../lib/errorHandlers/index');
const { deleteTable } = require('@hubspot/local-dev-lib/api/hubdb');
const { loadAndValidateOptions } = require('../../lib/validation');
const { trackCommandUsage } = require('../../lib/usageTracking');

const { addUseEnvironmentOptions } = require('../../lib/commonOpts');
const {
addConfigOptions,
addAccountOptions,
addUseEnvironmentOptions,
} = require('../../lib/commonOpts');
selectHubDBTablePrompt,
} = require('../../lib/prompts/selectHubDBTablePrompt');
const { promptUser } = require('../../lib/prompts/promptUtils');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const { i18n } = require('../../lib/lang');

const i18nKey = 'commands.hubdb.subcommands.delete';

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

exports.handler = async options => {
const { tableId, derivedAccountId } = options;
const { force, derivedAccountId } = options;

await loadAndValidateOptions(options);

trackCommandUsage('hubdb-delete', null, derivedAccountId);

try {
const { tableId } =
'tableId' in options
? options
: await selectHubDBTablePrompt({
accountId: derivedAccountId,
options,
isDeleteCommand: true,
});

if (!force) {
const { shouldDeleteTable } = await promptUser({
name: 'shouldDeleteTable',
type: 'confirm',
message: i18n(`${i18nKey}.shouldDeleteTable`, { tableId }),
});
process.stdin.resume();

if (!shouldDeleteTable) {
process.exit(EXIT_CODES.SUCCESS);
}
}

await deleteTable(derivedAccountId, tableId);
logger.success(
i18n(`${i18nKey}.success.delete`, {
accountId: derivedAccountId,
tableId,
})
);
process.exit(EXIT_CODES.SUCCESS);
} catch (e) {
logger.error(
i18n(`${i18nKey}.errors.delete`, {
Expand All @@ -43,12 +66,15 @@ exports.handler = async options => {
};

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

yargs.positional('tableId', {
yargs.positional('table-id', {
describe: i18n(`${i18nKey}.positionals.tableId.describe`),
type: 'string',
});

yargs.option('force', {
describe: i18n(`${i18nKey}.options.force.describe`),
type: 'boolean',
});
};
6 changes: 5 additions & 1 deletion lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,16 @@ en:
success:
create: "The table {{ tableId }} was created in {{ accountId }} with {{ rowCount }} rows"
delete:
describe: "Delete a HubDB table"
describe: "Delete a HubDB table."
shouldDeleteTable: "Proceed with deleting HubDB table {{ tableId }}?"
errors:
delete: "Deleting the table {{ tableId }} failed"
positionals:
tableId:
describe: "HubDB Table ID"
options:
force:
describe: "Skips confirmation prompt when deleting a HubDB table"
success:
delete: "The table {{ tableId }} was deleted from {{ accountId }}"
fetch:
Expand Down
4 changes: 3 additions & 1 deletion lib/prompts/selectHubDBTablePrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ export async function selectHubDBTablePrompt({
accountId,
options,
skipDestPrompt = true,
isDeleteCommand = false,
}: {
accountId: number;
options: {
tableId?: number;
dest?: string;
};
skipDestPrompt?: boolean;
isDeleteCommand?: boolean;
}) {
const hubdbTables: Table[] = (await fetchHubDBOptions(accountId)) || [];
const id = options.tableId?.toString();
Expand All @@ -55,7 +57,7 @@ export async function selectHubDBTablePrompt({
when: !id && !isValidTable,
type: 'list',
choices: hubdbTables.map(table => {
if (table.rowCount === 0) {
if (table.rowCount === 0 && !isDeleteCommand) {
return {
name: `${table.label} (${table.id})`,
disabled: i18n(`${i18nKey}.errors.tableEmpty`),
Expand Down

0 comments on commit 1ad3821

Please sign in to comment.