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 9afaa54
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
16 changes: 16 additions & 0 deletions packages/cli-tools/src/executeCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import execa from 'execa';
import {logger} from '@react-native-community/cli-tools';

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

export default executeCommand;
1 change: 1 addition & 0 deletions packages/cli-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export {default as handlePortUnavailable} from './handlePortUnavailable';
export * from './port';
export {default as cacheManager} from './cacheManager';
export {default as runSudo} from './runSudo';
export {default as executeCommand} from './executeCommand';

export * from './errors';
33 changes: 33 additions & 0 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getLoader,
Loader,
cacheManager,
executeCommand,
} from '@react-native-community/cli-tools';
import {installPods} from '@react-native-community/cli-platform-ios';
import {
Expand All @@ -28,6 +29,7 @@ import {getBunVersionIfAvailable} from '../../tools/bun';
import {getNpmVersionIfAvailable} from '../../tools/npm';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import {createHash} from 'crypto';
import semver from 'semver';

const DEFAULT_VERSION = 'latest';

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

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

loader.succeed();
const {postInitScript} = templateConfig;
if (postInitScript) {
Expand Down
16 changes: 2 additions & 14 deletions packages/cli/src/tools/packageManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import execa from 'execa';
import {logger} from '@react-native-community/cli-tools';
import {executeCommand} from '@react-native-community/cli-tools';
import {getYarnVersionIfAvailable, isProjectUsingYarn} from './yarn';
import {getBunVersionIfAvailable, isProjectUsingBun} from './bun';
import {getNpmVersionIfAvailable, isProjectUsingNpm} from './npm';
Expand Down Expand Up @@ -48,18 +47,7 @@ function configurePackageManager(

const [executable, ...flags] = packageManagers[pm][action];
const args = [executable, ...flags, ...packageNames];
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,
});
return executeCommand(pm, args, options.root, options.silent);
}

export function shouldUseYarn(options: Options) {
Expand Down

0 comments on commit 9afaa54

Please sign in to comment.