Skip to content

Commit

Permalink
refactor: Replace _.forEach with Object.entries().forEach (#8280)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Sep 24, 2020
1 parent 05d703e commit 76e02cc
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 238 deletions.
21 changes: 11 additions & 10 deletions lib/classes/CLI.js
Expand Up @@ -132,9 +132,11 @@ class CLI {
this.consoleLog(`${indent}${chalk.yellow(command)} ${chalk.dim(dots)} ${usage}`);
}

_.forEach(commandObject.commands, (subcommandObject, subcommand) => {
this.displayCommandUsage(subcommandObject, `${command} ${subcommand}`, indents);
});
if (commandObject.commands) {
Object.entries(commandObject.commands).forEach(([subcommand, subcommandObject]) => {
this.displayCommandUsage(subcommandObject, `${command} ${subcommand}`, indents);
});
}
}

displayCommandOptions(commandObject) {
Expand All @@ -149,7 +151,7 @@ class CLI {
})
: commandObject.options;

_.forEach(commandOptions, (optionsObject, option) => {
Object.entries(commandOptions).forEach(([option, optionsObject]) => {
let optionsDots = '.'.repeat(Math.max(dotsLength - option.length, 0));
const optionsUsage = optionsObject.usage;

Expand Down Expand Up @@ -231,7 +233,7 @@ class CLI {

this.consoleLog('');
if (Object.keys(this.loadedCommands).length) {
_.forEach(this.loadedCommands, (details, command) => {
Object.entries(this.loadedCommands).forEach(([command, details]) => {
this.displayCommandUsage(details, command);
});
} else {
Expand Down Expand Up @@ -291,25 +293,24 @@ functionalities related to given service or current environment.`

// check for subcommands
if ('commands' in cmd) {
_.forEach(cmd.commands, d => {
Object.values(cmd.commands).forEach(d => {
addToPluginCommands(d);
});
}
};

// fill up pluginCommands with commands in loadedCommands
_.forEach(this.loadedCommands, details => {
Object.values(this.loadedCommands).forEach(details => {
addToPluginCommands(details);
});

// sort plugins alphabetically
pluginCommands = _(pluginCommands)
.toPairs()
pluginCommands = _(Object.entries(pluginCommands))
.sortBy(0)
.fromPairs()
.value();

_.forEach(pluginCommands, (details, plugin) => {
Object.entries(pluginCommands).forEach(([plugin, details]) => {
this.consoleLog(plugin);
details.forEach(cmd => {
// display command usage with single(1) indent
Expand Down
142 changes: 76 additions & 66 deletions lib/classes/PluginManager.js
Expand Up @@ -261,44 +261,48 @@ class PluginManager {

loadCommands(pluginInstance) {
const pluginName = pluginInstance.constructor.name;
_.forEach(pluginInstance.commands, (details, key) => {
const command = this.loadCommand(pluginName, details, key);
// Grab and extract deprecated events
command.lifecycleEvents = (command.lifecycleEvents || []).map(event => {
if (event.startsWith('deprecated#')) {
// Extract event and optional redirect
const transformedEvent = /^deprecated#(.*?)(?:->(.*?))?$/.exec(event);
this.deprecatedEvents[`${command.key}:${transformedEvent[1]}`] =
transformedEvent[2] || null;
return transformedEvent[1];
}
return event;
if (pluginInstance.commands) {
Object.entries(pluginInstance.commands).forEach(([key, details]) => {
const command = this.loadCommand(pluginName, details, key);
// Grab and extract deprecated events
command.lifecycleEvents = (command.lifecycleEvents || []).map(event => {
if (event.startsWith('deprecated#')) {
// Extract event and optional redirect
const transformedEvent = /^deprecated#(.*?)(?:->(.*?))?$/.exec(event);
this.deprecatedEvents[`${command.key}:${transformedEvent[1]}`] =
transformedEvent[2] || null;
return transformedEvent[1];
}
return event;
});
this.commands[key] = _.merge({}, this.commands[key], command);
});
this.commands[key] = _.merge({}, this.commands[key], command);
});
}
}

loadHooks(pluginInstance) {
const pluginName = pluginInstance.constructor.name;
_.forEach(pluginInstance.hooks, (hook, event) => {
let target = event;
const baseEvent = event.replace(/^(?:after:|before:)/, '');
if (this.deprecatedEvents[baseEvent]) {
const redirectedEvent = this.deprecatedEvents[baseEvent];
if (process.env.SLS_DEBUG) {
this.serverless.cli.log(`WARNING: Plugin ${pluginName} uses deprecated hook ${event},
if (pluginInstance.hooks) {
Object.entries(pluginInstance.hooks).forEach(([event, hook]) => {
let target = event;
const baseEvent = event.replace(/^(?:after:|before:)/, '');
if (this.deprecatedEvents[baseEvent]) {
const redirectedEvent = this.deprecatedEvents[baseEvent];
if (process.env.SLS_DEBUG) {
this.serverless.cli.log(`WARNING: Plugin ${pluginName} uses deprecated hook ${event},
use ${redirectedEvent} hook instead`);
}
if (redirectedEvent) {
target = event.replace(baseEvent, redirectedEvent);
}
}
if (redirectedEvent) {
target = event.replace(baseEvent, redirectedEvent);
}
}
this.hooks[target] = this.hooks[target] || [];
this.hooks[target].push({
pluginName,
hook,
this.hooks[target] = this.hooks[target] || [];
this.hooks[target].push({
pluginName,
hook,
});
});
});
}
}

loadVariableResolvers(pluginInstance) {
Expand Down Expand Up @@ -560,31 +564,33 @@ class PluginManager {
}

validateOptions(command) {
_.forEach(command.options, (value, key) => {
if (value.required && (this.cliOptions[key] === true || !this.cliOptions[key])) {
let requiredThings = `the --${key} option`;
if (command.options) {
Object.entries(command.options).forEach(([key, value]) => {
if (value.required && (this.cliOptions[key] === true || !this.cliOptions[key])) {
let requiredThings = `the --${key} option`;

if (value.shortcut) {
requiredThings += ` / -${value.shortcut} shortcut`;
}
let errorMessage = `This command requires ${requiredThings}.`;
if (value.shortcut) {
requiredThings += ` / -${value.shortcut} shortcut`;
}
let errorMessage = `This command requires ${requiredThings}.`;

if (value.usage) {
errorMessage = `${errorMessage} Usage: ${value.usage}`;
}
if (value.usage) {
errorMessage = `${errorMessage} Usage: ${value.usage}`;
}

throw new this.serverless.classes.Error(errorMessage);
}
throw new this.serverless.classes.Error(errorMessage);
}

if (
_.isPlainObject(value.customValidation) &&
value.customValidation.regularExpression instanceof RegExp &&
typeof value.customValidation.errorMessage === 'string' &&
!value.customValidation.regularExpression.test(this.cliOptions[key])
) {
throw new this.serverless.classes.Error(value.customValidation.errorMessage);
}
});
if (
_.isPlainObject(value.customValidation) &&
value.customValidation.regularExpression instanceof RegExp &&
typeof value.customValidation.errorMessage === 'string' &&
!value.customValidation.regularExpression.test(this.cliOptions[key])
) {
throw new this.serverless.classes.Error(value.customValidation.errorMessage);
}
});
}
}

updateAutocompleteCacheFile() {
Expand All @@ -594,7 +600,7 @@ class PluginManager {
validationHash: '',
};

_.forEach(commands, (commandObj, commandName) => {
Object.entries(commands).forEach(([commandName, commandObj]) => {
const command = commandObj;
if (!command.options) {
command.options = {};
Expand All @@ -618,23 +624,27 @@ class PluginManager {
}

convertShortcutsIntoOptions(command) {
_.forEach(command.options, (optionObject, optionKey) => {
if (optionObject.shortcut && Object.keys(this.cliOptions).includes(optionObject.shortcut)) {
Object.keys(this.cliOptions).forEach(option => {
if (option === optionObject.shortcut) {
this.cliOptions[optionKey] = this.cliOptions[option];
}
});
}
});
if (command.options) {
Object.entries(command.options).forEach(([optionKey, optionObject]) => {
if (optionObject.shortcut && Object.keys(this.cliOptions).includes(optionObject.shortcut)) {
Object.keys(this.cliOptions).forEach(option => {
if (option === optionObject.shortcut) {
this.cliOptions[optionKey] = this.cliOptions[option];
}
});
}
});
}
}

assignDefaultOptions(command) {
_.forEach(command.options, (value, key) => {
if (value.default != null && (!this.cliOptions[key] || this.cliOptions[key] === true)) {
this.cliOptions[key] = value.default;
}
});
if (command.options) {
Object.entries(command.options).forEach(([key, value]) => {
if (value.default != null && (!this.cliOptions[key] || this.cliOptions[key] === true)) {
this.cliOptions[key] = value.default;
}
});
}
}

asyncPluginInit() {
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Service.js
Expand Up @@ -178,7 +178,7 @@ class Service {

// setup function.name property
const stageNameForFunction = options.stage || this.provider.stage;
_.forEach(that.functions, (functionObj, functionName) => {
Object.entries(that.functions).forEach(([functionName, functionObj]) => {
if (!functionObj.events) {
that.functions[functionName].events = [];
}
Expand Down
3 changes: 1 addition & 2 deletions lib/plugins/aws/info/display.js
@@ -1,7 +1,6 @@
'use strict';

const chalk = require('chalk');
const _ = require('lodash');

module.exports = {
displayServiceInfo() {
Expand Down Expand Up @@ -56,7 +55,7 @@ module.exports = {
info.endpoints.forEach(endpoint => {
// if the endpoint is of type http(s)
if (endpoint.startsWith('https://')) {
_.forEach(this.serverless.service.functions, functionObject => {
Object.values(this.serverless.service.functions).forEach(functionObject => {
functionObject.events.forEach(event => {
if (event.http) {
let method;
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/aws/lib/normalizeFiles.js
Expand Up @@ -6,7 +6,7 @@ module.exports = {
normalizeCloudFormationTemplate(template) {
const normalizedTemplate = _.cloneDeep(template);

_.forEach(normalizedTemplate.Resources, (value, key) => {
Object.entries(normalizedTemplate.Resources).forEach(([key, value]) => {
if (key.startsWith('ApiGatewayDeployment')) {
delete Object.assign(normalizedTemplate.Resources, {
ApiGatewayDeployment: normalizedTemplate.Resources[key],
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/aws/package/compile/events/alb/lib/validate.js
Expand Up @@ -29,7 +29,7 @@ module.exports = {

const events = [];

_.forEach(this.serverless.service.functions, (functionObject, functionName) => {
Object.entries(this.serverless.service.functions).forEach(([functionName, functionObject]) => {
functionObject.events.forEach(event => {
if (event.alb) {
if (_.isObject(event.alb)) {
Expand Down

0 comments on commit 76e02cc

Please sign in to comment.