Skip to content

Commit

Permalink
removed lodash dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeledy committed Feb 27, 2025
1 parent fd5579e commit 188f4b5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## {{ UNRELEASED_VERSION }} - [{{ UNRELEASED_DATE }}]({{ UNRELEASED_LINK }})

- Removed `lando mailpit` command
- Removed `lodash` dependency

## v1.0.0-beta.5 - [February 12, 2025](https://github.com/lando/mailpit/releases/tag/v1.0.0-beta.5)

Expand Down
52 changes: 35 additions & 17 deletions builders/mailpit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* This module exports a configuration object for the Mailpit service in Lando.
*/

const _ = require('lodash');
const path = require('path');
const addBuildStep = require('../utils/addBuildStep');
const setConfigOptions = require('../utils/setConfigOptions');
Expand Down Expand Up @@ -93,7 +92,9 @@ module.exports = {
const debug = userConfig._app.log.debug;

// Merge the user config onto the default config
const options = _.merge({}, defaultConfig, userConfig);
/** @type {import('@lando/core/lib/utils').merge} */
const merge = userConfig._app._lando.utils.merge;
const options = merge(defaultConfig, userConfig);

/**
* Mailpit service configuration
Expand Down Expand Up @@ -152,29 +153,46 @@ module.exports = {
} else if (service === options.name) {
debug(`Skipping mail configuration for ${service} as it is the mailpit service itself.`);
}
return exists;
return exists && service !== options.name;
});

// Create sources for mail configuration
const sources = [];

validServices.forEach(service => {
debug(`Configuring mail settings for ${service}`);
options.sources.push({
services: _.set({}, service, {
environment: {
MAIL_HOST: options.name,
MAIL_PORT: options.port,
},
volumes: [
`${options.confDest}/php.ini:/usr/local/etc/php/conf.d/zzzz-lando-mailpit.ini`,
],
}),
});
const serviceConfig = {
services: {}

Check failure on line 165 in builders/mailpit.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Missing trailing comma

Check failure on line 165 in builders/mailpit.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Missing trailing comma
};

Check failure on line 167 in builders/mailpit.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Trailing spaces not allowed

Check failure on line 167 in builders/mailpit.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Trailing spaces not allowed
serviceConfig.services[service] = {
environment: {
MAIL_HOST: options.name,
MAIL_PORT: options.port,
},
volumes: [
`${options.confDest}/php.ini:/usr/local/etc/php/conf.d/zzzz-lando-mailpit.ini`,
],
};

Check failure on line 177 in builders/mailpit.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Trailing spaces not allowed

Check failure on line 177 in builders/mailpit.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Trailing spaces not allowed
sources.push(serviceConfig);
});

// Add the Mailpit service to the sources
options.sources.push({services: _.set({}, options.name, mailpit)});
const mailpitServiceConfig = {
services: {}

Check failure on line 183 in builders/mailpit.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Missing trailing comma

Check failure on line 183 in builders/mailpit.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Missing trailing comma
};
mailpitServiceConfig.services[options.name] = mailpit;
sources.push(mailpitServiceConfig);

// Combine existing sources with new sources
options.sources = [...options.sources, ...sources];

// Flatten the sources array if needed
const flattenedSources = sources.flat();

// Call the parent constructor with the processed options
super(id, options, ..._.flatten(options.sources));
// Call the parent constructor with the processed options and flattened sources
super(id, options, ...flattenedSources);
}
},
};
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
"test:leia": "npm run leia \"examples/**/README.md\" -c 'Destroy tests' --stdin",
"test": "npm run lint && npm run test:unit"
},
"dependencies": {
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/eslint-parser": "^7.25.7",
"@lando/core": "^3",
Expand Down
18 changes: 5 additions & 13 deletions test/mailpit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
* builder, including default options, environment variables, and other
* configurations. Run with `npm run test:unit`
*
* @requires _
* @requires chai
* @requires builders/mailpit
*/

/** @type {import('lodash')} */
const _ = require('lodash');
const chai = require('chai');
const expect = chai.expect;
const mailpitBuilder = require('../builders/mailpit');
Expand All @@ -28,7 +25,8 @@ describe('Mailpit Builder', function() {
mockParent = class MockParent {
constructor(id, options = {}, ...sources) {
this.id = id;
this.options = _.merge({}, options);
// Create a deep copy of options
this.options = JSON.parse(JSON.stringify(options));
this.sources = sources;
}
};
Expand All @@ -53,6 +51,9 @@ describe('Mailpit Builder', function() {
log: {
debug: () => {}, // Mock debug function
},
_lando: {
utils: require('@lando/core/lib/utils')

Check failure on line 55 in test/mailpit.spec.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Missing trailing comma

Check failure on line 55 in test/mailpit.spec.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Missing trailing comma
}

Check failure on line 56 in test/mailpit.spec.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Missing trailing comma

Check failure on line 56 in test/mailpit.spec.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Missing trailing comma
},
};
});
Expand Down Expand Up @@ -157,13 +158,4 @@ describe('Mailpit Builder', function() {
expect(instance.options.maxMessages).to.equal(2012);
expect(instance.options.port).to.equal(2025);
});

it('should initialize sources array even when not provided in options', () => {
const optionsWithoutSources = _.omit(mockOptions, 'sources');
const LandoMailpitService = mailpitBuilder.builder(mockParent, mailpitBuilder.config);
const instance = new LandoMailpitService('smtpserver', optionsWithoutSources);

expect(instance.options.sources).to.be.an('array');
expect(instance.options.sources).to.have.lengthOf(2);
});
});
27 changes: 22 additions & 5 deletions utils/addBuildStep.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const _ = require('lodash');

