Skip to content
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

feature(create-expo): configure pnpm and yarn berry when creating a new project #27699

Merged
merged 4 commits into from Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/create-expo/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
### 🎉 New features

- Add support for GitHub URLs in `--template` option. ([#26554](https://github.com/expo/expo/pull/26554) by [@byCedric](https://github.com/byCedric))
- Add auto-configuration for pnpm and yarn berry. ([#27699](https://github.com/expo/expo/pull/27699) by [@byCedric](https://github.com/byCedric))

### 🐛 Bug fixes

Expand Down
2 changes: 2 additions & 0 deletions packages/create-expo/src/createAsync.ts
Expand Up @@ -12,6 +12,7 @@ import * as Template from './Template';
import { promptTemplateAsync } from './legacyTemplates';
import { Log } from './log';
import {
configurePackageManager,
installDependenciesAsync,
PackageManagerName,
resolvePackageManager,
Expand Down Expand Up @@ -190,6 +191,7 @@ async function installNodeDependenciesAsync(
packageManager: PackageManagerName
): Promise<void> {
try {
await configurePackageManager(projectRoot, packageManager, { silent: false });
await installDependenciesAsync(projectRoot, packageManager, { silent: false });
} catch (error: any) {
debug(`Error installing node modules: %O`, error);
Expand Down
53 changes: 44 additions & 9 deletions packages/create-expo/src/resolvePackageManager.ts
Expand Up @@ -70,19 +70,54 @@ export function formatSelfCommand() {
}
}

function createPackageManager(
packageManager: PackageManagerName,
options?: PackageManager.PackageManagerOptions
) {
switch (packageManager) {
case 'yarn':
return new PackageManager.YarnPackageManager(options);
case 'pnpm':
return new PackageManager.PnpmPackageManager(options);
case 'bun':
return new PackageManager.BunPackageManager(options);
case 'npm':
default:
return new PackageManager.NpmPackageManager(options);
}
}

export async function installDependenciesAsync(
projectRoot: string,
packageManager: PackageManagerName,
flags: { silent: boolean } = { silent: false }
) {
const options = { cwd: projectRoot, silent: flags.silent };
if (packageManager === 'yarn') {
await new PackageManager.YarnPackageManager(options).installAsync();
} else if (packageManager === 'pnpm') {
await new PackageManager.PnpmPackageManager(options).installAsync();
} else if (packageManager === 'bun') {
await new PackageManager.BunPackageManager(options).installAsync();
} else {
await new PackageManager.NpmPackageManager(options).installAsync();
await createPackageManager(packageManager, {
cwd: projectRoot,
silent: flags.silent,
}).installAsync();
}

export async function configurePackageManager(
projectRoot: string,
packageManager: PackageManagerName,
flags: { silent: boolean } = { silent: false }
) {
const manager = createPackageManager(packageManager, { cwd: projectRoot, ...flags });
switch (manager.name) {
case 'pnpm':
await manager.runAsync(['config', '--location', 'project', 'set', 'node-linker', 'hoisted']);
break;

case 'yarn': {
const yarnVersion = await manager.versionAsync();
const majorVersion = parseInt(yarnVersion.split('.')[0], 10);

if (majorVersion >= 2) {
await manager.runAsync(['config', 'set', 'nodeLinker', 'node-modules']);
}

break;
}
}
}