Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check version information before runnning dev phone #153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"phone",
"twilio"
],
"engines": {
"twilio": ">=4.0.0"
},
"author": "Twilio @twilio",
"license": "MIT",
"bugs": {
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-dev-phone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"@twilio/cli-core": "^6.0.0",
"express": "^4.17.1",
"get-port": "^5.1.1",
"open": "^8.4.0"
"open": "^8.4.0",
"semver": "^7.3.7"
},
"devDependencies": {
"@oclif/dev-cli": "^1.22.2",
"@oclif/test": "^1.2.5",
"@twilio/cli-test": "^2.1.0",
"@types/express": "^4.17.13",
"@types/semver": "^7.3.12",
"chai": "^4.2.0",
"eslint": "^8.3.0",
"eslint-config-oclif": "^3.1.0",
Expand All @@ -33,7 +35,8 @@
"typescript": "^4.6.2"
},
"engines": {
"node": ">=10.0.0"
"node": ">=10.0.0",
"twilio": ">=4.0.0"
},
"files": [
"/oclif.manifest.json",
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-dev-phone/src/commands/dev-phone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import express from 'express';

import { flags } from '@oclif/command';
import { deployServerless, constants } from '../utils/create-serverless-util';
import { getAvailablePort, isValidPort } from '../utils/helpers'
import { getAvailablePort, isValidPort, meetsRequiredVersion } from '../utils/helpers'
import { isSmsUrlSet, isVoiceUrlSet } from '../utils/phone-number-utils';
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const { TwilioCliError } = require('@twilio/cli-core').services.error;
const WebClientPath = path.resolve(require.resolve('@twilio-labs/dev-phone-ui'), '..')
const { version } = require('../../package.json');
const { engines, version } = require('../../package.json');

// Types
import { ServiceInstance as ServerlessServiceInstance } from 'twilio/lib/rest/serverless/v1/service'
Expand Down Expand Up @@ -57,6 +57,10 @@ class DevPhoneServer extends TwilioClientCommand {
async run() {
await super.run();

if(!meetsRequiredVersion(this.config.version, engines.twilio)) {
throw new TwilioCliError(`You're using Twilio CLI version ${this.config.version}. Please update your Twilio CLI to a version ${engines.twilio}. You can find instructions here: https://www.twilio.com/docs/twilio-cli/getting-started/install`)
}

const props = this.parseProperties() || {};
await this.validatePropsAndFlags(props, this.flags)

Expand Down
14 changes: 14 additions & 0 deletions packages/plugin-dev-phone/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getPort from 'get-port'
import semver from 'semver'

export function isValidPort (port:string) {
const portRegex = /^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$/gi;
Expand All @@ -8,4 +9,17 @@ export function isValidPort (port:string) {
export async function getAvailablePort() {
const availablePort = await getPort({port: [1337, 3000, 3001, 8000, 8080]})
return availablePort
}

// Checks whether an actual version meets some requirement
export function meetsRequiredVersion (actual: string, requirement: string) {
// ensure that the strings passed are actually valid semver values
const coercedActual = semver.coerce(actual)
const coercedRequirement = semver.coerce(requirement)
if (semver.valid(coercedActual) && semver.valid(coercedRequirement)) {
// @ts-ignore
return semver.gt(coercedActual, coercedRequirement)
}

return false
}