Skip to content

Commit 95923a9

Browse files
Merge pull request #39 from twilio-labs/update-oclif
Update oclif & minor refactors
2 parents c0642f7 + 45ad11a commit 95923a9

File tree

13 files changed

+1730
-10253
lines changed

13 files changed

+1730
-10253
lines changed

package-lock.json

Lines changed: 1654 additions & 10156 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
"access": "public"
99
},
1010
"dependencies": {
11-
"@oclif/command": "^1.6.1",
12-
"@oclif/config": "^1.15.1",
13-
"@twilio/cli-core": "^6.0.0"
11+
"@oclif/core": "^1.13.10",
12+
"@twilio/cli-core": "^7.0.0"
1413
},
1514
"devDependencies": {
1615
"@oclif/dev-cli": "^1.22.2",

src/commands/token/capability/client.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
22
const ClientCapability = require('twilio').jwt.ClientCapability;
33
const globalFlags = require('../../../helpers/globalFlags.js');
4-
const { voiceFlags, validateTwimlAppSid } = require('../../../helpers/voiceGlobals.js');
4+
const { voiceFlags } = require('../../../helpers/voiceGlobals.js');
5+
const { validateSid } = require('../../../helpers/validation-helpers.js');
56

67
class ClientCapabilityTokenGenerator extends TwilioClientCommand {
78
constructor(argv, config) {
@@ -13,27 +14,29 @@ class ClientCapabilityTokenGenerator extends TwilioClientCommand {
1314
async run() {
1415
await super.run();
1516

16-
let ttl = this.flags['ttl'];
17+
const voiceAppSid = await this.flags['voice-app-sid'];
18+
const ttl = await this.flags['ttl'];
19+
const incomingAllow = await this.flags['allow-incoming'];
20+
const identity = await this.flags['identity'];
1721
const capability = new ClientCapability({
1822
accountSid: this.twilioClient.accountSid,
1923
authToken: this.twilioClient.password,
2024
ttl
2125
});
2226

23-
if (!validateTwimlAppSid(this.flags['voice-app-sid'])) {
27+
if (!validateSid('AP', voiceAppSid)) {
2428
this.logger.error(
2529
'Invalid TwiML Application SID, must look like APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2630
);
2731
process.exit(1);
2832
}
2933

3034
capability.addScope(new ClientCapability.OutgoingClientScope({
31-
applicationSid: this.flags['voice-app-sid']
35+
applicationSid: voiceAppSid
3236
}));
3337

34-
let incomingAllow = (this.flags['allow-incoming'] == 'true');
3538
if (incomingAllow) {
36-
capability.addScope(new ClientCapability.IncomingClientScope(this.flags['identity']))
39+
capability.addScope(new ClientCapability.IncomingClientScope(identity))
3740
}
3841

3942
this.logger.info('Copy/paste this voice token into your test application:');

src/commands/token/capability/worker.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
22
const globalFlags = require('../../../helpers/globalFlags.js');
3-
const { taskrouterFlags, validateWorkerSid, validateWorkspaceSid } = require('../../../helpers/taskrouterGlobals.js');
3+
const { taskrouterFlags } = require('../../../helpers/taskrouterGlobals.js');
4+
const { validateSid } = require('../../../helpers/validation-helpers.js');
45
const taskrouter = require('twilio').jwt.taskrouter;
56
const util = taskrouter.util;
67
const TaskRouterCapability = taskrouter.TaskRouterCapability;
@@ -16,17 +17,20 @@ class WorkerCapabilityTokenGenerator extends TwilioClientCommand {
1617
async run() {
1718
await super.run();
1819

20+
const workerSid = await this.flags['worker-sid'];
21+
const workspaceSid = await this.flags['workspace-sid'];
22+
let ttl = await this.flags['ttl'];
1923
const TASKROUTER_BASE_URL = 'https://taskrouter.twilio.com';
2024
const version = 'v1';
2125

22-
if (!validateWorkerSid(this.flags['worker-sid'])) {
26+
if (!validateSid('WK', workerSid)) {
2327
this.logger.error(
2428
'Invalid Worker SID, must look like WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2529
);
2630
process.exit(1);
2731
}
2832

29-
if (!validateWorkspaceSid(this.flags['workspace-sid'])) {
33+
if (!validateSid('WS', workspaceSid)) {
3034
this.logger.error(
3135
'Invalid Workspace SID, must look like WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
3236
);
@@ -35,10 +39,7 @@ class WorkerCapabilityTokenGenerator extends TwilioClientCommand {
3539

3640
const accountSid = this.twilioClient.accountSid;
3741
const authToken = this.twilioClient.password;
38-
const workspaceSid = this.flags['workspace-sid'];
39-
const workerSid = this.flags['worker-sid'];
4042

41-
let ttl = this.flags['ttl'];
4243
const capability = new TaskRouterCapability({
4344
accountSid,
4445
authToken,

src/commands/token/chat.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const { flags } = require('@oclif/command');
1+
const { Flags } = require('@oclif/core');
22
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
33
const Twilio = require('twilio');
44
const createToken = require('../../helpers/accessToken.js');
55
const globalFlags = require('../../helpers/globalFlags.js');
6+
const { validateSid } = require('../../helpers/validation-helpers.js');
67

78
class ChatTokenGenerator extends TwilioClientCommand {
89
constructor(argv, config) {
@@ -11,27 +12,21 @@ class ChatTokenGenerator extends TwilioClientCommand {
1112
this.showHeaders = true;
1213
}
1314

14-
validateChatServiceSid() {
15-
return (
16-
this.flags['chat-service-sid'].startsWith('IS') &&
17-
this.flags['chat-service-sid'].length === 34
18-
);
19-
}
20-
2115
async run() {
2216
await super.run();
2317

18+
const chatServiceSid = await this.flags['chat-service-sid'];
2419
const accessToken = createToken.call(this);
2520

26-
if (!this.validateChatServiceSid()) {
21+
if (!validateSid('IS', chatServiceSid)) {
2722
this.logger.error(
2823
'Invalid Chat Service SID, must look like ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2924
);
3025
process.exit(1);
3126
}
3227

3328
let chatGrant = new Twilio.jwt.AccessToken.ChatGrant({
34-
serviceSid: this.flags['chat-service-sid'],
29+
serviceSid: chatServiceSid
3530
});
3631
accessToken.addGrant(chatGrant);
3732

@@ -43,13 +38,13 @@ class ChatTokenGenerator extends TwilioClientCommand {
4338
}
4439

4540
const ChatTokenGeneratorFlags = {
46-
identity: flags.string({
41+
identity: Flags.string({
4742
description: 'The user identity for this Chat',
48-
required: true,
43+
required: true
4944
}),
50-
'chat-service-sid': flags.string({
45+
'chat-service-sid': Flags.string({
5146
description: 'The service SID for the Chat, starts with ISXXX',
52-
required: true,
47+
required: true
5348
}),
5449
};
5550

src/commands/token/flex.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const { flags } = require('@oclif/command');
21
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
32
const { TwilioCliError } = require('@twilio/cli-core').services.error;
43
const Twilio = require('twilio');
54
const createToken = require('../../helpers/accessToken.js');
65
const globalFlags = require('../../helpers/globalFlags.js');
7-
const { taskrouterFlags, validateWorkerSid, validateWorkspaceSid } = require('../../helpers/taskrouterGlobals.js');
6+
const { taskrouterFlags } = require('../../helpers/taskrouterGlobals.js');
7+
const { validateSid } = require('../../helpers/validation-helpers.js');
88

99
class FlexTokenGenerator extends TwilioClientCommand {
1010
constructor(argv, config) {
@@ -16,25 +16,27 @@ class FlexTokenGenerator extends TwilioClientCommand {
1616
async run() {
1717
await super.run();
1818

19+
const workerSid = await this.flags['worker-sid'];
20+
const workspaceSid = await this.flags['workspace-sid'];
1921
const accessToken = createToken.call(this);
2022

21-
if (!validateWorkerSid(this.flags['worker-sid'])) {
23+
if (!validateSid('WK', workerSid)) {
2224
this.logger.error(
2325
'Invalid Worker SID, must look like WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2426
);
2527
process.exit(1);
2628
}
2729

28-
if (!validateWorkspaceSid(this.flags['workspace-sid'])) {
30+
if (!validateSid('WS', workspaceSid)) {
2931
this.logger.error(
3032
'Invalid Workspace SID, must look like WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
3133
);
3234
process.exit(1);
3335
}
3436

3537
let flexGrant = new Twilio.jwt.AccessToken.TaskRouterGrant({
36-
workerSid: this.flags['worker-sid'],
37-
workspaceSid: this.flags['workspace-sid'],
38+
workerSid,
39+
workspaceSid,
3840
role: 'worker'
3941
});
4042
accessToken.addGrant(flexGrant);

src/commands/token/sync.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const { flags } = require('@oclif/command');
1+
const { Flags } = require('@oclif/core');
22
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
33
const Twilio = require('twilio');
44
const createToken = require('../../helpers/accessToken.js');
55
const globalFlags = require('../../helpers/globalFlags.js');
6+
const { validateSid } = require('../../helpers/validation-helpers.js');
67

78
class SyncTokenGenerator extends TwilioClientCommand {
89
constructor(argv, config) {
@@ -11,27 +12,21 @@ class SyncTokenGenerator extends TwilioClientCommand {
1112
this.showHeaders = true;
1213
}
1314

14-
validateSyncServiceSid() {
15-
return (
16-
this.flags['sync-service-sid'].startsWith('IS') &&
17-
this.flags['sync-service-sid'].length === 34
18-
);
19-
}
20-
2115
async run() {
2216
await super.run();
2317

18+
const syncServiceSid = await this.flags['sync-service-sid'];
2419
const accessToken = createToken.call(this);
2520

26-
if (!this.validateSyncServiceSid()) {
21+
if (!validateSid('IS', syncServiceSid)) {
2722
this.logger.error(
2823
'Invalid Sync Service SID, must look like ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2924
);
3025
process.exit(1);
3126
}
3227

3328
let syncGrant = new Twilio.jwt.AccessToken.SyncGrant({
34-
serviceSid: this.flags['sync-service-sid'],
29+
serviceSid: syncServiceSid
3530
});
3631
accessToken.addGrant(syncGrant);
3732

@@ -43,11 +38,11 @@ class SyncTokenGenerator extends TwilioClientCommand {
4338
}
4439

4540
const SyncTokenGeneratorFlags = {
46-
identity: flags.string({
41+
identity: Flags.string({
4742
description: 'The user identity for this Sync Service',
4843
required: true,
4944
}),
50-
'sync-service-sid': flags.string({
45+
'sync-service-sid': Flags.string({
5146
description: 'The service SID for the Sync, starts with ISXXX',
5247
required: true,
5348
}),

src/commands/token/video.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { flags } = require('@oclif/command');
1+
const { Flags } = require('@oclif/core');
22
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
33
const Twilio = require('twilio');
44
const createToken = require('../../helpers/accessToken.js');
@@ -16,7 +16,7 @@ class VideoTokenGenerator extends TwilioClientCommand {
1616

1717
const accessToken = createToken.call(this);
1818

19-
let room = this.flags['room-name'];
19+
let room = await this.flags['room-name'];
2020
let videoGrant = new Twilio.jwt.AccessToken.VideoGrant({ room });
2121
accessToken.addGrant(videoGrant);
2222

@@ -28,7 +28,7 @@ class VideoTokenGenerator extends TwilioClientCommand {
2828
}
2929

3030
const VideoTokenGeneratorFlags = {
31-
'room-name': flags.string({
31+
'room-name': Flags.string({
3232
description: 'The name of the room this token grants access to',
3333
required: false,
3434
}),

src/commands/token/voice.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
22
const Twilio = require('twilio');
33
const createToken = require('../../helpers/accessToken.js');
44
const globalFlags = require('../../helpers/globalFlags.js');
5-
const { voiceFlags, validateTwimlAppSid, validatePushCredentialSid } = require('../../helpers/voiceGlobals.js');
5+
const { voiceFlags } = require('../../helpers/voiceGlobals.js');
6+
const { validateSid } = require('../../helpers/validation-helpers.js');
67

78
class VoiceTokenGenerator extends TwilioClientCommand {
89
constructor(argv, config) {
@@ -15,27 +16,31 @@ class VoiceTokenGenerator extends TwilioClientCommand {
1516
await super.run();
1617

1718
const accessToken = createToken.call(this);
19+
// all flags
20+
const voiceAppSid = await this.flags['voice-app-sid'];
21+
const pushCredentialSid = await this.flags['push-credential-sid'];
22+
const incomingAllow = await this.flags['allow-incoming'];
1823

19-
if (!validateTwimlAppSid(this.flags['voice-app-sid'])) {
24+
if (!validateSid('AP', voiceAppSid)) {
2025
this.logger.error(
2126
'Invalid TwiML Application SID, must look like APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2227
);
2328
process.exit(1);
2429
}
2530

26-
let pushCredentialSid = this.flags['push-credential-sid'];
27-
if (pushCredentialSid && !validatePushCredentialSid(pushCredentialSid)) {
31+
// logic for optional pushCredential
32+
if (pushCredentialSid && !validateSid('CR', pushCredentialSid)) {
2833
this.logger.error(
2934
'Invalid Push Credential SID, must look like CRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
3035
);
3136
process.exit(1);
3237
}
3338

34-
let incomingAllow = (this.flags['allow-incoming'] == 'true');
35-
let voiceGrant = new Twilio.jwt.AccessToken.VoiceGrant({
36-
outgoingApplicationSid: this.flags['voice-app-sid'],
39+
const voiceGrant = new Twilio.jwt.AccessToken.VoiceGrant({
40+
outgoingApplicationSid: voiceAppSid,
3741
incomingAllow
3842
});
43+
3944
if (pushCredentialSid) {
4045
voiceGrant.pushCredentialSid = pushCredentialSid;
4146
}

src/helpers/globalFlags.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const { flags } = require('@oclif/command');
1+
const { Flags } = require('@oclif/core');
22

33
const globalFlags = {
4-
identity: flags.string({
4+
identity: Flags.string({
55
description: 'The user identity',
66
required: true,
77
}),
8-
ttl: flags.integer({
8+
ttl: Flags.integer({
99
description: 'Optional TTL for token (up to 24 hours) (value in seconds)',
1010
required: false,
1111
})

0 commit comments

Comments
 (0)