<commit_before>// @flow
import includes from 'array-includes';
import projectBinPath from 'project-bin-path';
import * as path from 'path';
import type { Dependency, configDependencyType } from '../types';
import type Package from '../Package';
import Project from '../Project';
import * as processes from './processes';
import * as fs from '../utils/fs';
import * as logger from '../utils/fs';
import { DEPENDENCY_TYPE_FLAGS_MAP, BOLT_VERSION } from '../constants';

function getLocalBinPath(): Promise<string> {
  return projectBinPath(__dirname);
}

function depTypeToFlag(depType) {
  let flag = Object.keys(DEPENDENCY_TYPE_FLAGS_MAP).find(
    key => DEPENDENCY_TYPE_FLAGS_MAP[key] === depType
  );

  return flag ? `--${flag}` : flag;
}

export async function install(cwd: string, pureLockfile?: boolean) {
  let localYarn = path.join(await getLocalBinPath(), 'yarn');
  let installFlags = [];

  if (pureLockfile) installFlags.push('--pure-lockfile');

  let yarnUserAgent = await userAgent();
  let boltUserAgent = `bolt/${BOLT_VERSION} ${yarnUserAgent}`;

  await processes.spawn(localYarn, ['install', ...installFlags], {
    cwd,
    tty: true,
    env: {
      ...process.env,
      npm_config_user_agent: boltUserAgent,
      bolt_config_user_agent: boltUserAgent
    },
    useBasename: true
  });
}

export async function add(
  pkg: Package,
  dependencies: Array<Dependency>,
  type?: configDependencyType
) {
  let localYarn = path.join(await getLocalBinPath(), 'yarn');
  let spawnArgs = ['add'];
  if (!dependencies.length) return;

  dependencies.forEach(dep => {
    if (dep.version) {
      spawnArgs.push(`${dep.name}@${dep.version}`);
    } else {
      spawnArgs.push(dep.name);
    }
  });

  if (type) {
    let flag = depTypeToFlag(type);
    if (flag) spawnArgs.push(flag);
  }

  await processes.spawn(localYarn, spawnArgs, {
    cwd: pkg.dir,
    pkg: pkg,
    tty: true
  });
}

export async function upgrade(
  pkg: Package,
  dependencies: Array<Dependency> = [],
  flags: Array<string> = []
) {
  let localYarn = path.join(await getLocalBinPath(), 'yarn');
  let spawnArgs = ['upgrade'];

  if (dependencies.length) {
    dependencies.forEach(dep => {
      if (dep.version) {
        spawnArgs.push(`"${dep.name}@${dep.version}"`);
      } else {
        spawnArgs.push(dep.name);
      }
    });
  }

  await processes.spawn(localYarn, [...spawnArgs, ...flags], {
    cwd: pkg.dir,
    pkg: pkg,
    tty: true
  });
}

export async function run(
  pkg: Package,
  script: string,
  args: Array<string> = []
) {
  let project = await Project.init(pkg.dir);
  let localYarn = path.join(await getLocalBinPath(), 'yarn');
  // We use a relative path because the absolute paths are very long and noisy in logs
  let localYarnRelative = path.relative(pkg.dir, localYarn);
  let spawnArgs = ['run', '-s', script];

  if (args.length) {
    spawnArgs = spawnArgs.concat('--', args);
  }
  await processes.spawn(localYarnRelative, spawnArgs, {
    cwd: pkg.dir,
    pkg: pkg,
    tty: true,
    useBasename: true
  });<commit_msg>Get rid of yarn run -- warning (#225)<commit_after>// @flow
import includes from 'array-includes';
import projectBinPath from 'project-bin-path';
import * as path from 'path';
import type { Dependency, configDependencyType } from '../types';
import type Package from '../Package';
import Project from '../Project';
import * as processes from './processes';
import * as fs from '../utils/fs';
import * as logger from '../utils/fs';
import { DEPENDENCY_TYPE_FLAGS_MAP, BOLT_VERSION } from '../constants';

function getLocalBinPath(): Promise<string> {
  return projectBinPath(__dirname);
}

function depTypeToFlag(depType) {
  let flag = Object.keys(DEPENDENCY_TYPE_FLAGS_MAP).find(
    key => DEPENDENCY_TYPE_FLAGS_MAP[key] === depType
  );

  return flag ? `--${flag}` : flag;
}

export async function install(cwd: string, pureLockfile?: boolean) {
  let localYarn = path.join(await getLocalBinPath(), 'yarn');
  let installFlags = [];

  if (pureLockfile) installFlags.push('--pure-lockfile');

  let yarnUserAgent = await userAgent();
  let boltUserAgent = `bolt/${BOLT_VERSION} ${yarnUserAgent}`;

  await processes.spawn(localYarn, ['install', ...installFlags], {
    cwd,
    tty: true,
    env: {
      ...process.env,
      npm_config_user_agent: boltUserAgent,
      bolt_config_user_agent: boltUserAgent
    },
    useBasename: true
  });
}

export async function add(
  pkg: Package,
  dependencies: Array<Dependency>,
  type?: configDependencyType
) {
  let localYarn = path.join(await getLocalBinPath(), 'yarn');
  let spawnArgs = ['add'];
  if (!dependencies.length) return;

  dependencies.forEach(dep => {
    if (dep.version) {
      spawnArgs.push(`${dep.name}@${dep.version}`);
    } else {
      spawnArgs.push(dep.name);
    }
  });

  if (type) {
    let flag = depTypeToFlag(type);
    if (flag) spawnArgs.push(flag);
  }

  await processes.spawn(localYarn, spawnArgs, {
    cwd: pkg.dir,
    pkg: pkg,
    tty: true
  });
}

export async function upgrade(
  pkg: Package,
  dependencies: Array<Dependency> = [],
  flags: Array<string> = []
) {
  let localYarn = path.join(await getLocalBinPath(), 'yarn');
  let spawnArgs = ['upgrade'];

  if (dependencies.length) {
    dependencies.forEach(dep => {
      if (dep.version) {
        spawnArgs.push(`"${dep.name}@${dep.version}"`);
      } else {
        spawnArgs.push(dep.name);
      }
    });
  }

  await processes.spawn(localYarn, [...spawnArgs, ...flags], {
    cwd: pkg.dir,
    pkg: pkg,
    tty: true
  });
}

export async function run(
  pkg: Package,
  script: string,
  args: Array<string> = []
) {
  let project = await Project.init(pkg.dir);
  let localYarn = path.join(await getLocalBinPath(), 'yarn');
  // We use a relative path because the absolute paths are very long and noisy in logs
  let localYarnRelative = path.relative(pkg.dir, localYarn);
  let spawnArgs = ['run', '-s', script];

  if (args.length) {
    spawnArgs = spawnArgs.concat(args);
  }
  await processes.spawn(localYarnRelative, spawnArgs, {
    cwd: pkg.dir,
    pkg: pkg,
    tty: true,
    useBasename: true
  });
{'fraction_non_alphanumeric': 0.15189048239895697, 'fraction_numerical': 0.0004889178617992178, 'mean_word_length': 3.5025678650036682, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6155031', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2036, 'n_words': 499}