/**
* Adds build steps to a service
* @param {string|string[]} steps - The build step(s) to add
Expand All @@ -19,7 +17,26 @@ module.exports = (steps, app, name, step = 'build_internal', front = false) => {
throw new Error(`Invalid build step type: ${step}`);
}

const current = _.get(app, `config.services.${name}.${step}`, []);
const add = (front) ? _.flatten([steps, current]) : _.flatten([current, steps]);
_.set(app, `config.services.${name}.${step}`, _.uniq(add));
// Get current steps or empty array if none exist
const current = app.config.services?.[name]?.[step] || [];

Check failure on line 22 in utils/addBuildStep.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Trailing spaces not allowed

Check failure on line 22 in utils/addBuildStep.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Trailing spaces not allowed
// Convert steps to array if it's a string
const stepsArray = Array.isArray(steps) ? steps : [steps];

Check failure on line 25 in utils/addBuildStep.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Trailing spaces not allowed

Check failure on line 25 in utils/addBuildStep.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Trailing spaces not allowed
// Combine arrays based on front parameter
const combined = front ? [...stepsArray, ...current] : [...current, ...stepsArray];

Check failure on line 28 in utils/addBuildStep.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Trailing spaces not allowed

Check failure on line 28 in utils/addBuildStep.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Trailing spaces not allowed
// Remove duplicates
const uniqueSteps = [...new Set(combined)];

Check failure on line 31 in utils/addBuildStep.js

View workflow job for this annotation

GitHub Actions / docs-tests (ubuntu-24.04, 20)

Trailing spaces not allowed

Check failure on line 31 in utils/addBuildStep.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-24.04, 20)

Trailing spaces not allowed
// Ensure the path exists and set the value
if (!app.config.services) {
app.config.services = {};
}

if (!app.config.services[name]) {
app.config.services[name] = {};
}

app.config.services[name][step] = uniqueSteps;
};
18 changes: 15 additions & 3 deletions utils/setConfigOptions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
'use strict';

const _ = require('lodash');

/**
* Sets configuration options for a service
* @param {object} options - The configuration options to set
* @param {object} app - The Lando app object
* @param {string} name - The name of the service
*/
module.exports = (options, app, name) => {
// Ensure the services and service name paths exist
if (!app.config) {
app.config = {};
}

if (!app.config.services) {
app.config.services = {};
}

if (!app.config.services[name]) {
app.config.services[name] = {};
}

// Set each option on the service
Object.entries(options).forEach(([key, value]) => {
_.set(app, `config.services.${name}.${key}`, value);
app.config.services[name][key] = value;
});
};

0 comments on commit 188f4b5

Please sign in to comment.