-
Notifications
You must be signed in to change notification settings - Fork 26
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
chore: lint using twilio-style #100
Merged
+822
−738
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,13 @@ | ||
{ | ||
"extends": "oclif", | ||
"plugins": ["mocha"], | ||
"env": { | ||
"mocha": true | ||
"extends": [ | ||
"twilio", | ||
"twilio-mocha" | ||
], | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"rules": { | ||
"indent": ["error", 2], | ||
"linebreak-style": ["error", "unix"], | ||
"quotes": ["error", "single"], | ||
"semi": ["error", "always"], | ||
"object-curly-spacing": ["error", "always"], | ||
"comma-dangle": ["error", "never"], | ||
"mocha/no-exclusive-tests": "error", | ||
"node/no-extraneous-require": [ | ||
"error", | ||
{ | ||
"allowModules": ["lodash", "request", "q"] | ||
} | ||
] | ||
"global-require": "off", | ||
"prefer-named-capture-group": "off" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
const { Command, flags } = require('@oclif/command'); | ||
const { Command, flags: oclifFlags } = 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'); | ||
|
@@ -9,6 +10,7 @@ const { OutputFormats } = require('../services/output-formats'); | |
const { getCommandPlugin, requireInstall } = require('../services/require-install'); | ||
const { SecureStorage } = require('../services/secure-storage'); | ||
const { instanceOf } = require('../services/javascript-utilities'); | ||
|
||
let inquirer; // We'll lazy-load this only when it's needed. | ||
|
||
const DEFAULT_LOG_LEVEL = 'info'; | ||
|
@@ -40,7 +42,7 @@ class BaseCommand extends Command { | |
this.logger = logger; | ||
this.logger.config.level = LoggingLevel[flags['cli-log-level'] || DEFAULT_LOG_LEVEL]; | ||
|
||
this.logger.debug('Config File: ' + this.configFile.filePath); | ||
this.logger.debug(`Config File: ${this.configFile.filePath}`); | ||
|
||
// Replace oclif's output commands | ||
this.log = this.logger.info; | ||
|
@@ -65,20 +67,31 @@ class BaseCommand extends Command { | |
this.exit(error.exitCode || 1); | ||
} else { | ||
// System errors | ||
const plugin = getCommandPlugin(this); | ||
this.logger.error(MessageTemplates.unexpectedError({ url: this.getIssueUrl(plugin) })); | ||
let url = ''; | ||
try { | ||
url = this.getIssueUrl(getCommandPlugin(this)); | ||
} catch (e) { | ||
// No-op | ||
} | ||
|
||
this.logger.error(MessageTemplates.unexpectedError({ url })); | ||
this.logger.debug(error.message); | ||
this.logger.debug(error.stack); | ||
this.exit(1); | ||
} | ||
|
||
throw err; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
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. | ||
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); | ||
} | ||
|
||
|
@@ -105,16 +118,16 @@ class BaseCommand extends Command { | |
|
||
const limitedData = properties ? this.getLimitedData(dataArray, properties) : null; | ||
|
||
process.stdout.write(this.outputProcessor(dataArray, limitedData || dataArray, options) + '\n'); | ||
process.stdout.write(`${this.outputProcessor(dataArray, limitedData || dataArray, options)}\n`); | ||
} | ||
|
||
getLimitedData(dataArray, properties) { | ||
const invalidPropertyNames = new Set(); | ||
const propNames = properties.split(',').map(p => p.trim()); | ||
const limitedData = dataArray.map(fullItem => { | ||
const propNames = properties.split(',').map((p) => p.trim()); | ||
const limitedData = dataArray.map((fullItem) => { | ||
const limitedItem = {}; | ||
|
||
propNames.forEach(p => { | ||
propNames.forEach((p) => { | ||
let propValue = fullItem[p]; | ||
|
||
if (propValue === undefined) { | ||
|
@@ -137,7 +150,7 @@ class BaseCommand extends Command { | |
|
||
if (invalidPropertyNames.size > 0) { | ||
const warn = this.logger.warn.bind(this.logger); | ||
invalidPropertyNames.forEach(p => { | ||
invalidPropertyNames.forEach((p) => { | ||
warn(`"${p}" is not a valid property name.`); | ||
}); | ||
} | ||
|
@@ -156,21 +169,21 @@ class BaseCommand extends Command { | |
} | ||
|
||
BaseCommand.flags = { | ||
'cli-log-level': flags.enum({ | ||
'cli-log-level': oclifFlags.enum({ | ||
char: 'l', | ||
helpLabel: '-l', | ||
default: DEFAULT_LOG_LEVEL, | ||
options: Object.keys(LoggingLevel), | ||
description: 'Level of logging messages.' | ||
description: 'Level of logging messages.', | ||
}), | ||
|
||
'cli-output-format': flags.enum({ | ||
'cli-output-format': oclifFlags.enum({ | ||
char: 'o', | ||
helpLabel: '-o', | ||
default: DEFAULT_OUTPUT_FORMAT, | ||
options: Object.keys(OutputFormats), | ||
description: 'Format of command output.' | ||
}) | ||
description: 'Format of command output.', | ||
}), | ||
}; | ||
|
||
module.exports = BaseCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const { flags } = require('@oclif/command'); | ||
|
||
const BaseCommand = require('./base-command'); | ||
const CliRequestClient = require('../services/cli-http-client'); | ||
const { TwilioApiClient, TwilioApiFlags } = require('../services/twilio-api'); | ||
|
@@ -27,18 +28,16 @@ class TwilioClientCommand extends BaseCommand { | |
|
||
const reportUnconfigured = (verb, message = '') => { | ||
const profileParam = this.flags.profile ? ` --profile "${this.flags.profile}"` : ''; | ||
throw new TwilioCliError( | ||
`To ${verb} the profile, run:\n\n twilio profiles:create${profileParam}${message}` | ||
); | ||
throw new TwilioCliError(`To ${verb} the profile, run:\n\n twilio profiles:create${profileParam}${message}`); | ||
}; | ||
|
||
if (!this.currentProfile) { | ||
const profileName = this.flags.profile ? ` "${this.flags.profile}"` : ''; | ||
this.logger.error(`Could not find profile${profileName}.`); | ||
reportUnconfigured('create', '\n\n' + HELP_ENVIRONMENT_VARIABLES); | ||
reportUnconfigured('create', `\n\n${HELP_ENVIRONMENT_VARIABLES}`); | ||
} | ||
|
||
this.logger.debug('Using profile: ' + this.currentProfile.id); | ||
this.logger.debug(`Using profile: ${this.currentProfile.id}`); | ||
|
||
if (!this.currentProfile.apiKey || !this.currentProfile.apiSecret) { | ||
const creds = await this.secureStorage.getCredentials(this.currentProfile.id); | ||
|
@@ -54,11 +53,14 @@ class TwilioClientCommand extends BaseCommand { | |
} | ||
|
||
async catch(error) { | ||
// Append to the error message when catching API access denied errors with | ||
// profile-auth (i.e., standard API key auth). | ||
/* | ||
* Append to the error message when catching API access denied errors with | ||
* profile-auth (i.e., standard API key auth). | ||
*/ | ||
if (instanceOf(error, TwilioCliError) && error.exitCode === ACCESS_DENIED_CODE) { | ||
if (!this.currentProfile.id.startsWith('${TWILIO')) { // Auth *not* using env vars. | ||
error.message += '\n\n' + ACCESS_DENIED; | ||
if (!this.currentProfile.id.startsWith('${TWILIO')) { | ||
// Auth *not* using env vars. | ||
error.message += `\n\n${ACCESS_DENIED}`; | ||
} | ||
} | ||
|
||
|
@@ -71,7 +73,7 @@ class TwilioClientCommand extends BaseCommand { | |
} | ||
|
||
let updatedProperties = null; | ||
Object.keys(this.constructor.PropertyFlags).forEach(propName => { | ||
Object.keys(this.constructor.PropertyFlags).forEach((propName) => { | ||
if (this.flags[propName] !== undefined) { | ||
updatedProperties = updatedProperties || {}; | ||
const paramName = camelCase(propName); | ||
|
@@ -85,7 +87,7 @@ class TwilioClientCommand extends BaseCommand { | |
async updateResource(resource, resourceSid, updatedProperties) { | ||
const results = { | ||
sid: resourceSid, | ||
result: '?' | ||
result: '?', | ||
}; | ||
|
||
updatedProperties = updatedProperties || this.parseProperties(); | ||
|
@@ -128,38 +130,36 @@ class TwilioClientCommand extends BaseCommand { | |
accountSid: this.flags[CliFlags.ACCOUNT_SID] || this.currentProfile.accountSid, | ||
edge: process.env.TWILIO_EDGE || this.userConfig.edge, | ||
region: this.currentProfile.region, | ||
httpClient: this.httpClient | ||
httpClient: this.httpClient, | ||
}); | ||
} | ||
} | ||
|
||
TwilioClientCommand.flags = Object.assign( | ||
{ | ||
profile: flags.string({ | ||
char: 'p', | ||
description: 'Shorthand identifier for your profile.' | ||
}) | ||
}, | ||
BaseCommand.flags | ||
); | ||
TwilioClientCommand.flags = { | ||
profile: flags.string({ | ||
char: 'p', | ||
description: 'Shorthand identifier for your profile.', | ||
}), | ||
...BaseCommand.flags, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W00t! |
||
}; | ||
|
||
TwilioClientCommand.accountSidFlag = { | ||
[CliFlags.ACCOUNT_SID]: flags.string({ | ||
description: 'Access resources for the specified account.' | ||
}) | ||
description: 'Access resources for the specified account.', | ||
}), | ||
}; | ||
|
||
TwilioClientCommand.limitFlags = { | ||
[CliFlags.LIMIT]: flags.string({ | ||
description: `The maximum number of resources to return. Use '--${CliFlags.NO_LIMIT}' to disable.`, | ||
default: 50, | ||
exclusive: [CliFlags.NO_LIMIT] | ||
exclusive: [CliFlags.NO_LIMIT], | ||
}), | ||
[CliFlags.NO_LIMIT]: flags.boolean({ | ||
default: false, | ||
hidden: true, | ||
exclusive: [CliFlags.LIMIT] | ||
}) | ||
exclusive: [CliFlags.LIMIT], | ||
}), | ||
}; | ||
|
||
module.exports = TwilioClientCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be dropped? No longer used in
.eslintrc
.