@@ -10,18 +10,31 @@ const { TwilioCliError } = require('@twilio/cli-core').services.error;
10
10
11
11
const { couldNotGetEnvironment } = require ( './errorMessages' ) ;
12
12
13
- const DEFAULT_ASSET_SERVICE_NAME = 'CLI-Assets-Bucket' ;
14
-
15
- async function createServiceAndEnvironment ( client ) {
16
- const serviceSid = await createService ( DEFAULT_ASSET_SERVICE_NAME , client ) ;
13
+ async function createServiceAndEnvironment ( client , serviceName ) {
14
+ const serviceSid = await createService ( serviceName , client ) ;
17
15
const environment = await createEnvironmentFromSuffix ( '' , serviceSid , client ) ;
18
16
return {
19
17
serviceSid,
20
18
environment,
21
19
} ;
22
20
}
23
21
24
- async function init ( { apiKey, apiSecret, accountSid, pluginConfig, logger } ) {
22
+ function validateServiceName ( serviceName ) {
23
+ if ( ! serviceName . match ( / ^ (? = .* $ ) [ a - z A - Z 0 - 9 ] + (?: - [ a - z A - Z 0 - 9 ] + ) * $ / ) ) {
24
+ throw new TwilioCliError (
25
+ `Service name may only contain alphanumeric characters and hyphens.`
26
+ ) ;
27
+ }
28
+ }
29
+
30
+ async function init ( {
31
+ apiKey,
32
+ apiSecret,
33
+ accountSid,
34
+ pluginConfig,
35
+ logger,
36
+ serviceName,
37
+ } ) {
25
38
logger . debug ( 'Loading config' ) ;
26
39
const client = new TwilioServerlessApiClient ( {
27
40
username : apiKey ,
@@ -53,7 +66,11 @@ async function init({ apiKey, apiSecret, accountSid, pluginConfig, logger }) {
53
66
} else {
54
67
try {
55
68
logger . debug ( 'Creating new assets service and environment' ) ;
56
- const serviceAndEnvironment = await createServiceAndEnvironment ( client ) ;
69
+ validateServiceName ( serviceName ) ;
70
+ const serviceAndEnvironment = await createServiceAndEnvironment (
71
+ client ,
72
+ serviceName
73
+ ) ;
57
74
config [ accountSid ] = {
58
75
serviceSid : serviceAndEnvironment . serviceSid ,
59
76
environmentSid : serviceAndEnvironment . environment . sid ,
@@ -62,9 +79,13 @@ async function init({ apiKey, apiSecret, accountSid, pluginConfig, logger }) {
62
79
return serviceAndEnvironment . environment ;
63
80
} catch ( error ) {
64
81
logger . debug ( error . toString ( ) ) ;
65
- throw new TwilioCliError (
66
- `Could not create a new asset service for account ${ accountSid } `
67
- ) ;
82
+ if ( error . name === 'TwilioCliError' ) {
83
+ throw error ;
84
+ } else {
85
+ throw new TwilioCliError (
86
+ `Could not create a new asset service for account ${ accountSid } `
87
+ ) ;
88
+ }
68
89
}
69
90
}
70
91
}
0 commit comments