Skip to content

Commit bb3ce51

Browse files
authored
feat(plugin-assets): adds flag for setting service name in init (#286)
1 parent 20d024d commit bb3ce51

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

packages/plugin-assets/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"@twilio-labs/serverless-api": "^5.1.0",
1313
"@twilio/cli-core": "^5.22.0",
1414
"inquirer": "^8.0.0",
15-
"ora": "^5.4.0"
15+
"ora": "^5.4.0",
16+
"project-name-generator": "^2.1.9"
1617
},
1718
"devDependencies": {
1819
"@oclif/dev-cli": "^1.22.2",

packages/plugin-assets/src/commands/assets/init.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const { flags } = require('@oclif/command');
22
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
33
const { getPluginConfig } = require('../../pluginConfig');
4+
const generateProjectName = require('project-name-generator');
5+
46
const { init } = require('../../init');
57

68
class InitCommand extends TwilioClientCommand {
@@ -14,6 +16,7 @@ class InitCommand extends TwilioClientCommand {
1416
accountSid: this.currentProfile.accountSid,
1517
pluginConfig: pluginConfig,
1618
logger: this.logger,
19+
serviceName: this.flags['service-name'],
1720
});
1821
this.output(result, this.flags.properties);
1922
} catch (error) {
@@ -23,6 +26,11 @@ class InitCommand extends TwilioClientCommand {
2326
}
2427

2528
InitCommand.flags = {
29+
'service-name': flags.string({
30+
description:
31+
'A unique name for your asset service. May only contain alphanumeric characters and hyphens.',
32+
default: () => generateProjectName().dashed,
33+
}),
2634
properties: flags.string({
2735
default: 'service_sid, sid, domain_name',
2836
description:

packages/plugin-assets/src/commands/assets/list.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { list } = require('../../list');
66
class ListCommand extends TwilioClientCommand {
77
async run() {
88
await super.run();
9-
109
try {
1110
const pluginConfig = getPluginConfig(this);
1211
const assets = await list({

packages/plugin-assets/src/commands/assets/upload.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ class UploadCommand extends TwilioClientCommand {
88
await super.run();
99

1010
try {
11-
const { args } = this.parse(UploadCommand);
1211
const pluginConfig = getPluginConfig(this);
1312
const assets = await upload({
1413
apiKey: this.currentProfile.apiKey,
1514
apiSecret: this.currentProfile.apiSecret,
1615
accountSid: this.currentProfile.accountSid,
1716
pluginConfig: pluginConfig,
18-
file: args.file,
17+
file: this.args.file,
1918
logger: this.logger,
2019
});
2120
this.output(assets, this.flags.properties);

packages/plugin-assets/src/init.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,31 @@ const { TwilioCliError } = require('@twilio/cli-core').services.error;
1010

1111
const { 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-zA-Z0-9]+(?:-[a-zA-Z0-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
}

packages/plugin-assets/tests/init.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ const mockLogger = {
3030
debug: jest.fn(),
3131
};
3232

33-
const configPath = path.join(tmpdir(), 'scratch', 'plugin-assets-config.json');
34-
3533
describe('init', () => {
3634
describe('with nothing in the config', () => {
3735
const fakeConfig = {
@@ -45,6 +43,7 @@ describe('init', () => {
4543
accountSid: 'test-account-sid',
4644
pluginConfig: fakeConfig,
4745
logger: mockLogger,
46+
serviceName: 'test-asset-service',
4847
});
4948

5049
expect(createEnvironmentFromSuffix).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)