import * as path from 'path';
import config from '../config';

const depBinRE = new RegExp(`${config.pluginRootDir}[\\/]${config.pluginPrefix}.*[/].bin`);

export function cleanEnvPath(...cleanPaths: string[]): string {
  const envPath = process.env.PATH || '';
  const paths = envPath.split(path.delimiter);
  return paths?.filter((p, i) => paths.indexOf(p, 0) === i
    && !cleanPaths.includes(p) && !depBinRE.test(p)).join(path.delimiter);
}

export function addEnvPath(...paths: string[]): string {
  return paths.join(path.delimiter) + path.delimiter + process.env.PATH;
}
