-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathclear.ts
88 lines (80 loc) · 2.35 KB
/
clear.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
import { Argv, ArgumentsCamelCase } from 'yargs';
import { logger } from '@hubspot/local-dev-lib/logger';
import { logError } from '../../lib/errorHandlers/index';
import { clearHubDbTableRows } from '@hubspot/local-dev-lib/hubdb';
import { publishTable } from '@hubspot/local-dev-lib/api/hubdb';
import { selectHubDBTablePrompt } from '../../lib/prompts/selectHubDBTablePrompt';
import { trackCommandUsage } from '../../lib/usageTracking';
import {
addConfigOptions,
addAccountOptions,
addUseEnvironmentOptions,
} from '../../lib/commonOpts';
import { i18n } from '../../lib/lang';
import {
CommonArgs,
ConfigArgs,
AccountArgs,
EnvironmentArgs,
} from '../../types/Yargs';
export const command = 'clear [table-id]';
export const describe = i18n('commands.hubdb.subcommands.clear.describe');
type CombinedArgs = ConfigArgs & AccountArgs & EnvironmentArgs;
type HubdbClearArgs = CommonArgs &
CombinedArgs & { tableId?: number; dest?: string };
export async function handler(
args: ArgumentsCamelCase<HubdbClearArgs>
): Promise<void> {
const { derivedAccountId } = args;
trackCommandUsage('hubdb-clear', {}, derivedAccountId);
try {
const { tableId } =
'tableId' in args
? args
: await selectHubDBTablePrompt({
accountId: derivedAccountId,
options: args,
});
const { deletedRowCount } = await clearHubDbTableRows(
derivedAccountId,
tableId
);
if (deletedRowCount > 0) {
logger.log(
i18n('commands.hubdb.subcommands.clear.logs.removedRows', {
deletedRowCount,
tableId,
})
);
const {
data: { rowCount },
} = await publishTable(derivedAccountId, tableId);
logger.log(
i18n('commands.hubdb.subcommands.clear.logs.rowCount', {
rowCount,
tableId,
})
);
} else {
logger.log(
i18n('commands.hubdb.subcommands.clear.logs.emptyTable', {
tableId,
})
);
}
} catch (e) {
logError(e);
}
}
export function builder(yargs: Argv): Argv<HubdbClearArgs> {
addAccountOptions(yargs);
addConfigOptions(yargs);
addUseEnvironmentOptions(yargs);
yargs.positional('table-id', {
describe: i18n(
'commands.hubdb.subcommands.clear.positionals.tableId.describe'
),
type: 'string',
});
return yargs as Argv<HubdbClearArgs>;
}