-
Notifications
You must be signed in to change notification settings - Fork 55
/
validators.ts
66 lines (57 loc) · 1.74 KB
/
validators.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
import { ValidationError } from 'flex-dev-utils/dist/errors';
import { prompt } from 'flex-dev-utils/dist/inquirer';
import { logger } from 'flex-dev-utils';
import { isSidOfType, SidPrefix } from 'flex-dev-utils/dist/sids';
import {
isValidPluginName,
isValidUrl,
validateAccountSid,
validateGitHubUrl,
} from 'flex-dev-utils/dist/validators';
import { FlexPluginArguments } from '../lib/create-flex-plugin';
/**
* Prompts the user to enter AccountSid
* @private
*/
export const _promptForAccountSid = async (): Promise<string> => {
return await prompt({
type: 'input',
name: 'accountSid',
message: 'Twilio Flex Account SID',
validate: validateAccountSid,
});
};
/**
* Prompts the user to enter template URL
* @private
*/
export const _promptForTemplateUrl = async (): Promise<string> => {
return await prompt({
type: 'input',
name: 'template',
message: 'Template URL',
validate: validateGitHubUrl,
});
};
/**
* Further validates the configuration
*
* @param config {FlexPluginArguments} the configuration
* @return {Promise<FlexPluginArguments>}
*/
const validate = async (config: FlexPluginArguments): Promise<FlexPluginArguments> => {
config.name = config.name || '';
if (!isValidPluginName(config.name)) {
const coloredName = logger.coloredStrings.name;
const msg = `Invalid plugin name ${coloredName(config.name)}; plugin name must start with plugin-`;
throw new ValidationError(msg);
}
if (config.accountSid && !isSidOfType(config.accountSid, SidPrefix.AccountSid)) {
config.accountSid = await _promptForAccountSid();
}
if (config.template && !isValidUrl(config.template)) {
config.template = await _promptForTemplateUrl();
}
return config;
};
export default validate;