@@ -10,18 +10,31 @@ const { TwilioCliError } = require('@twilio/cli-core').services.error;
1010
1111const { couldNotGetEnvironment } = require ( './errorMessages' ) ;
1212
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 ) ;
1715 const environment = await createEnvironmentFromSuffix ( '' , serviceSid , client ) ;
1816 return {
1917 serviceSid,
2018 environment,
2119 } ;
2220}
2321
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+ } ) {
2538 logger . debug ( 'Loading config' ) ;
2639 const client = new TwilioServerlessApiClient ( {
2740 username : apiKey ,
@@ -53,7 +66,11 @@ async function init({ apiKey, apiSecret, accountSid, pluginConfig, logger }) {
5366 } else {
5467 try {
5568 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+ ) ;
5774 config [ accountSid ] = {
5875 serviceSid : serviceAndEnvironment . serviceSid ,
5976 environmentSid : serviceAndEnvironment . environment . sid ,
@@ -62,9 +79,13 @@ async function init({ apiKey, apiSecret, accountSid, pluginConfig, logger }) {
6279 return serviceAndEnvironment . environment ;
6380 } catch ( error ) {
6481 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+ }
6889 }
6990 }
7091}
0 commit comments