All files / if-check/util npm.ts

100% Statements 31/31
100% Branches 43/43
100% Functions 2/2
100% Lines 29/29

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 981x 1x 1x 1x   1x         8x         1x 4x 4x 4x 4x     4x       4x 4x   4x                 4x                     4x 4x                     4x         4x       4x       4x       4x 4x       4x           4x 4x     4x   4x    
import * as path from 'node:path';
import {execFileSync} from 'child_process';
import {getFileName, removeFileIfExists} from '../../common/util/fs';
import {STRINGS} from '../config';
 
const {IF_CHECK_VERIFIED} = STRINGS;
 
/**
 * Escapes shell characters that could be dangerous in a command.
 */
const escapeShellArg = (str: string) => str.replace(/([`$\\&;|*?<>])/g, '\\$1');
 
/**
 * Executes a series of npm commands based on the provided manifest file.
 */
export const executeCommands = async (manifest: string, cwd: boolean) => {
  const isGlobal = !!process.env.npm_config_global;
  const manifestDirPath = path.dirname(manifest);
  const manifestFileName = getFileName(manifest);
  const executedManifest = path.join(manifestDirPath, `re-${manifestFileName}`);
 
  const prefixFlag =
    process.env.CURRENT_DIR && process.env.CURRENT_DIR !== process.cwd()
      ? `--prefix=${path.relative(process.env.CURRENT_DIR!, process.cwd())}`
      : '';
 
  const sanitizedManifest = escapeShellArg(manifest);
  const sanitizedExecutedManifest = escapeShellArg(executedManifest);
 
  const ifEnvCommand = [
    isGlobal ? 'if-env' : 'npm',
    ...(isGlobal ? [] : ['run', 'if-env']),
    isGlobal ? '' : '--',
    ...(prefixFlag === '' ? [] : [prefixFlag]),
    '-m',
    sanitizedManifest,
  ];
 
  const ifRunCommand = [
    isGlobal ? 'if-run' : 'npm',
    ...(isGlobal ? [] : ['run', 'if-run']),
    isGlobal ? '' : '--',
    ...(prefixFlag === '' ? [] : [prefixFlag]),
    '-m',
    sanitizedManifest,
    '-o',
    sanitizedExecutedManifest,
  ];
 
  const ttyCommand = ['node', '-p', 'Boolean(process.stdout.isTTY)'];
  const ifDiffCommand = [
    isGlobal ? 'if-diff' : 'npm',
    ...(isGlobal ? [] : ['run', 'if-diff']),
    isGlobal ? '' : '--',
    ...(prefixFlag === '' ? [] : [prefixFlag]),
    '-s',
    `${sanitizedExecutedManifest}.yaml`,
    '-t',
    sanitizedManifest,
  ];
 
  execFileSync(ifEnvCommand[0], ifEnvCommand.slice(1), {
    cwd: process.env.CURRENT_DIR || process.cwd(),
    shell: true,
  });
 
  execFileSync(ifRunCommand[0], ifRunCommand.slice(1), {
    cwd: process.env.CURRENT_DIR || process.cwd(),
  });
 
  execFileSync(ttyCommand[0], ttyCommand.slice(1), {
    cwd: process.env.CURRENT_DIR || process.cwd(),
  });
 
  const ttyResult = execFileSync(ttyCommand[0], ttyCommand.slice(1), {
    cwd: process.env.CURRENT_DIR || process.cwd(),
  });
 
  const tty = ttyResult && ttyResult.toString().trim();
  const fullCommand = `${tty === 'true' ? 'tty |' : ''} ${ifDiffCommand.join(
    ' '
  )}`;
 
  execFileSync(fullCommand, {
    cwd: process.env.CURRENT_DIR || process.cwd(),
    stdio: 'inherit',
    shell: true,
  });
 
  if (!cwd) {
    await removeFileIfExists(`${manifestDirPath}/package.json`);
  }
 
  await removeFileIfExists(`${executedManifest}.yaml`);
 
  console.log(IF_CHECK_VERIFIED(path.basename(manifest)));
};