Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
chore: using lodash modules instead of the whole lib (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogs authored Apr 26, 2021
1 parent 9c64218 commit 7488ae1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 52 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
"author": "John McKim <[email protected]>",
"license": "MIT",
"dependencies": {
"lodash": "^4.17.10"
"lodash.merge": "^4.6.2",
"lodash.isstring": "^4.0.1",
"lodash.isobject": "^3.0.2",
"lodash.union": "^4.6.0",
"lodash.upperfirst": "^4.3.1"
},
"devDependencies": {
"@commitlint/config-conventional": "^11.0.0",
Expand Down
7 changes: 7 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
merge: require('lodash.merge'),
isString: require('lodash.isstring'),
isObject: require('lodash.isobject'),
union: require('lodash.union'),
upperFirst: require('lodash.upperfirst'),
};
83 changes: 39 additions & 44 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Try to remove this. Such a large package
const _ = require('lodash');

const { merge, isString, isObject, union, upperFirst } = require('./helpers');
const Naming = require('./naming');
const ExternalStack = require('./external-stack');
const defaultDefinitions = require('./defaults/definitions');
Expand Down Expand Up @@ -41,14 +39,14 @@ class AlertsPlugin {
}

getDefinitions(config) {
return _.merge({}, defaultDefinitions, config.definitions);
return merge({}, defaultDefinitions, config.definitions);
}

getAlarms(alarms, definitions) {
if (!alarms) return [];

return alarms.reduce((result, alarm) => {
if (_.isString(alarm)) {
if (isString(alarm)) {
const definition = definitions[alarm];

if (!definition) {
Expand All @@ -67,9 +65,9 @@ class AlertsPlugin {
}
)
);
} else if (_.isObject(alarm)) {
} else if (isObject(alarm)) {
result.push(
_.merge(
merge(
{
enabled: true,
type: 'static',
Expand All @@ -88,7 +86,7 @@ class AlertsPlugin {
if (!config) throw new Error('Missing config argument');
if (!definitions) throw new Error('Missing definitions argument');

const alarms = _.union(config.alarms, config.global, config.function);
const alarms = union(config.alarms, config.global, config.function);

return this.getAlarms(alarms, definitions);
}
Expand Down Expand Up @@ -189,7 +187,7 @@ class AlertsPlugin {
},
};

if (_.includes(statisticValues, definition.statistic)) {
if (statisticValues.includes(definition.statistic)) {
alarm.Properties.Statistic = definition.statistic;
} else {
alarm.Properties.ExtendedStatistic = definition.statistic;
Expand Down Expand Up @@ -281,10 +279,10 @@ class AlertsPlugin {

_addAlertTopic(key, topics, alertTopics, customAlarmName) {
const topicConfig = topics[key];
const isTopicConfigAnObject = _.isObject(topicConfig);
const isTopicConfigAnObject = isObject(topicConfig);

const topic = isTopicConfigAnObject ? topicConfig.topic : topicConfig;
const isTopicAnObject = _.isObject(topic);
const isTopicAnObject = isObject(topic);

const notifications = isTopicConfigAnObject
? topicConfig.notifications
Expand All @@ -300,8 +298,8 @@ class AlertsPlugin {
}
} else {
const cfRef = `AwsAlerts${
customAlarmName ? _.upperFirst(customAlarmName) : ''
}${_.upperFirst(key)}`;
customAlarmName ? upperFirst(customAlarmName) : ''
}${upperFirst(key)}`;
if (customAlarmName) {
if (!alertTopics[customAlarmName]) {
alertTopics[customAlarmName] = {};
Expand Down Expand Up @@ -413,7 +411,7 @@ class AlertsPlugin {
definitions
);
const alarms = globalAlarms.concat(functionAlarms).map((alarm) =>
_.assign(
Object.assign(
{
nameTemplate: config.nameTemplate,
prefixTemplate: config.prefixTemplate,
Expand Down Expand Up @@ -443,7 +441,7 @@ class AlertsPlugin {
normalizedFunctionName,
functionObj
);
_.merge(statements, logMetricCF);
merge(statements, logMetricCF);
} else {
delete statements[key];
}
Expand Down Expand Up @@ -494,34 +492,31 @@ class AlertsPlugin {
.getAllFunctions()
.map((functionName) => ({ name: functionName }));

const cf = _.chain(dashboardTemplates)
.uniq()
.reduce((acc, d) => {
const dashboard = dashboards.createDashboard(
service.service,
stage,
region,
functions,
d
);
const cf = [...new Set(dashboardTemplates)].reduce((acc, d) => {
const dashboard = dashboards.createDashboard(
service.service,
stage,
region,
functions,
d
);

const cfResource =
d === 'default' ? 'AlertsDashboard' : `AlertsDashboard${d}`;
const dashboardName =
d === 'default'
? `${service.service}-${stage}-${region}`
: `${service.service}-${stage}-${region}-${d}`;

acc[cfResource] = {
Type: 'AWS::CloudWatch::Dashboard',
Properties: {
DashboardName: dashboardName,
DashboardBody: JSON.stringify(dashboard),
},
};
return acc;
}, {})
.value();
const cfResource =
d === 'default' ? 'AlertsDashboard' : `AlertsDashboard${d}`;
const dashboardName =
d === 'default'
? `${service.service}-${stage}-${region}`
: `${service.service}-${stage}-${region}-${d}`;

acc[cfResource] = {
Type: 'AWS::CloudWatch::Dashboard',
Properties: {
DashboardName: dashboardName,
DashboardBody: JSON.stringify(dashboard),
},
};
return acc;
}, {});
this.addCfResources(cf);
}

Expand All @@ -532,7 +527,7 @@ class AlertsPlugin {
return;
}

if (config.stages && !_.includes(config.stages, this.options.stage)) {
if (config.stages && !config.stages.includes(this.options.stage)) {
this.serverless.cli.log(
`Warning: Not deploying alerts on stage ${this.options.stage}`
);
Expand All @@ -555,7 +550,7 @@ class AlertsPlugin {
this.externalStack.mergeResources(resources);
} else {
// Otherwise merge the resources to the main Serverless stack.
_.merge(
merge(
this.serverless.service.provider.compiledCloudFormationTemplate
.Resources,
resources
Expand Down
4 changes: 2 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _ = require('lodash');
const path = require('path');
const { upperFirst } = require('./helpers');

const Plugin = require('./index');

Expand Down Expand Up @@ -35,7 +35,7 @@ const pluginFactory = (alarmsConfig, s) => {
},
getProvider: () => ({
naming: {
getLambdaLogicalId: (name) => `${_.upperFirst(name)}LambdaFunction`,
getLambdaLogicalId: (name) => `${upperFirst(name)}LambdaFunction`,
getLogGroupLogicalId: (name) => name,
getLogGroupName: (name) => `/aws/lambda/${name}`,
getStackName: () => `fooservice-${stage}`,
Expand Down
8 changes: 4 additions & 4 deletions src/naming.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');
const { upperFirst } = require('./helpers');

const getNormalisedName = (name) =>
`${_.upperFirst(name.replace(/-/g, 'Dash').replace(/_/g, 'Underscore'))}`;
`${upperFirst(name.replace(/-/g, 'Dash').replace(/_/g, 'Underscore'))}`;

class Naming {
getAlarmCloudFormationRef(alarmName, prefix) {
Expand All @@ -12,11 +12,11 @@ class Naming {
}

getLogMetricCloudFormationRef(normalizedName, alarmName) {
return `${normalizedName}${_.upperFirst(alarmName)}LogMetricFilter`;
return `${normalizedName}${upperFirst(alarmName)}LogMetricFilter`;
}

getPatternMetricName(metricName, functionName) {
return `${_.upperFirst(metricName)}${functionName}`;
return `${upperFirst(metricName)}${functionName}`;
}

getDimensionsList(dimensionsList, funcRef, omitDefaultDimension) {
Expand Down
27 changes: 26 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,32 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

lodash@^4.14.0, lodash@^4.17.10, lodash@^4.2.0:
lodash.isobject@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d"
integrity sha1-PI+41bW/S/kK4G4U8qUwpO2TXh0=

lodash.isstring@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=

lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.union@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.union/-/lodash.union-4.6.0.tgz#48bb5088409f16f1821666641c44dd1aaae3cd88"
integrity sha1-SLtQiECfFvGCFmZkHETdGqrjzYg=

lodash.upperfirst@^4.3.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/lodash.upperfirst/-/lodash.upperfirst-4.3.1.tgz#1365edf431480481ef0d1c68957a5ed99d49f7ce"
integrity sha1-E2Xt9DFIBIHvDRxolXpe2Z1J984=

lodash@^4.14.0, lodash@^4.2.0:
version "4.17.19"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"

Expand Down

0 comments on commit 7488ae1

Please sign in to comment.