Skip to content
Merged
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
16 changes: 10 additions & 6 deletions packages/twilio-run/__tests__/config/start.withoutNgrok.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@ jest.mock('ngrok', () => {

describe('getUrl', () => {
test('calls ngrok if ngrok is defined', async () => {
const config = ({
const config = {
ngrok: '',
} as unknown) as StartCliFlags;
} as unknown as StartCliFlags;

expect.assertions(1);
try {
await getUrl(config, 3000);
} catch (error) {
expect(error.message).toMatch("Cannot find module 'ngrok'");
expect(error.message).toMatch(
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
);
}
});

test('calls ngrok with custom subdomain if passed', async () => {
const config = ({
const config = {
ngrok: 'dom',
} as unknown) as StartCliFlags;
} as unknown as StartCliFlags;

expect.assertions(1);
try {
await getUrl(config, 3000);
} catch (error) {
expect(error.message).toMatch("Cannot find module 'ngrok'");
expect(error.message).toMatch(
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
);
}
});
});
8 changes: 5 additions & 3 deletions packages/twilio-run/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ export async function handler(
resolve();
} catch (error) {
server.close(() => {
logger.info(
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
);
if (error.msg && error.details && error.details.err) {
logger.error(`${error.msg}\n\n${error.details.err}`);
} else {
logger.error(error.message);
}
process.exit(1);
});
}
Expand Down
11 changes: 9 additions & 2 deletions packages/twilio-run/src/config/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ export async function getUrl(cli: StartCliFlags, port: string | number) {
if (typeof cli.ngrok === 'string' && cli.ngrok.length > 0) {
ngrokConfig.subdomain = cli.ngrok;
}

url = await require('ngrok').connect(ngrokConfig);
let ngrok;
try {
ngrok = require('ngrok');
} catch (error) {
throw new Error(
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
);
}
url = await ngrok.connect(ngrokConfig);
debug('ngrok tunnel URL: %s', url);
}

Expand Down