-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcreate.ts
251 lines (226 loc) · 7.5 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { Argv, ArgumentsCamelCase } from 'yargs';
import { getAccountConfig, getEnv } from '@hubspot/local-dev-lib/config';
import { logger } from '@hubspot/local-dev-lib/logger';
import { isMissingScopeError } from '@hubspot/local-dev-lib/errors/index';
import { getHubSpotWebsiteOrigin } from '@hubspot/local-dev-lib/urls';
import {
HUBSPOT_ACCOUNT_TYPES,
HUBSPOT_ACCOUNT_TYPE_STRINGS,
} from '@hubspot/local-dev-lib/constants/config';
import { getValidEnv } from '@hubspot/local-dev-lib/environment';
import {
addAccountOptions,
addConfigOptions,
addUseEnvironmentOptions,
addTestingOptions,
} from '../../lib/commonOpts';
import { i18n } from '../../lib/lang';
import { EXIT_CODES } from '../../lib/enums/exitCodes';
import {
uiFeatureHighlight,
uiBetaTag,
uiCommandReference,
} from '../../lib/ui';
import {
SANDBOX_TYPE_MAP,
getAvailableSyncTypes,
SYNC_TYPES,
validateSandboxUsageLimits,
} from '../../lib/sandboxes';
import { trackCommandUsage } from '../../lib/usageTracking';
import { sandboxTypePrompt } from '../../lib/prompts/sandboxesPrompt';
import { promptUser } from '../../lib/prompts/promptUtils';
import { syncSandbox } from '../../lib/sandboxSync';
import { logError } from '../../lib/errorHandlers/index';
import { buildSandbox } from '../../lib/buildAccount';
import { hubspotAccountNamePrompt } from '../../lib/prompts/accountNamePrompt';
import {
CommonArgs,
ConfigArgs,
AccountArgs,
EnvironmentArgs,
TestingArgs,
} from '../../types/Yargs';
import { SandboxSyncTask } from '../../types/Sandboxes';
export const command = 'create';
export const describe = uiBetaTag(i18n(`commands.sandbox.subcommands.create.describe`), false);
type CombinedArgs = ConfigArgs & AccountArgs & EnvironmentArgs & TestingArgs;
type SandboxCreateArgs = CommonArgs &
CombinedArgs & {
name?: string;
force?: boolean;
type?: string;
};
export async function handler(
args: ArgumentsCamelCase<SandboxCreateArgs>
): Promise<void> {
const { name, type, force, derivedAccountId } = args;
const accountConfig = getAccountConfig(derivedAccountId);
const env = getValidEnv(getEnv(derivedAccountId));
trackCommandUsage('sandbox-create', {}, derivedAccountId);
// Check if account config exists
if (!accountConfig) {
logger.error(
i18n(`commands.sandbox.subcommands.create.failure.noAccountConfig`, {
accountId: derivedAccountId,
authCommand: uiCommandReference('hs auth'),
})
);
process.exit(EXIT_CODES.ERROR);
}
// Default account is not a production portal
if (
accountConfig.accountType &&
accountConfig.accountType !== HUBSPOT_ACCOUNT_TYPES.STANDARD
) {
logger.error(
i18n(`commands.sandbox.subcommands.create.failure.invalidAccountType`, {
accountType:
HUBSPOT_ACCOUNT_TYPE_STRINGS[
HUBSPOT_ACCOUNT_TYPES[accountConfig.accountType]
],
accountName: accountConfig.name || '',
})
);
process.exit(EXIT_CODES.ERROR);
}
let typePrompt;
let namePrompt;
if ((type && !(type.toLowerCase() in SANDBOX_TYPE_MAP)) || !type) {
if (!force) {
typePrompt = await sandboxTypePrompt();
} else {
logger.error(i18n(`commands.sandbox.subcommands.create.failure.optionMissing.type`));
process.exit(EXIT_CODES.ERROR);
}
}
const sandboxType = type
? SANDBOX_TYPE_MAP[type.toLowerCase()]
: typePrompt!.type;
// Check usage limits and exit if parent portal has no available sandboxes for the selected type
try {
await validateSandboxUsageLimits(accountConfig, sandboxType, env);
} catch (err) {
if (isMissingScopeError(err)) {
logger.error(
i18n('lib.sandbox.create.failure.scopes.message', {
accountName: accountConfig.name || derivedAccountId,
})
);
const websiteOrigin = getHubSpotWebsiteOrigin(env);
const url = `${websiteOrigin}/personal-access-key/${derivedAccountId}`;
logger.info(
i18n('lib.sandbox.create.failure.scopes.instructions', {
accountName: accountConfig.name || derivedAccountId,
url,
})
);
} else {
logError(err);
}
process.exit(EXIT_CODES.ERROR);
}
if (!name) {
if (!force) {
namePrompt = await hubspotAccountNamePrompt({ accountType: sandboxType });
} else {
logger.error(i18n(`commands.sandbox.subcommands.create.failure.optionMissing.name`));
process.exit(EXIT_CODES.ERROR);
}
}
const sandboxName = name || namePrompt!.name;
let contactRecordsSyncPromptResult = false;
if (!force) {
const isStandardSandbox =
sandboxType === HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX;
// Prompt to sync contact records for standard sandboxes only
if (isStandardSandbox) {
const { contactRecordsSyncPrompt } = await promptUser([
{
name: 'contactRecordsSyncPrompt',
type: 'confirm',
message: i18n('lib.sandbox.sync.confirm.syncContactRecords.standard'),
},
]);
contactRecordsSyncPromptResult = contactRecordsSyncPrompt;
}
}
try {
const result = await buildSandbox(
sandboxName,
accountConfig,
sandboxType,
env,
force
);
const sandboxAccountConfig = getAccountConfig(result.sandbox.sandboxHubId);
// Check if sandbox account config exists
if (!sandboxAccountConfig) {
logger.error(
i18n(`commands.sandbox.subcommands.create.failure.noSandboxAccountConfig`, {
accountId: result.sandbox.sandboxHubId,
authCommand: uiCommandReference('hs auth'),
})
);
process.exit(EXIT_CODES.ERROR);
}
// For v1 sandboxes, keep sync here. Once we migrate to v2, this will be handled by BE automatically
async function handleSyncSandbox(
syncTasks: SandboxSyncTask[]
): Promise<void> {
await syncSandbox(sandboxAccountConfig!, accountConfig!, env, syncTasks);
}
try {
let availableSyncTasks = await getAvailableSyncTypes(
accountConfig,
sandboxAccountConfig
);
if (!contactRecordsSyncPromptResult) {
availableSyncTasks = availableSyncTasks.filter(
t => t.type !== SYNC_TYPES.OBJECT_RECORDS
);
}
await handleSyncSandbox(availableSyncTasks);
} catch (err) {
logError(err);
throw err;
}
const highlightItems = ['accountsUseCommand', 'projectCreateCommand'];
if (sandboxType === HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX) {
highlightItems.push('projectDevCommand');
} else {
highlightItems.push('projectUploadCommand');
}
uiFeatureHighlight(highlightItems);
process.exit(EXIT_CODES.SUCCESS);
} catch (error) {
// Errors are logged in util functions
process.exit(EXIT_CODES.ERROR);
}
}
export function builder(yargs: Argv): Argv<SandboxCreateArgs> {
yargs.option('force', {
type: 'boolean',
alias: 'f',
describe: i18n(`commands.sandbox.subcommands.create.options.force.describe`),
});
yargs.option('name', {
describe: i18n(`commands.sandbox.subcommands.create.options.name.describe`),
type: 'string',
});
yargs.option('type', {
describe: i18n(`commands.sandbox.subcommands.create.options.type.describe`),
choices: Object.keys(SANDBOX_TYPE_MAP),
});
yargs.example([
[
'$0 sandbox create --name=MySandboxAccount --type=STANDARD',
i18n(`commands.sandbox.subcommands.create.examples.default`),
],
]);
addConfigOptions(yargs);
addAccountOptions(yargs);
addUseEnvironmentOptions(yargs);
addTestingOptions(yargs);
return yargs as Argv<SandboxCreateArgs>;
}