Skip to content

Commit

Permalink
feat: DII 47 hyperlinks in help (twilio#290)
Browse files Browse the repository at this point in the history
* Merging changes from main (twilio#286)

* Removed sonar-code scan step

* [Librarian] Regenerated @ fdad267944635962308083659322c23f28226702

* Release 2.29.0

* fix: Added missing require statement (twilio#285)

Co-authored-by: lakshmiravali <lakshmiravali.rimmalapudi@gmail.com>

* [Librarian] Regenerated @ 9a313923ef0eae61a7da7210b7d5de59e65a697c

* Release 2.29.1

Co-authored-by: Anuj <abadhwar@twilio.com>
Co-authored-by: Twilio <team_interfaces+github@twilio.com>
Co-authored-by: ravali-rimmalapudi <83863595+ravali-rimmalapudi@users.noreply.github.com>
Co-authored-by: lakshmiravali <lakshmiravali.rimmalapudi@gmail.com>

* Changes to make url a hyperlink

Changes to make hyperLink for URLs in supported terminals

* Segregated testcases for different terminals

* Fix for failing test cases in Travis

* Fix for failing test cases in Travis

* Testing travis CI build failed testcases

* Testing travis CI build failed testcases

Co-authored-by: Anuj <abadhwar@twilio.com>
Co-authored-by: Twilio <team_interfaces+github@twilio.com>
Co-authored-by: ravali-rimmalapudi <83863595+ravali-rimmalapudi@users.noreply.github.com>
Co-authored-by: lakshmiravali <lakshmiravali.rimmalapudi@gmail.com>
  • Loading branch information
5 people committed Sep 14, 2021
1 parent 7e9d233 commit ce15661
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 5 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
"@twilio/cli-core": "^5.9.1",
"chalk": "^4.1.0",
"file-type": "^14.6.2",
"hyperlinker": "1.0.0",
"inquirer": "^7.3.0",
"ngrok": "^3.2.7",
"supports-hyperlinks": "2.2.0",
"twilio": "^3.49.4",
"untildify": "^4.0.0"
},
Expand All @@ -64,6 +66,7 @@
"eslint": "^7.5.0",
"eslint-config-twilio": "~1.31.0",
"eslint-config-twilio-mocha": "~1.31.0",
"flush-cache": "1.0.1",
"globby": "^11.0.1",
"mocha": "^8.0.1",
"nock": "^13.0.2",
Expand Down
21 changes: 21 additions & 0 deletions src/services/hyperlink-utility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function supportsHyperlink() {
const { env } = process;
const supports = require('supports-hyperlinks');
if (supports.stdout) {
return true;
}
// support for Windows terminal
if ('WT_SESSION' in env) {
return true;
}
return false;
}

function convertToHyperlink(text, link, params) {
if (supportsHyperlink()) {
const hyperlinker = require('hyperlinker');
return { url: hyperlinker(text, link, params), isSupported: true };
}
return { url: link, isSupported: false };
}
module.exports = { convertToHyperlink, supportsHyperlink };
13 changes: 9 additions & 4 deletions src/services/twilio-help/twilio-command-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const indent = require('indent-string');
const util = require('@oclif/plugin-help/lib/util');
const stripAnsi = require('strip-ansi');

const urlUtil = require('../hyperlink-utility');
const { getDocLink } = require('../twilio-api');
/**
* Extended functionality from @oclif/plugin-help.
Expand Down Expand Up @@ -41,10 +42,14 @@ class TwilioCommandHelp extends CommandHelp.default {
if (!helpDoc) {
return '';
}

listOfDetails.push(chalk.bold('MORE INFO'));
listOfDetails.push(indent(helpDoc, 2));

const hyperLink = urlUtil.convertToHyperlink('MORE INFO', helpDoc);
// if the terminal doesn't support hyperlink, mention complete url under More Info
if (hyperLink.isSupported) {
listOfDetails.push(chalk.bold(hyperLink.url));
} else {
listOfDetails.push(chalk.bold('MORE INFO'));
listOfDetails.push(indent(helpDoc, 2));
}
return listOfDetails.join('\n');
}

Expand Down
164 changes: 164 additions & 0 deletions test/services/hyperlink-utility.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/* eslint-disable import/no-extraneous-dependencies */
const { expect, test } = require('@twilio/cli-test');
const { Command } = require('@oclif/command');
const flush = require('flush-cache');

const ORIG_ENV = { ...process.env };
const { supportsHyperlink, convertToHyperlink } = require('../../src/services/hyperlink-utility');
const TwilioHelp = require('../../src/services/twilio-help/custom-help');

class TestCommand extends Command {
constructor(argv, config) {
super(argv, config);
this.id = 'api:core';
this.description = 'This is a dummy description';
this.args = [{ name: 'arg_1', description: 'argument description' }];
}
}
const testThisConfig = ({ platform, env, argv, stream }) => {
platform = platform || 'darwin';
env = env || {};
argv = argv || [];
// back up the original env
const oldPlatform = process.platform;
const oldEnv = process.env;
const oldArgv = process.argv;

// Inject new env properties from args
Object.defineProperties(process, {
platform: { value: platform },
env: { value: env },
argv: { value: [process.argv[0], ...argv] },
});

const result = supportsHyperlink(stream);
// restore the original env
Object.defineProperties(process, {
platform: { value: oldPlatform },
env: { value: oldEnv },
argv: { value: oldArgv },
});
return result;
};

const testLink = test
.loadConfig()
.add('help', (ctx) => new TwilioHelp(ctx.config))
.register('cmdTestLink', (args) => {
return {
async run(ctx) {
const dummyHelpCommand = new TestCommand(args, ctx.config);
dummyHelpCommand.docLink = 'https://twilio.com/docs/dummyCmd';
const help = ctx.help.formatCommand(dummyHelpCommand);
ctx.cmdTestLink = help
.split('\n')
.map((s) => s.trimRight())
.join('\n');
},
};
});

describe('supportsHyperlink', () => {
describe('test hyperlink generation', () => {
describe('test for Mac terminals', () => {
afterEach(() => {
flush();
});
test.it('not supported in Mac Terminal', () => {
expect(
testThisConfig({
env: {
TERM_PROGRAM: '',
},
stream: {
isTTY: false,
},
}),
).to.be.false;
});
test.it('testing convertToHyperlink, supported iTerm.app 3.1, tty stream', () => {
const result = testThisConfig({
env: {
TERM_PROGRAM: 'iTerm.app',
TERM_PROGRAM_VERSION: '3.1.0',
},
stream: {
isTTY: true,
},
});
if ('CI' in process.env) {
expect(result).to.be.false;
expect(convertToHyperlink('MORE INFO', 'https://twilio.com/docs/dummyCmd').isSupported).to.be.false;
} else {
expect(result).to.be.true;
expect(convertToHyperlink('MORE INFO', 'https://twilio.com/docs/dummyCmd').isSupported).to.be.true;
}
});
});

describe('test for iTerm terminals', () => {
afterEach(() => {
flush();
});
test.it('supported in iTerm.app 3.1, tty stream', () => {
const result = testThisConfig({
env: {
TERM_PROGRAM: 'iTerm.app',
TERM_PROGRAM_VERSION: '3.1.0',
},
stream: {
isTTY: true,
},
});
if ('CI' in process.env) {
expect(result).to.be.false;
} else {
expect(result).to.be.true;
}
});
});

describe('test for Windows', () => {
afterEach(() => {
flush();
});
test.it('supported in Windows Terminal', () => {
expect(
testThisConfig({
env: {
WT_SESSION: '',
},
stream: {
isTTY: false,
},
}),
).to.be.true;
});
});
});
});

describe('convertToHyperlink', () => {
describe('test hyperlink generation for dummyURL and dummyText on macOS', () => {
describe('test for iTerm', () => {
beforeEach(() => {
process.env.TERM_PROGRAM = 'iTerm.app';
process.env.TERM_PROGRAM_VERSION = '3.1.0';
});
afterEach(() => {
flush();
process.env = ORIG_ENV;
});
testLink.cmdTestLink([]).it('test', (ctx) => {
const result = convertToHyperlink('MORE INFO', 'https://twilio.com/docs/dummyCmd').isSupported;
if ('CI' in process.env) {
expect(result).to.be.false;
} else {
expect(result).to.be.true;
}
expect(ctx.cmdTestLink).to.contain('MORE INFO');
expect(ctx.cmdTestLink).to.contain('https://twilio.com/docs/dummyCmd');
});
});
});
});
1 change: 0 additions & 1 deletion test/services/twilio-help/twilio-help-doc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const { expect, test } = require('@oclif/test');
const { Command, flags } = require('@oclif/command');
const stripAnsi = require('strip-ansi');
const { id } = require('@twilio/cli-core/src/base-commands/base-command');

const TwilioHelp = require('../../../src/services/twilio-help/custom-help');

Expand Down

0 comments on commit ce15661

Please sign in to comment.