/* @flow */ import http from 'http'; import net from 'net'; import path from 'path'; import commander from 'commander'; import fs from 'fs'; import invariant from 'invariant'; import lockfile from 'proper-lockfile'; import loudRejection from 'loud-rejection'; import onDeath from 'death'; import semver from 'semver'; import {ConsoleReporter, JSONReporter} from '../reporters/index.js'; import {registries, registryNames} from '../registries/index.js'; import commands from './commands/index.js'; import * as constants from '../constants.js'; import * as network from '../util/network.js'; import {MessageError} from '../errors.js'; import Config from '../config.js'; import {getRcConfigForCwd, getRcArgs} from '../rc.js'; import {spawnp, forkp} from '../util/child.js'; import {version} from '../util/yarn-version.js'; import handleSignals from '../util/signal-handler.js'; import {boolify, boolifyWithDefault} from '../util/conversion.js'; import {ProcessTermError} from '../errors'; const chalk = require('chalk'); process.stdout.prependListener('error', err => { // swallow err only if downstream consumer process closed pipe early if (err.code === 'EPIPE' || err.code === 'ERR_STREAM_DESTROYED') { return; } throw err; }); function findPackageManager(base: string): string | null { let prev = null; let dir = base; do { const p = path.join(dir, constants.NODE_PACKAGE_JSON); let data; try { data = JSON.parse(fs.readFileSync(p, `utf8`)); } catch (err) {} if (data && typeof data.packageManager === `string`) { return data.packageManager; } prev = dir; dir = path.dirname(dir); } while (dir !== prev); return null; } function findProjectRoot(base: string): string { let prev = null; let dir = base; do { if (fs.existsSync(path.join(dir, constants.NODE_PACKAGE_JSON))) { return dir; } prev = dir; dir = path.dirname(dir); } while (dir !== prev); return base; } export async function main({ startArgs, args, endArgs, }: { startArgs: Array, args: Array, endArgs: Array, }): Promise { const collect = (val, acc) => { acc.push(val); return acc; }; loudRejection(); handleSignals(); // set global options commander.version(version, '-v, --version'); commander.usage('[command] [flags]'); commander.option('--no-default-rc', 'prevent Yarn from automatically detecting yarnrc and npmrc files'); commander.option( '--use-yarnrc ', 'specifies a yarnrc file that Yarn should use (.yarnrc only, not .npmrc)', collect, [], ); commander.option('--verbose', 'output verbose messages on internal operations'); commander.option('--offline', 'trigger an error if any required dependencies are not available in local cache'); commander.option('--prefer-offline', 'use network only if dependencies are not available in local cache'); commander.option('--enable-pnp, --pnp', "enable the Plug'n'Play installation"); commander.option('--disable-pnp', "disable the Plug'n'Play installation"); commander.option('--strict-semver'); commander.option('--json', 'format Yarn log messages as lines of JSON (see jsonlines.org)'); commander.option('--ignore-scripts', "don't run lifecycle scripts"); commander.option('--har', 'save HAR output of network traffic'); commander.option('--ignore-platform', 'ignore platform checks'); commander.option('--ignore-engines', 'ignore engines check'); commander.option('--ignore-optional', 'ignore optional dependencies'); commander.option('--force', 'install and build packages even if they were built before, overwrite lockfile'); commander.option('--skip-integrity-check', 'run install without checking if node_modules is installed'); commander.option('--check-files', 'install will verify file tree of packages for consistency'); commander.option('--no-bin-links', "don't generate bin links when setting up packages"); commander.option('--flat', 'only allow one version of a package'); commander.option('--prod, --production [prod]', '', boolify); commander.option('--no-lockfile', "don't read or generate a lockfile"); commander.option('--pure-lockfile', "don't generate a lockfile"); commander.option('--frozen-lockfile', "don't generate a lockfile and fail if an update is needed"); commander.option('--update-checksums', 'update package checksums from current repository'); commander.option('--link-duplicates', 'create hardlinks to the repeated modules in node_modules'); commander.option('--link-folder ', 'specify a custom folder to store global links'); commander.option('--global-folder ', 'specify a custom folder to store global packages'); commander.option( '--modules-folder ', 'rather than installing modules into the node_modules folder relative to the cwd, output them here', ); commander.option('--preferred-cache-folder ', 'specify a custom folder to store the yarn cache if possible'); commander.option('--cache-folder ', 'specify a custom folder that must be used to store the yarn cache'); commander.option('--mutex [:specifier]', 'use a mutex to ensure only one yarn instance is executing'); commander.option( '--emoji [bool]', 'enable emoji in output', boolify, process.platform === 'darwin' || process.env.TERM_PROGRAM === 'Hyper' || process.env.TERM_PROGRAM === 'HyperTerm' || process.env.TERM_PROGRAM === 'Terminus', ); commander.option('-s, --silent', 'skip Yarn console logs, other types of logs (script output) will be printed'); commander.option('--cwd ', 'working directory to use', process.cwd()); commander.option('--proxy ', ''); commander.option('--https-proxy ', ''); commander.option('--registry ', 'override configuration registry'); commander.option('--no-progress', 'disable progress bar'); commander.option('--network-concurrency ', 'maximum number of concurrent network requests', parseInt); commander.option('--network-timeout ', 'TCP timeout for network requests', parseInt); commander.option('--non-interactive', 'do not show interactive prompts'); commander.option( '--scripts-prepend-node-path [bool]', 'prepend the node executable dir to the PATH in scripts', boolify, ); commander.option('--no-node-version-check', 'do not warn when using a potentially unsupported Node version'); commander.option('--focus', 'Focus on a single workspace by installing remote copies of its sibling workspaces.'); commander.option('--otp ', 'one-time password for two factor authentication'); // if -v is the first command, then always exit after returning the version if (args[0] === '-v') { console.log(version.trim()); process.exitCode = 0; return; } // get command name const firstNonFlagIndex = args.findIndex((arg, idx, arr) => { const isOption = arg.startsWith('-'); const prev = idx > 0 && arr[idx - 1]; const prevOption = prev && prev.startsWith('-') && commander.optionFor(prev); const boundToPrevOption = prevOption && (prevOption.optional || prevOption.required); return !isOption && !boundToPrevOption; }); let preCommandArgs; let commandName = ''; if (firstNonFlagIndex > -1) { preCommandArgs = args.slice(0, firstNonFlagIndex); commandName = args[firstNonFlagIndex]; args = args.slice(firstNonFlagIndex + 1); } else { preCommandArgs = args; args = []; } let isKnownCommand = Object.prototype.hasOwnProperty.call(commands, commandName); const isHelp = arg => arg === '--help' || arg === '-h'; const helpInPre = preCommandArgs.findIndex(isHelp); const helpInArgs = args.findIndex(isHelp); const setHelpMode = () => { if (isKnownCommand) { args.unshift(commandName); } commandName = 'help'; isKnownCommand = true; }; if (helpInPre > -1) { preCommandArgs.splice(helpInPre); setHelpMode(); } else if (isKnownCommand && helpInArgs === 0) { args.splice(helpInArgs); setHelpMode(); } if (!commandName) { commandName = 'install'; isKnownCommand = true; } if (commandName === ('set': string) && args[0] === 'version') { commandName = ('policies': string); args.splice(0, 1, 'set-version'); isKnownCommand = true; } if (!isKnownCommand) { // if command is not recognized, then set default to `run` args.unshift(commandName); commandName = 'run'; } const command = commands[commandName]; let warnAboutRunDashDash = false; // we are using "yarn