All files / recipes/platformsh builder.js

0% Statements 0/36
0% Branches 0/5
0% Functions 0/9
0% Lines 0/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                                                                                                                                                                               
'use strict';
 
// Modules
const _ = require('lodash');
const {getLandoServices} = require('./../../lib/services');
const {getLandoProxyRoutes} = require('./../../lib/proxy');
const {getPlatformPull} = require('./../../lib/pull');
const {getPlatformPush} = require('./../../lib/push');
const tooling = require('./../../lib/tooling');
 
/*
 * Build Platformsh
 */
module.exports = {
  name: 'platformsh',
  parent: '_recipe',
  config: {
    confSrc: __dirname,
    proxy: {},
    services: {},
    tooling: {},
  },
  builder: (parent, config) => class LandoPlatformsh extends parent {
    constructor(id, options = {}) {
      // Get our options
      options = _.merge({}, config, options);
      // Get the platformConfig weve loaded and parsed
      const platformConfig = _.get(options, '_app.platformsh', {});
      // Combine application and service containers
      const services = _.flatten([platformConfig.applications, platformConfig.services]);
 
      // Map into lando services
      options.services = getLandoServices(services, platformConfig.runConfig);
      // Map into lando proxy routes
      const supported = _.map(options.services, service => ({name: service.name, port: service.proxyPort}));
      options.proxy = getLandoProxyRoutes(platformConfig.routes, supported);
 
      // Get the service version of our closest application
      const closestApp = _.find(options.services, service => {
        return service.name === platformConfig.closestApp.name;
      });
 
      // Only add tooling if we actually have a closest app
      // @NOTE: when we fully support all platform.sh languages we _may_
      // we able to remove this guard
      if (closestApp) {
        // Get the app tooling
        const applicationTooling = tooling.getAppTooling(closestApp);
        // Get relatable services
        const openData = options._app._lando.cache.get(`${options._app.name}.${closestApp.name}.open.cache`);
        const relatableServices = tooling.getRelatableServices(openData);
        const serviceContainers = _(options.services)
          .filter(service => _.includes(relatableServices, service.name))
          .map(service => service)
          .value();
        const serviceTooling = tooling.getServiceTooling(serviceContainers, openData, closestApp.name);
 
        // Merge and set the lando tooling
        options.tooling = _.merge({}, applicationTooling, serviceTooling);
 
        // Add in the pull scripts
        options.tooling.pull = getPlatformPull(closestApp.name, options._app);
        // Add in relationship envvars
        options.tooling.pull.env = _(serviceTooling)
          // Get connect strings and merge with any env set by the command eg PG_PASS
          .map((data, name) => ([
            [
              [_.toUpper(`LANDO_CONNECT_${name}`), data.connect],
              [_.toUpper(`LANDO_CONNECT_${name}_DEFAULT_SCHEMA`), data.default],
            ],
            _.toPairs(data.env),
          ]))
          // Level it all off and convert back to object
          .flatten()
          .flatten()
          .fromPairs()
          .value();
 
        // Add in the pull scripts
        options.tooling.push = getPlatformPush(closestApp.name, options._app);
        // Add in relationship envvars
        options.tooling.push.env = _(serviceTooling)
          // Get connect strings and merge with any env set by the command eg PG_PASS
          .map((data, name) => ([
            [
              [_.toUpper(`LANDO_DUMP_${name}`), data.dump],
              [_.toUpper(`LANDO_CONNECT_${name}_DEFAULT_SCHEMA`), data.default],
            ],
            _.toPairs(data.env),
          ]))
          // Level it all off and convert back to object
          .flatten()
          .flatten()
          .fromPairs()
          .value();
      }
 
      // If we have a token lets set that into all our CLI tools
      if (_.has(options, '_app.meta.token')) {
        _.forEach(options.tooling, config => {
          config.env = _.merge({}, config.env, {
            PLATFORMSH_CLI_TOKEN: options._app.meta.token,
          });
        });
      }
 
      // Send downstream
      super(id, options);
    };
  },
};