All files / lib utils.js

0% Statements 0/32
0% Branches 0/13
0% Functions 0/17
0% Lines 0/26

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                                                                                                                                                           
'use strict';
 
// Modules
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
 
/*
 * Helper to filter out services from application containers
 */
exports.getApplicationServices = (services = []) => _(services)
  // Filter out non psh containers
  .filter(service => !_.isEmpty(service.platformsh))
  // Filter out psh services
  .filter(service => service.platformsh.application)
  .value();
 
/*
 * Helper to get the applications doc root
 */
exports.getDocRoot = appConfig => {
  if (_.has(appConfig, 'web.locations./.root')) {
    return `/app/${appConfig.web.locations['/'].root}`;
  }
  return '/app';
};
 
/*
 * Helper to filter out services from application containers
 */
exports.getNonApplicationServices = (services = []) => _(services)
  // Filter out non psh containers
  .filter(service => !_.isEmpty(service.platformsh))
  // Filter out psh application containers
  .filter(service => !service.platformsh.application)
  .value();
 
/*
 * Helper to get terminus tokens
 */
exports.getPlatformshTokens = home => {
  if (fs.existsSync(path.join(home, '.platformsh', 'cache', 'tokens'))) {
    return _(fs.readdirSync(path.join(home, '.platformsh', 'cache', 'tokens')))
      .map(tokenFile => path.join(home, '.platformsh', 'cache', 'tokens', tokenFile))
      .map(file => JSON.parse(fs.readFileSync(file, 'utf8')))
      .value();
  } else {
    return [];
  }
};
 
/*
 * Helper to prefix commands
 */
exports.setPshExec = (cmds = [], fallback = 'app', needsPrefix = []) => _(cmds)
  // Set service if needed
  .map(cmd => _.isString(cmd) ? _.set({}, fallback, cmd) : cmd)
  // Extract the service data
  .map(cmd => _.map(cmd, (cmd, service) => ([service, cmd])))
  // Flatten the thing
  .flatten()
  // Prefix if needed
  .map(data => {
    if (_.includes(needsPrefix, data[0])) data[1] = `/helpers/psh-exec.sh ${data[1]}`;
    return _.set({}, data[0], data[1]);
  })
  // Return
  .value();
 
/*
 * Helper to return most recent tokens
 */
exports.sortTokens = (...sources) => _(_.flatten([...sources]))
  .sortBy('date')
  .groupBy('email')
  .map(tokens => _.last(tokens))
  .value();