Skip to content

Commit

Permalink
feat: use Yarn v3 when creating new project
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Nov 2, 2023
1 parent 3f206fd commit 33ff1d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
2 changes: 2 additions & 0 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function createCustomTemplateFiles() {

const customTemplateCopiedFiles = [
'.git',
'.yarn',
'.yarnrc.yml',
'dir',
'file',
'node_modules',
Expand Down
31 changes: 31 additions & 0 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {getYarnVersionIfAvailable} from '../../tools/yarn';
import {createHash} from 'crypto';
import createGitRepository from './createGitRepository';
import deepmerge from 'deepmerge';
import semver from 'semver';
import {executeCommand} from '../../tools/executeCommand';

const DEFAULT_VERSION = 'latest';

Expand Down Expand Up @@ -58,6 +60,30 @@ interface TemplateOptions {
installCocoaPods?: string | boolean;
}

const YARN_VERSION = '3.6.4';

const bumpYarnVersion = async (silent: boolean, root: string) => {
try {
let yarnVersion = semver.parse(getYarnVersionIfAvailable());
if (yarnVersion && semver.major(yarnVersion) === 1) {
await executeCommand('yarn', ['set', 'version', YARN_VERSION], {
root,
silent,
});

// 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

await executeCommand(
'yarn',
['config', 'set', 'nodeLinker', 'node-modules'],
{root, silent},
);
}
} catch (e) {
logger.debug(e as string);
}
};

function doesDirectoryExist(dir: string) {
return fs.existsSync(dir);
}
Expand Down Expand Up @@ -172,6 +198,11 @@ async function createFromTemplate({

createDefaultConfigFile(projectDirectory, loader);

if (packageManager === 'yarn') {
await bumpYarnVersion(false, projectDirectory);
}

loader.succeed();
const {postInitScript} = templateConfig;
if (postInitScript) {
loader.info('Executing post init script ');
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/init/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import replacePathSepForRegex from '../../tools/replacePathSepForRegex';
import fs from 'fs';
import chalk from 'chalk';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import {executeCommand} from '../../tools/packageManager';
import {executeCommand} from '../../tools/executeCommand';

export type TemplateConfig = {
placeholderName: string;
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/src/tools/executeCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {logger} from '@react-native-community/cli-tools';
import execa from 'execa';

export function executeCommand(
command: string,
args: Array<string>,
options: {
root: string;
silent?: boolean;
},
) {
return execa(command, args, {
stdio: options.silent && !logger.isVerbose() ? 'pipe' : 'inherit',
cwd: options.root,
});
}
17 changes: 1 addition & 16 deletions packages/cli/src/tools/packageManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import execa from 'execa';
import {logger} from '@react-native-community/cli-tools';
import {getYarnVersionIfAvailable, isProjectUsingYarn} from './yarn';
import {getBunVersionIfAvailable, isProjectUsingBun} from './bun';
import {getNpmVersionIfAvailable, isProjectUsingNpm} from './npm';
import {executeCommand} from './executeCommand';

export type PackageManager = keyof typeof packageManagers;

Expand Down Expand Up @@ -65,20 +64,6 @@ function configurePackageManager(
return executeCommand(pm, args, options);
}

export function executeCommand(
command: string,
args: Array<string>,
options: {
root: string;
silent?: boolean;
},
) {
return execa(command, args, {
stdio: options.silent && !logger.isVerbose() ? 'pipe' : 'inherit',
cwd: options.root,
});
}

export function shouldUseYarn(options: Options) {
if (options.packageManager === 'yarn') {
return getYarnVersionIfAvailable();
Expand Down

0 comments on commit 33ff1d7

Please sign in to comment.