Skip to content

Commit

Permalink
fix: init uses yarn assuming version > 1.x
Browse files Browse the repository at this point in the history
- Checks that we're using yarn > 1.x when calling `yarn set` which isn't
supported [1] but is for later versions [2].
- Update tests to support
environment change differences between yarn 1.x & 3.x.
- Fix glob.sync to only use forward slashes.

[1] https://classic.yarnpkg.com/en/docs/cli/set
[2] https://yarnpkg.com/cli/set/version
[3] https://github.com/mrmlnc/fast-glob#how-to-write-patterns-on-windows
  • Loading branch information
blakef committed Mar 19, 2024
1 parent 332b84d commit 2e72bec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
15 changes: 11 additions & 4 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import fs from 'fs';
import path from 'path';
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {execSync} from 'child_process';
import semver from 'semver';
import slash from 'slash';

const DIR = getTempDirectory('command-init');
const PROJECT_NAME = 'TestInit';

const yarnVersion = semver.parse(execSync('yarn --version').toString().trim())!;

const yarnConfigFiles: string[] = [];
if (yarnVersion.major >= 3) {
yarnConfigFiles.push('.yarnrc.yml', '.yarn');
}

function createCustomTemplateFiles() {
writeFiles(DIR, {
'custom/template/template.config.js': `module.exports = {
Expand All @@ -22,8 +31,7 @@ function createCustomTemplateFiles() {

const customTemplateCopiedFiles = [
'.git',
'.yarn',
'.yarnrc.yml',
...yarnConfigFiles,
'dir',
'file',
'node_modules',
Expand Down Expand Up @@ -177,8 +185,7 @@ test('init uses npm as the package manager with --npm', () => {

// Remove yarn specific files and node_modules
const filteredFiles = customTemplateCopiedFiles.filter(
(file) =>
!['yarn.lock', 'node_modules', '.yarnrc.yml', '.yarn'].includes(file),
(file) => !['yarn.lock', 'node_modules', ...yarnConfigFiles].includes(file),
);

// Add package-lock.json
Expand Down
10 changes: 5 additions & 5 deletions jest/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const makeTemplate =
});

export const cleanup = (directory: string) => {
fs.rmSync(directory, {recursive: true, force: true});
fs.rmSync(directory, {recursive: true, force: true, maxRetries: 10});
};

/**
Expand Down Expand Up @@ -142,18 +142,18 @@ function getExecaOptions(options: SpawnOptions) {
function handleTestFailure(
cmd: string,
options: SpawnOptions,
result: {[key: string]: any},
result: execa.ExecaReturnBase<string>,
args: string[] | undefined,
) {
if (!options.expectedFailure && result.code !== 0) {
if (!options.expectedFailure && result.exitCode !== 0) {
console.log(`Running ${cmd} command failed for unexpected reason. Here's more info:
${chalk.bold('cmd:')} ${cmd}
${chalk.bold('options:')} ${JSON.stringify(options)}
${chalk.bold('args:')} ${(args || []).join(' ')}
${chalk.bold('stderr:')} ${result.stderr}
${chalk.bold('stdout:')} ${result.stdout}
${chalk.bold('code:')} ${result.code}`);
} else if (options.expectedFailure && result.code === 0) {
${chalk.bold('exitCode:')}${result.exitCode}`);
} else if (options.expectedFailure && result.exitCode === 0) {
throw new Error("Expected command to fail, but it didn't");
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-platform-android/src/config/findManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import path from 'path';
import {unixifyPaths} from '@react-native-community/cli-tools';

export default function findManifest(folder: string) {
let manifestPaths = glob.sync(path.join('**', 'AndroidManifest.xml'), {
let manifestPaths = glob.sync('**/AndroidManifest.xml', {
cwd: unixifyPaths(folder),
ignore: [
'node_modules/**',
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ const bumpYarnVersion = async (silent: boolean, root: string) => {
try {
let yarnVersion = semver.parse(getYarnVersionIfAvailable());

if (yarnVersion) {
// `yarn set` is unsupported in 1.x
if (yarnVersion && yarnVersion.major > 1) {
await executeCommand('yarn', ['set', 'version', YARN_VERSION], {
root,
silent,
Expand Down

0 comments on commit 2e72bec

Please sign in to comment.