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 Oct 26, 2023
1 parent dea0642 commit 4457c77
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
30 changes: 30 additions & 0 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {getBunVersionIfAvailable} from '../../tools/bun';
import {getNpmVersionIfAvailable} from '../../tools/npm';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import {createHash} from 'crypto';
import semver from 'semver';
import {executeCommand} from '../../tools/executeCommand';

const DEFAULT_VERSION = 'latest';

Expand Down Expand Up @@ -56,6 +58,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 @@ -168,6 +194,10 @@ async function createFromTemplate({
packageName,
});

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

loader.succeed();
const {postInitScript} = templateConfig;
if (postInitScript) {
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,
});
}
14 changes: 1 addition & 13 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 @@ -51,17 +50,6 @@ function configurePackageManager(
return executeCommand(pm, args, options);
}

function executeCommand(
command: string,
args: Array<string>,
options: Options,
) {
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 4457c77

Please sign in to comment.