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

feat: look through plugin pjson for an issue URL #87

Merged
merged 1 commit into from
May 15, 2020
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
17 changes: 14 additions & 3 deletions src/base-commands/base-command.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { Command, flags } = require('@oclif/command');
const { CLIError } = require('@oclif/errors');
const pkg = require('../../package.json');
const MessageTemplates = require('../services/messaging/templates');
const { Config, ConfigData } = require('../services/config');
const { TwilioCliError } = require('../services/error');
const { UNEXPECTED_ERROR } = require('../services/messaging/help-messages');
const { logger, LoggingLevel } = require('../services/messaging/logging');
const { OutputFormats } = require('../services/output-formats');
const { requireInstall } = require('../services/require-install');
const { getCommandPlugin, requireInstall } = require('../services/require-install');
const { SecureStorage } = require('../services/secure-storage');
let inquirer; // We'll lazy-load this only when it's needed.

Expand Down Expand Up @@ -63,13 +64,23 @@ class BaseCommand extends Command {
this.exit(error.exitCode || 1);
} else {
// System errors
this.logger.error(UNEXPECTED_ERROR);
const plugin = getCommandPlugin(this);
this.logger.error(MessageTemplates.unexpectedError({ url: this.getIssueUrl(plugin) }));
this.logger.debug(error.message);
this.logger.debug(error.stack);
this.exit(1);
}
}

getIssueUrl(plugin) {
const getPropertyUrl = value => value && (value.url || value);
const getPackageUrl = pjson => getPropertyUrl(pjson.bugs) || getPropertyUrl(pjson.homepage) || getPropertyUrl(pjson.repository);

// If we found the plugin and an issue URL for it, use it. Otherwise
// fallback to our own issue URL.
return (plugin && getPackageUrl(plugin.pjson)) || getPackageUrl(pkg);
}

/**
* Drops the week day and timezone name from the result of Date.toString().
*
Expand Down
4 changes: 0 additions & 4 deletions src/services/messaging/help-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ TWILIO_AUTH_TOKEN = your Auth Token from twil.io/console

Once these environment variables are set, a ${CLI_NAME} profile is not required to move forward with installation.`;

exports.UNEXPECTED_ERROR = `${CLI_NAME} encountered an unexpected error. \
To report this issue, execute the command with the "-l debug" flag, then copy the output to a new issue here: \
https://github.com/twilio/twilio-cli/issues`;

exports.NETWORK_ERROR = `${CLI_NAME} encountered a network connectivity error. \
Please check your network connection and try your command again. \
Check on Twilio service status at https://status.twilio.com/`;
8 changes: 4 additions & 4 deletions src/services/messaging/templates.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { templatize } = require('./templating');

const configSaved = templatize`twilio-cli configuration saved to "${'path'}"`;
exports.configSaved = templatize`twilio-cli configuration saved to "${'path'}"`;

module.exports = {
configSaved
};
exports.unexpectedError = templatize`twilio-cli encountered an unexpected error. \
To report this issue, execute the command with the "-l debug" flag, then copy the output to a new issue here: \
"${'url'}"`;
2 changes: 1 addition & 1 deletion src/services/require-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { logger } = require('./messaging/logging');
* Retrieves the plugin for a given command.
*/
const getCommandPlugin = command => {
for (let plugin of command.config.plugins) {
for (let plugin of command.config.plugins || []) {
for (let pluginCommand of plugin.commands) {
if (pluginCommand.id === command.id || pluginCommand.aliases.includes(command.id)) {
// Check the plugin options/config name first. This will contain the
Expand Down
26 changes: 26 additions & 0 deletions test/base-commands/base-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ describe('base-commands', () => {
await expect(ctx.testCmd.catch(new TwilioCliError('hey-o!'))).to.be.rejectedWith(TwilioCliError);
});

describe('getIssueUrl', () => {
baseCommandTest.it('follows the proper precedence order', ctx => {
const pjson = {
bugs: 'could be',
homepage: 'maybe',
repository: 'nope'
};

expect(ctx.testCmd.getIssueUrl({ pjson })).to.equal('could be');

delete pjson.bugs;
expect(ctx.testCmd.getIssueUrl({ pjson })).to.equal('maybe');

delete pjson.homepage;
expect(ctx.testCmd.getIssueUrl({ pjson })).to.equal('nope');
});

baseCommandTest.it('handles url properties', ctx => {
expect(ctx.testCmd.getIssueUrl({ pjson: { bugs: { email: 'me', url: 'you' } } })).to.equal('you');
});

baseCommandTest.it('use the main repo when no url is found', ctx => {
expect(ctx.testCmd.getIssueUrl({ pjson: { anything: 'nothing' } })).to.equal('https://github.com/twilio/twilio-cli/issues');
});
});

describe('sanitizeDateString', () => {
baseCommandTest.it('check date is sliced correctly', ctx => {
expect(ctx.testCmd.sanitizeDateString('Fri May 24 2019 11:43:11 GMT-0600 (MDT)')).to.equal('May 24 2019 11:43:11 GMT-0600');
Expand Down