Skip to content

Commit 3061015

Browse files
authored
feat(twilio-run:start): make ngrok optional and handle failure (#207)
This is intended to help with #205. Still need to investigate installing CLI plugins without optional dependencies.
1 parent 6ff8225 commit 3061015

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { getUrl, StartCliFlags } from '../../src/config/start';
2+
3+
jest.mock('ngrok', () => {
4+
throw new Error("Cannot find module 'ngrok'");
5+
});
6+
7+
describe('getUrl', () => {
8+
test('calls ngrok if ngrok is defined', async () => {
9+
const config = ({
10+
ngrok: '',
11+
} as unknown) as StartCliFlags;
12+
13+
expect.assertions(1);
14+
try {
15+
await getUrl(config, 3000);
16+
} catch (error) {
17+
expect(error.message).toMatch("Cannot find module 'ngrok'");
18+
}
19+
});
20+
21+
test('calls ngrok with custom subdomain if passed', async () => {
22+
const config = ({
23+
ngrok: 'dom',
24+
} as unknown) as StartCliFlags;
25+
26+
expect.assertions(1);
27+
try {
28+
await getUrl(config, 3000);
29+
} catch (error) {
30+
expect(error.message).toMatch("Cannot find module 'ngrok'");
31+
}
32+
});
33+
});

packages/twilio-run/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"lodash.kebabcase": "^4.1.1",
6363
"lodash.startcase": "^4.4.0",
6464
"log-symbols": "^2.2.0",
65-
"ngrok": "^3.0.1",
6665
"nocache": "^2.1.0",
6766
"normalize.css": "^8.0.1",
6867
"ora": "^3.3.1",
@@ -76,6 +75,9 @@
7675
"wrap-ansi": "^5.1.0",
7776
"yargs": "^13.2.2"
7877
},
78+
"optionalDependencies": {
79+
"ngrok": "^3.3.0"
80+
},
7981
"devDependencies": {
8082
"@types/cheerio": "^0.22.12",
8183
"@types/common-tags": "^1.8.0",

packages/twilio-run/src/commands/start.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inquirer from 'inquirer';
22
import { Argv } from 'yargs';
3+
import { Server } from 'http';
34
import checkNodejsVersion from '../checks/nodejs-version';
45
import checkProjectStructure from '../checks/project-structure';
56
import { getConfigFromCli, StartCliFlags } from '../config/start';
@@ -71,6 +72,7 @@ export async function handler(
7172
}
7273

7374
const app = await createServer(config.port, config);
75+
let server: Server;
7476
debug('Start server on port %d', config.port);
7577
return new Promise((resolve, reject) => {
7678
let attempts = 1;
@@ -80,9 +82,18 @@ export async function handler(
8082
if (port) {
8183
config.port = port;
8284
}
83-
config.url = await getUrl(argv, config.port);
84-
printRouteInfo(config);
85-
resolve();
85+
try {
86+
config.url = await getUrl(argv, config.port);
87+
printRouteInfo(config);
88+
resolve();
89+
} catch (error) {
90+
server.close(() => {
91+
logger.info(
92+
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
93+
);
94+
process.exit(1);
95+
});
96+
}
8697
};
8798
};
8899
const handleServerError = async (error: ServerError) => {
@@ -101,7 +112,7 @@ export async function handler(
101112
},
102113
]);
103114
attempts += 1;
104-
const server = app.listen(
115+
server = app.listen(
105116
answers.newPortNumber,
106117
serverStartedSuccessfully(answers.newPortNumber)
107118
);
@@ -112,7 +123,7 @@ export async function handler(
112123
}
113124
};
114125

115-
const server = app.listen(config.port, serverStartedSuccessfully());
126+
server = app.listen(config.port, serverStartedSuccessfully());
116127
server.on('error', handleServerError);
117128
});
118129
}

0 commit comments

Comments
 (0)