|
| 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 | +}; |
0 commit comments