Skip to content

Commit

Permalink
chore: lint using twilio-style (twilio#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktalebian committed Jul 22, 2020
1 parent b3af9cc commit 90ee3f3
Show file tree
Hide file tree
Showing 40 changed files with 822 additions and 738 deletions.
27 changes: 9 additions & 18 deletions .eslintrc
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"
}
}
}
54 changes: 28 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
{
"name": "@twilio/cli-core",
"description": "Core functionality for the twilio-cli",
"version": "5.8.0",
"author": "Twilio @twilio",
"description": "Core functionality for the twilio-cli",
"keywords": [
"twilio"
],
"homepage": "https://github.com/twilio/twilio-cli-core",
"bugs": "https://github.com/twilio/twilio-cli/issues",
"repository": {
"type": "git",
"url": "https://github.com/twilio/twilio-cli-core.git"
},
"license": "MIT",
"author": "Twilio @twilio",
"main": "src/index.js",
"files": [
"/bin",
"/src"
],
"scripts": {
"lint": "eslint --ext js --ext jsx src/ test/",
"lint:fix": "npm run lint -- --fix",
"test": "nyc --check-coverage --lines 90 --reporter=html --reporter=text mocha --forbid-only \"test/**/*.test.js\"",
"posttest": "eslint --ignore-path .gitignore . && npm audit --audit-level=moderate"
},
"dependencies": {
"@oclif/command": "^1.7.0",
"@oclif/config": "^1.16.0",
Expand All @@ -20,41 +40,23 @@
"tsv": "^0.2.0",
"twilio": "^3.48.1"
},
"optionalDependencies": {
"keytar": "^6.0.1"
},
"devDependencies": {
"@oclif/test": "^1.2.6",
"@twilio/cli-test": "^2.1.0",
"chai": "^4.2.0",
"eslint": "^7.3.1",
"eslint-config-oclif": "^3.1.0",
"eslint-plugin-mocha": "^7.0.1",
"eslint": "^7.5.0",
"eslint-config-twilio": "^1.28.0",
"eslint-config-twilio-mocha": "^1.28.0",
"mocha": "^8.0.1",
"nock": "^13.0.2",
"nyc": "^15.1.0",
"sinon": "^9.0.2",
"tmp": "^0.2.1"
},
"optionalDependencies": {
"keytar": "^6.0.1"
},
"engines": {
"node": ">=10.12.0"
},
"files": [
"/bin",
"/src"
],
"homepage": "https://github.com/twilio/twilio-cli-core",
"keywords": [
"twilio"
],
"license": "MIT",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/twilio/twilio-cli-core.git"
},
"scripts": {
"posttest": "eslint --ignore-path .gitignore . && npm audit --audit-level=moderate",
"test": "nyc --check-coverage --lines 90 --reporter=html --reporter=text mocha --forbid-only \"test/**/*.test.js\""
}
}
51 changes: 32 additions & 19 deletions src/base-commands/base-command.js
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');
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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 error;
}

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);
}

Expand All @@ -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) {
Expand All @@ -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.`);
});
}
Expand All @@ -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;
52 changes: 26 additions & 26 deletions src/base-commands/twilio-client-command.js
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');
Expand Down Expand Up @@ -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);
Expand All @@ -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}`;
}
}

Expand All @@ -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);
Expand All @@ -85,7 +87,7 @@ class TwilioClientCommand extends BaseCommand {
async updateResource(resource, resourceSid, updatedProperties) {
const results = {
sid: resourceSid,
result: '?'
result: '?',
};

updatedProperties = updatedProperties || this.parseProperties();
Expand Down Expand Up @@ -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,
};

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;
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
baseCommands: {
BaseCommand: require('./base-commands/base-command'),
TwilioClientCommand: require('./base-commands/twilio-client-command')
TwilioClientCommand: require('./base-commands/twilio-client-command'),
},
services: {
TwilioApi: require('./services/twilio-api'),
Expand All @@ -13,7 +13,7 @@ module.exports = {
templating: require('./services/messaging/templating'),
namingConventions: require('./services/naming-conventions'),
outputFormats: require('./services/output-formats'),
secureStorage: require('./services/secure-storage')
secureStorage: require('./services/secure-storage'),
},
configureEnv: require('./services/env')
configureEnv: require('./services/env'),
};

0 comments on commit 90ee3f3

Please sign in to comment.