All files / src/commands printenv.js

100% Statements 18/18
100% Branches 4/4
100% Functions 3/3
100% Lines 16/16

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        1x 1x 1x     1x 3x       1x     1x   3x 3x 3x   3x 1x             2x   2x 1x         1x    
/**
 * Prints environment variable values
 * Usage: printenv cwd
 */
import parseOptions from 'parser/option-parser';
import * as OutputFactory from 'emulator-output/output-factory';
import { getEnvironmentVariable } from 'emulator-state/environment-variables';
 
// Converts all key-value pairs of the environment variables to a printable format
const stringifyEnvVariables = (envVariables) => {
  const outputs = envVariables.reduce((outputs, varVal, varKey) => [
    ...outputs, `${varKey}=${varVal}`
  ], []);
 
  return outputs.join('\n');
};
 
export const optDef = {};
 
export default (state, commandOptions) => {
  const {argv} = parseOptions(commandOptions, optDef);
  const envVariables = state.getEnvVariables();
 
  if (argv.length === 0) {
    return {
      output: OutputFactory.makeTextOutput(stringifyEnvVariables(envVariables))
    };
  }
 
  // An argument has been passed to printenv; printenv will only print the first
  // argument provided
  const varValue = getEnvironmentVariable(envVariables, argv[0]);
 
  if (varValue) {
    return {
      output: OutputFactory.makeTextOutput(varValue)
    };
  }
 
  return {};
};