Skip to content

Commit 9b7333f

Browse files
authored
Merge pull request #92 from Automattic/integrate-compose
refactor: integrate `compose` plugin into lando and fix paths in `env_file`
2 parents 0893250 + 2433e20 commit 9b7333f

File tree

7 files changed

+112
-100
lines changed

7 files changed

+112
-100
lines changed

.github/workflows/pr-plugins-installed.yml

Lines changed: 0 additions & 92 deletions
This file was deleted.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
}
102102
},
103103
"dependencies": {
104-
"@lando/compose": "1.0.0",
105104
"axios": "^1.7.2",
106105
"bluebird": "^3.4.1",
107106
"cli-table3": "^0.6.5",

plugins/lando-compose/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = lando => {};

plugins/lando-compose/lib/utils.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
// Modules
4+
const _ = require('lodash');
5+
const path = require('path');
6+
7+
/*
8+
* Helper method to get the host part of a volume
9+
*/
10+
exports.getHostPath = mount => _.dropRight(mount.split(':')).join(':');
11+
12+
/*
13+
* Helper method to normalize a path so that Lando overrides can be used as though
14+
* the docker-compose files were in the app root.
15+
*/
16+
exports.normalizePath = (local, base = '.', excludes = []) => {
17+
// Return local if it starts with $ or ~
18+
if (_.startsWith(local, '$') || _.startsWith(local, '~')) return local;
19+
// Return local if it is one of the excludes
20+
if (_.includes(excludes, local)) return local;
21+
// Return local if local is an absolute path
22+
if (path.isAbsolute(local)) return local;
23+
// Otherwise this is a relaive path so return local resolved by base
24+
return path.resolve(path.join(base, local));
25+
};
26+
27+
/*
28+
* Helper to normalize overrides
29+
*/
30+
exports.normalizeOverrides = (overrides, base = '.', volumes = {}) => {
31+
// Normalize any build paths
32+
if (_.has(overrides, 'build')) {
33+
if (_.isObject(overrides.build) && _.has(overrides, 'build.context')) {
34+
overrides.build.context = exports.normalizePath(overrides.build.context, base);
35+
} else {
36+
overrides.build = exports.normalizePath(overrides.build, base);
37+
}
38+
}
39+
// Normalize any volumes
40+
if (_.has(overrides, 'volumes')) {
41+
overrides.volumes = _.map(overrides.volumes, volume => {
42+
if (!_.includes(volume, ':')) {
43+
return volume;
44+
} else {
45+
const local = exports.getHostPath(volume);
46+
const remote = _.last(volume.split(':'));
47+
// @TODO: I don't think below does anything?
48+
const excludes = _.keys(volumes).concat(_.keys(volumes));
49+
const host = exports.normalizePath(local, base, excludes);
50+
return [host, remote].join(':');
51+
}
52+
});
53+
}
54+
55+
if (overrides.env_file) {
56+
if (Array.isArray(overrides.env_file)) {
57+
overrides.env_file = overrides.env_file.map(entry => {
58+
if (typeof entry === 'string') {
59+
return exports.normalizePath(entry, base);
60+
}
61+
62+
if (typeof entry === 'object' && typeof entry.path === 'string') {
63+
return {
64+
...entry,
65+
path: exports.normalizePath(entry.path, base),
66+
};
67+
}
68+
69+
return entry;
70+
});
71+
} else {
72+
overrides.env_file = exports.normalizePath(overrides.env_file, base);
73+
}
74+
}
75+
76+
return overrides;
77+
};

plugins/lando-compose/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name: "@lando/compose"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// Modules
4+
const _ = require('lodash');
5+
const utils = require('./../../lib/utils');
6+
7+
// Builder
8+
module.exports = {
9+
name: 'compose',
10+
config: {
11+
version: 'custom',
12+
services: {},
13+
networks: {},
14+
volumes: {},
15+
},
16+
parent: '_lando',
17+
builder: (parent, config) => class LandoCompose extends parent {
18+
constructor(id, options = {}) {
19+
options = _.merge({}, config, options);
20+
super(id, options, {
21+
services: _.set(
22+
{},
23+
options.name,
24+
utils.normalizeOverrides(options.services, options._app.root, options.volumes),
25+
),
26+
networks: options.networks,
27+
volumes: options.volumes,
28+
});
29+
};
30+
},
31+
};

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,6 @@
285285
"@jridgewell/resolve-uri" "^3.1.0"
286286
"@jridgewell/sourcemap-codec" "^1.4.14"
287287

288-
"@lando/compose@1.0.0":
289-
version "1.0.0"
290-
resolved "https://registry.yarnpkg.com/@lando/compose/-/compose-1.0.0.tgz#1e24bdfe39fb536f43e86aa179c4671d62ffb771"
291-
integrity sha512-n7EelwK+1T22ueYiOWpniBo5508QJtGsXHENIdnew9e30T33GRUbXCeK7at+dv8CvpqgKsw76lD2Antna9b/Zg==
292-
dependencies:
293-
lodash "^4.17.21"
294-
295288
"@nicolo-ribaudo/eslint-scope-5-internals@5.1.1-v1":
296289
version "5.1.1-v1"
297290
resolved "https://registry.yarnpkg.com/@nicolo-ribaudo/eslint-scope-5-internals/-/eslint-scope-5-internals-5.1.1-v1.tgz#dbf733a965ca47b1973177dc0bb6c889edcfb129"

0 commit comments

Comments
 (0)