All files run.ts

100% Statements 40/40
100% Branches 16/16
100% Functions 3/3
100% Lines 38/38

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 832x 2x   2x 2x 2x 2x                     2x 7x   9x   6x   6x 2x 2x     4x 1x 1x     3x 3x   3x 3x   3x 3x   3x             3x 3x   3x                     3x 3x   3x     2x 3x 3x 3x 3x 3x 3x   3x     2x  
import * as childProcess from 'child_process';
import * as colors from 'colors/safe';
import { Tree } from 'jargs';
import * as path from 'path';
import { DEFAULT_ENV, DISAPPROVAL, WAT } from './constants';
import * as logger from './logger';
import {
  getConfigPath,
  getEnvVariables,
  getIn,
  getProjectName,
  handleShebang,
  injectEnvVars,
  loadWtfJson,
  onClose,
} from './utils';
 
export const runCommand = (
  commandAndOptions: undefined | ReadonlyArray<string | undefined> = [],
  env: string,
  rest: undefined | ReadonlyArray<string | undefined> = []
) => {
  const [command, ...commandOptions] = commandAndOptions;
 
  if (typeof command === 'undefined') {
    logger.log('No command supplied');
    return process.exit(1);
  }
 
  if (command === 'wtf') {
    logger.log(colors.red(WAT + 'Wat are you doing? ' + DISAPPROVAL));
    return process.exit(1);
  }
 
  const configPath = getConfigPath();
  const projectName = getProjectName();
 
  const wtfJson = loadWtfJson(configPath, projectName, env);
  const configEnvVariables = getIn(wtfJson, [projectName, 'env', env]) || {};
 
  const envPath = path.join(process.cwd(), 'etc/environments', env, 'env');
  const envVariables = getEnvVariables(envPath);
 
  const environment: {[i: string]: string} = {
    ...envVariables,
    ...configEnvVariables,
    ...process.env,
    PYTHONUNBUFFERED: 'true',
  };
 
  const resolvedCommand = handleShebang(command);
  const resolvedCommandOptions = injectEnvVars([...commandOptions, ...rest], environment);
 
  const subProcess = childProcess.spawn(
    resolvedCommand,
    resolvedCommandOptions,
    {
      cwd: process.cwd(),
      shell: true,
      env: environment,
      stdio: 'inherit',
    }
  );
 
  logger.log(colors.green(`Running ${resolvedCommand} ${resolvedCommandOptions.join(' ')}`));
  logger.log(colors.green(`PID: ${subProcess.pid}, Parent PID: ${process.pid}\n`));
 
  subProcess.on('close', (code) => onClose('', code));
};
 
const run = (tree: Tree) => {
  let { command } = tree.args;
  let { env } = tree.kwargs;
  let { rest } = tree;
  command = Array.isArray(command) ? command : [];
  rest = Array.isArray(rest) ? rest : [];
  env = typeof env === 'string' ? env : DEFAULT_ENV;
 
  runCommand(command, env, rest);
};
 
export default run;