Skip to content

Commit

Permalink
Fix autoinstall with Yarn 2+ (#7023)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Oct 6, 2021
1 parent fe41fc1 commit 4904f20
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/core/package-manager/src/Yarn.js
Expand Up @@ -4,6 +4,8 @@ import type {PackageInstaller, InstallerOptions} from './types';

import commandExists from 'command-exists';
import spawn from 'cross-spawn';
import {exec as _exec} from 'child_process';
import {promisify} from 'util';
import logger from '@parcel/logger';
import split from 'split2';
import JSONParseStream from './JSONParseStream';
Expand All @@ -15,6 +17,7 @@ import {npmSpecifierFromModuleRequest} from './utils';
import pkg from '../package.json';

const YARN_CMD = 'yarn';
const exec = promisify(_exec);

type YarnStdOutMessage =
| {|
Expand All @@ -35,6 +38,8 @@ type YarnStdErrMessage = {|
|};

let hasYarn: ?boolean;
let yarnVersion: ?number;

export class Yarn implements PackageInstaller {
static async exists(): Promise<boolean> {
if (hasYarn != null) {
Expand All @@ -55,12 +60,20 @@ export class Yarn implements PackageInstaller {
cwd,
saveDev = true,
}: InstallerOptions): Promise<void> {
if (yarnVersion == null) {
let version = await exec('yarn --version');
yarnVersion = parseInt(version.stdout, 10);
}

let args = ['add', '--json'].concat(
modules.map(npmSpecifierFromModuleRequest),
);

if (saveDev) {
args.push('-D', '-W');
args.push('-D');
if (yarnVersion < 2) {
args.push('-W');
}
}

// When Parcel is run by Yarn (e.g. via package.json scripts), several environment variables are
Expand Down

0 comments on commit 4904f20

Please sign in to comment.