-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcreate.ts
103 lines (93 loc) · 2.93 KB
/
create.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
95
96
97
98
99
100
101
102
103
import { Argv, ArgumentsCamelCase } from 'yargs';
import path from 'path';
import { logger } from '@hubspot/local-dev-lib/logger';
import { logError } from '../../lib/errorHandlers/index';
import { getCwd, untildify, isValidPath } from '@hubspot/local-dev-lib/path';
import { createHubDbTable } from '@hubspot/local-dev-lib/hubdb';
import { promptUser } from '../../lib/prompts/promptUtils';
import { checkAndConvertToJson } from '../../lib/validation';
import { trackCommandUsage } from '../../lib/usageTracking';
import {
addConfigOptions,
addAccountOptions,
addUseEnvironmentOptions,
} from '../../lib/commonOpts';
import { i18n } from '../../lib/lang';
import { EXIT_CODES } from '../../lib/enums/exitCodes';
import {
CommonArgs,
ConfigArgs,
AccountArgs,
EnvironmentArgs,
} from '../../types/Yargs';
export const command = 'create';
export const describe = i18n(`commands.hubdb.subcommands.create.describe`);
type CombinedArgs = ConfigArgs & AccountArgs & EnvironmentArgs;
type HubdbCreateArgs = CommonArgs & CombinedArgs & { path?: string };
function selectPathPrompt(options: HubdbCreateArgs): Promise<{ path: string }> {
return promptUser([
{
name: 'path',
message: i18n(`commands.hubdb.subcommands.create.enterPath`),
when: !options.path,
validate: (input: string) => {
if (!input) {
return i18n(`commands.hubdb.subcommands.create.errors.pathRequired`);
}
if (!isValidPath(input)) {
return i18n(
`commands.hubdb.subcommands.create.errors.invalidCharacters`
);
}
return true;
},
filter: (input: string) => {
return untildify(input);
},
},
]);
}
export async function handler(
args: ArgumentsCamelCase<HubdbCreateArgs>
): Promise<void> {
const { derivedAccountId } = args;
trackCommandUsage('hubdb-create', {}, derivedAccountId);
let filePath;
try {
filePath =
'path' in args && args.path
? path.resolve(getCwd(), args.path)
: path.resolve(getCwd(), (await selectPathPrompt(args)).path);
if (!checkAndConvertToJson(filePath)) {
process.exit(EXIT_CODES.ERROR);
}
const table = await createHubDbTable(
derivedAccountId,
path.resolve(getCwd(), filePath)
);
logger.success(
i18n(`commands.hubdb.subcommands.create.success.create`, {
accountId: derivedAccountId,
rowCount: table.rowCount,
tableId: table.tableId,
})
);
} catch (e) {
logger.error(
i18n(`commands.hubdb.subcommands.create.errors.create`, {
filePath: filePath || '',
})
);
logError(e);
}
}
export function builder(yargs: Argv): Argv<HubdbCreateArgs> {
addAccountOptions(yargs);
addConfigOptions(yargs);
addUseEnvironmentOptions(yargs);
yargs.options('path', {
describe: i18n(`commands.hubdb.subcommands.create.options.path.describe`),
type: 'string',
});
return yargs as Argv<HubdbCreateArgs>;
}