Skip to content

Commit 97a19b9

Browse files
authored
fix: init uses yarn assuming version > 1.x (#2329)
1 parent 45e686e commit 97a19b9

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

__e2e__/init.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import fs from 'fs';
22
import path from 'path';
33
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
4+
import {execSync} from 'child_process';
5+
import semver from 'semver';
46
import slash from 'slash';
57

8+
const yarnVersion = semver.parse(execSync('yarn --version').toString().trim())!;
9+
10+
// .yarnrc -> .yarnrc.yml >= 2.0.0: yarnpkg/berry/issues/239
11+
const yarnConfigFile = yarnVersion.major > 1 ? '.yarnrc.yml' : '.yarnrc';
12+
613
const DIR = getTempDirectory('command-init');
714
const PROJECT_NAME = 'TestInit';
815

@@ -23,7 +30,7 @@ function createCustomTemplateFiles() {
2330
const customTemplateCopiedFiles = [
2431
'.git',
2532
'.yarn',
26-
'.yarnrc.yml',
33+
yarnConfigFile,
2734
'dir',
2835
'file',
2936
'node_modules',
@@ -178,7 +185,7 @@ test('init uses npm as the package manager with --npm', () => {
178185
// Remove yarn specific files and node_modules
179186
const filteredFiles = customTemplateCopiedFiles.filter(
180187
(file) =>
181-
!['yarn.lock', 'node_modules', '.yarnrc.yml', '.yarn'].includes(file),
188+
!['yarn.lock', 'node_modules', yarnConfigFile, '.yarn'].includes(file),
182189
);
183190

184191
// Add package-lock.json

jest/helpers.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const makeTemplate =
4545
});
4646

4747
export const cleanup = (directory: string) => {
48-
fs.rmSync(directory, {recursive: true, force: true});
48+
fs.rmSync(directory, {recursive: true, force: true, maxRetries: 10});
4949
};
5050

5151
/**
@@ -142,18 +142,18 @@ function getExecaOptions(options: SpawnOptions) {
142142
function handleTestFailure(
143143
cmd: string,
144144
options: SpawnOptions,
145-
result: {[key: string]: any},
145+
result: execa.ExecaReturnBase<string>,
146146
args: string[] | undefined,
147147
) {
148-
if (!options.expectedFailure && result.code !== 0) {
148+
if (!options.expectedFailure && result.exitCode !== 0) {
149149
console.log(`Running ${cmd} command failed for unexpected reason. Here's more info:
150150
${chalk.bold('cmd:')} ${cmd}
151151
${chalk.bold('options:')} ${JSON.stringify(options)}
152152
${chalk.bold('args:')} ${(args || []).join(' ')}
153153
${chalk.bold('stderr:')} ${result.stderr}
154154
${chalk.bold('stdout:')} ${result.stdout}
155-
${chalk.bold('code:')} ${result.code}`);
156-
} else if (options.expectedFailure && result.code === 0) {
155+
${chalk.bold('exitCode:')}${result.exitCode}`);
156+
} else if (options.expectedFailure && result.exitCode === 0) {
157157
throw new Error("Expected command to fail, but it didn't");
158158
}
159159
}

packages/cli-platform-android/src/config/findManifest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import path from 'path';
1111
import {unixifyPaths} from '@react-native-community/cli-tools';
1212

1313
export default function findManifest(folder: string) {
14-
let manifestPaths = glob.sync(path.join('**', 'AndroidManifest.xml'), {
14+
let manifestPaths = glob.sync('**/AndroidManifest.xml', {
1515
cwd: unixifyPaths(folder),
1616
ignore: [
1717
'node_modules/**',

packages/cli/src/commands/init/init.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,24 @@ interface TemplateReturnType {
7878

7979
// Here we are defining explicit version of Yarn to be used in the new project because in some cases providing `3.x` don't work.
8080
const YARN_VERSION = '3.6.4';
81+
const YARN_LEGACY_VERSION = '1.22.22';
8182

8283
const bumpYarnVersion = async (silent: boolean, root: string) => {
8384
try {
8485
let yarnVersion = semver.parse(getYarnVersionIfAvailable());
8586

8687
if (yarnVersion) {
87-
await executeCommand('yarn', ['set', 'version', YARN_VERSION], {
88+
// `yarn set` is unsupported until 1.22, however it's a alias (yarnpkg/yarn/pull/7862) calling `policies set-version`.
89+
const setVersionArgs =
90+
yarnVersion.major > 1
91+
? ['set', 'version', YARN_VERSION]
92+
: ['policies', 'set-version', YARN_LEGACY_VERSION];
93+
await executeCommand('yarn', setVersionArgs, {
8894
root,
8995
silent,
9096
});
9197

9298
// React Native doesn't support PnP, so we need to set nodeLinker to node-modules. Read more here: https://github.com/react-native-community/cli/issues/27#issuecomment-1772626767
93-
9499
await executeCommand(
95100
'yarn',
96101
['config', 'set', 'nodeLinker', 'node-modules'],

0 commit comments

Comments
 (0)