Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 948
feat: use Yarn v3 with nodeLinker: node-modules for new projects#2134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d52857d2554f20d4fdf35785af20b7d6c805624722fac59f2File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -28,7 +28,13 @@ import {getBunVersionIfAvailable} from '../../tools/bun'; | ||
| import {getNpmVersionIfAvailable} from '../../tools/npm'; | ||
| import {getYarnVersionIfAvailable} from '../../tools/yarn'; | ||
| import {createHash} from 'crypto'; | ||
| import createGitRepository from './createGitRepository'; | ||
| import { | ||
| createGitRepository, | ||
| checkGitInstallation, | ||
| checkIfFolderIsGitRepo, | ||
| } from './git'; | ||
| import semver from 'semver'; | ||
| import {executeCommand} from '../../tools/executeCommand'; | ||
| const DEFAULT_VERSION = 'latest'; | ||
| @@ -49,6 +55,7 @@ type Options = { | ||
| interface TemplateOptions { | ||
| projectName: string; | ||
| shouldBumpYarnVersion: boolean; | ||
| templateUri: string; | ||
| npm?: boolean; | ||
| pm?: PackageManager.PackageManager; | ||
| @@ -64,17 +71,38 @@ interface TemplateReturnType { | ||
| didInstallPods?: boolean; | ||
| } | ||
| // 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. | ||
| const YARN_VERSION = '3.6.4'; | ||
| const bumpYarnVersion = async (silent: boolean, root: string) => { | ||
| try { | ||
| let yarnVersion = semver.parse(getYarnVersionIfAvailable()); | ||
| if (yarnVersion) { | ||
| await executeCommand('yarn', ['set', 'version', YARN_VERSION], { | ||
| root, | ||
| silent, | ||
| }); | ||
Comment on lines
+82
to
+85
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to check my understanding here - this will install the requested version via corepack if it isn't currently on the machine? What if corepack isn't enabled? | ||
| // 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'], | ||
szymonrybczak marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| {root, silent}, | ||
| ); | ||
| } | ||
| } catch (e) { | ||
| logger.debug(e as string); | ||
| } | ||
| }; | ||
| function doesDirectoryExist(dir: string) { | ||
| return fs.existsSync(dir); | ||
| } | ||
| async function setProjectDirectory(directory: string) { | ||
| if (doesDirectoryExist(directory)) { | ||
| throw new DirectoryAlreadyExistsError(directory); | ||
| } | ||
| try { | ||
| fs.mkdirSync(directory, {recursive: true}); | ||
| process.chdir(directory); | ||
| } catch (error) { | ||
| throw new CLIError( | ||
| @@ -108,6 +136,7 @@ function setEmptyHashForCachedDependencies(projectName: string) { | ||
| async function createFromTemplate({ | ||
| projectName, | ||
| shouldBumpYarnVersion, | ||
| templateUri, | ||
| npm, | ||
| pm, | ||
| @@ -182,6 +211,11 @@ async function createFromTemplate({ | ||
| packageName, | ||
| }); | ||
| if (packageManager === 'yarn' && shouldBumpYarnVersion) { | ||
| await bumpYarnVersion(false, projectDirectory); | ||
| } | ||
| loader.succeed(); | ||
| const {postInitScript} = templateConfig; | ||
| if (postInitScript) { | ||
| loader.info('Executing post init script '); | ||
| @@ -310,12 +344,14 @@ async function createProject( | ||
| projectName: string, | ||
| directory: string, | ||
| version: string, | ||
| shouldBumpYarnVersion: boolean, | ||
| options: Options, | ||
| ): Promise<TemplateReturnType> { | ||
| const templateUri = createTemplateUri(options, version); | ||
| return createFromTemplate({ | ||
| projectName, | ||
| shouldBumpYarnVersion, | ||
| templateUri, | ||
| npm: options.npm, | ||
| pm: options.pm, | ||
| @@ -361,6 +397,7 @@ export default (async function initialize( | ||
| const version = options.version || DEFAULT_VERSION; | ||
| const directoryName = path.relative(root, options.directory || projectName); | ||
| const projectFolder = path.join(root, directoryName); | ||
| if (options.pm && !checkPackageManagerAvailability(options.pm)) { | ||
| logger.error( | ||
| @@ -369,16 +406,40 @@ export default (async function initialize( | ||
| return; | ||
| } | ||
| if (doesDirectoryExist(projectFolder)) { | ||
| throw new DirectoryAlreadyExistsError(directoryName); | ||
| } else { | ||
| fs.mkdirSync(projectFolder, {recursive: true}); | ||
| } | ||
| let shouldBumpYarnVersion = true; | ||
| let shouldCreateGitRepository = false; | ||
| const isGitAvailable = await checkGitInstallation(); | ||
| if (isGitAvailable) { | ||
| const isFolderGitRepo = await checkIfFolderIsGitRepo(projectFolder); | ||
| if (isFolderGitRepo) { | ||
| shouldBumpYarnVersion = false; | ||
| } else { | ||
| shouldCreateGitRepository = true; // Initialize git repo after creating project | ||
| } | ||
| } else { | ||
| logger.warn( | ||
| 'Git is not installed on your system. This might cause some features to work incorrectly.', | ||
| ); | ||
| } | ||
| const {didInstallPods} = await createProject( | ||
| projectName, | ||
| directoryName, | ||
| version, | ||
| shouldBumpYarnVersion, | ||
| options, | ||
| ); | ||
| const projectFolder = path.join(root, directoryName); | ||
| if (!options.skipGitInit) { | ||
| if (shouldCreateGitRepository && !options.skipGitInit) { | ||
| await createGitRepository(projectFolder); | ||
| } | ||
| 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, | ||
| }); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.