All files / src/util proc.ts

100% Statements 57/57
100% Branches 13/13
100% Functions 3/3
100% Lines 57/57

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 581x 1x 1x 1x 1x 1x 1x 1x 4x 4x 2x 1x 1x 2x 2x 2x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 3x 2x 1x 1x 2x 2x 3x 3x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 1x 1x 3x 1x 1x 1x 4x 4x 4x  
import { Logger } from "./log.js";
import { Either, Left, Maybe, Right } from "purify-ts";
import { hideSecrets } from "./misc.js";
import spawn from "cross-spawn";
import { StdioOptions } from "node:child_process";
import { SpawnSyncOptionsWithStringEncoding } from "child_process";
 
export function setExitCode(code: number, ignoreErrors: boolean, log: Logger) {
  let finalCode = 0;
  if (ignoreErrors) {
    if (code != 0) {
      log.warn(`Ignoring error code ${code.toString()}`);
    }
  } else {
    finalCode = code;
  }
  log.info(`Exit with code ${finalCode.toString()}`);
  process.exitCode = finalCode;
}
 
export function setEnv(
  key: string,
  value: Maybe<string>,
  append: boolean,
  log: Logger,
) {
  value.ifJust(value => {
    if (append && process.env[key]) {
      value = `${process.env[key]} ${value}`;
    }
    log.info(`Setting environment variable ${key} to "${hideSecrets(value)}"`);
    process.env[key] = value;
  });
}
 
export function spawnSync(
  command: string,
  args: string[],
  stdio: Maybe<StdioOptions>,
) {
  const spawnOpts: SpawnSyncOptionsWithStringEncoding = {
    shell: false,
    encoding: "utf-8",
    stdio: stdio.extract(),
  };
  return Either.encase(() => spawn.sync(command, args, spawnOpts)).chain(
    spawn => {
      if (spawn.error) {
        return Left(spawn.error);
      }
      if (spawn.status === null) {
        return Left(Error("Spawn did not complete with status code."));
      }
      return Right({ status: spawn.status, stdout: spawn.stdout });
    },
  );
}