-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathdelete.ts
94 lines (82 loc) · 2.59 KB
/
delete.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Argv, ArgumentsCamelCase } from 'yargs';
import { logger } from '@hubspot/local-dev-lib/logger';
import { logError } from '../../lib/errorHandlers/index';
import { deleteTable } from '@hubspot/local-dev-lib/api/hubdb';
import { trackCommandUsage } from '../../lib/usageTracking';
import {
addConfigOptions,
addAccountOptions,
addUseEnvironmentOptions,
} from '../../lib/commonOpts';
import { selectHubDBTablePrompt } from '../../lib/prompts/selectHubDBTablePrompt';
import { promptUser } from '../../lib/prompts/promptUtils';
import { EXIT_CODES } from '../../lib/enums/exitCodes';
import { i18n } from '../../lib/lang';
import {
CommonArgs,
ConfigArgs,
AccountArgs,
EnvironmentArgs,
} from '../../types/Yargs';
export const command = 'delete [table-id]';
export const describe = i18n('commands.hubdb.subcommands.delete.describe');
type CombinedArgs = ConfigArgs & AccountArgs & EnvironmentArgs;
type HubdbDeleteArgs = CommonArgs & CombinedArgs & { tableId?: number };
export async function handler(
args: ArgumentsCamelCase<HubdbDeleteArgs>
): Promise<void> {
const { force, derivedAccountId } = args;
trackCommandUsage('hubdb-delete', {}, derivedAccountId);
try {
const { tableId } =
'tableId' in args && args.tableId
? args
: await selectHubDBTablePrompt({
accountId: derivedAccountId,
options: args,
});
if (!force) {
const { shouldDeleteTable } = await promptUser({
name: 'shouldDeleteTable',
type: 'confirm',
message: i18n('commands.hubdb.subcommands.delete.shouldDeleteTable', {
tableId,
}),
});
if (!shouldDeleteTable) {
process.exit(EXIT_CODES.SUCCESS);
}
}
await deleteTable(derivedAccountId, tableId);
logger.success(
i18n('commands.hubdb.subcommands.delete.success.delete', {
accountId: derivedAccountId,
tableId,
})
);
process.exit(EXIT_CODES.SUCCESS);
} catch (e) {
logger.error(
i18n('commands.hubdb.subcommands.delete.errors.delete', {
tableId: args.tableId || '',
})
);
logError(e);
}
}
export function builder(yargs: Argv): Argv<HubdbDeleteArgs> {
addAccountOptions(yargs);
addConfigOptions(yargs);
addUseEnvironmentOptions(yargs);
yargs.positional('table-id', {
describe: i18n(
'commands.hubdb.subcommands.delete.positionals.tableId.describe'
),
type: 'string',
});
yargs.option('force', {
describe: i18n('commands.hubdb.subcommands.delete.options.force.describe'),
type: 'boolean',
});
return yargs as Argv<HubdbDeleteArgs>;
}