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

feat: add Bun to init command #2073

Merged
merged 8 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ module.exports = {
Skip dependencies installation

#### `--npm`
> [!WARNING]
> `--npm` is deprecated and will be removed in the future. Please use `--pm npm` instead.

Force use of npm during initialization

#### `--pm <string>`

Use specific package manager to initialize the project. Available options: `yarn`, `npm`, `bun`. Default: `yarn`

#### `--package-name <string>`

Create project with custom package name for Android and bundle identifier for iOS. The correct package name should:
Expand Down
11 changes: 11 additions & 0 deletions packages/cli-clean/src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ export async function clean(
: []),
],
},
bun: {
description: 'Bun cache',
tasks: [
{
label: 'Clean Bun cache',
action: async () => {
await execa('bun', ['pm', 'cache', 'rm'], {cwd: projectRoot});
},
},
],
},
watchman: {
description: 'Stop Watchman and delete its cache',
tasks: [
Expand Down
1 change: 1 addition & 0 deletions packages/cli-doctor/src/tools/checkInstallation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import commandExists from 'command-exists';
export enum PACKAGE_MANAGERS {
YARN = 'YARN',
NPM = 'NPM',
BUN = 'BUN',
}

const isSoftwareNotInstalled = async (command: string): Promise<boolean> => {
Expand Down
1 change: 1 addition & 0 deletions packages/cli-doctor/src/tools/versionRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default {
NODE_JS: '>= 18',
YARN: '>= 1.10.x',
NPM: '>= 4.x',
BUN: '>= 1.0.0',
RUBY: '>= 2.6.10',
JAVA: '>= 17 <= 20',
// Android
Expand Down
1 change: 1 addition & 0 deletions packages/cli-doctor/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type EnvironmentInfo = {
Node: AvailableInformation;
Yarn: AvailableInformation;
npm: AvailableInformation;
bun: AvailableInformation;
Watchman: AvailableInformation;
};
Managers: {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/init/__tests__/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ afterEach(() => {
test('installTemplatePackage', async () => {
jest.spyOn(PackageManger, 'install').mockImplementationOnce(() => null);

await installTemplatePackage(TEMPLATE_NAME, TEMPLATE_SOURCE_DIR, true);
await installTemplatePackage(TEMPLATE_NAME, TEMPLATE_SOURCE_DIR, 'npm');

expect(PackageManger.install).toHaveBeenCalledWith([TEMPLATE_NAME], {
preferYarn: false,
packageManager: 'npm',
silent: true,
root: TEMPLATE_SOURCE_DIR,
});
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export default {
name: '--npm',
description: 'Forces using npm for initialization',
},
{
name: '--pm <string>',
description:
'Use specific package manager to initialize the project. Available options: `yarn`, `npm`, `bun`. Default: `yarn`',
},
{
name: '--directory <string>',
description: 'Uses a custom directory instead of `<projectName>`.',
Expand Down
78 changes: 73 additions & 5 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import * as PackageManager from '../../tools/packageManager';
import {installPods} from '@react-native-community/cli-doctor';
import banner from './banner';
import TemplateAndVersionError from './errors/TemplateAndVersionError';
import {getBunVersionIfAvailable} from '../../tools/bun';
import {getNpmVersionIfAvailable} from '../../tools/npm';
import {getYarnVersionIfAvailable} from '../../tools/yarn';

const DEFAULT_VERSION = 'latest';

type Options = {
template?: string;
npm?: boolean;
pm: PackageManager.PackageManager;
directory?: string;
displayName?: string;
title?: string;
Expand All @@ -39,6 +43,7 @@ interface TemplateOptions {
projectName: string;
templateUri: string;
npm?: boolean;
pm: PackageManager.PackageManager;
directory: string;
projectTitle?: string;
skipInstall?: boolean;
Expand Down Expand Up @@ -82,6 +87,7 @@ async function createFromTemplate({
projectName,
templateUri,
npm,
pm = 'yarn',
directory,
projectTitle,
skipInstall,
Expand All @@ -90,6 +96,20 @@ async function createFromTemplate({
logger.debug('Initializing new project');
logger.log(banner);

let packageManager = pm;
if (npm) {
logger.warn(
'Flag --npm is deprecated and will be removed soon. In the future, please use --pm npm instead.',
);

packageManager = 'npm';
}

const userAgent = userAgentPackageManager();
if (userAgent === 'bun') {
packageManager = 'bun';
}
TMisiukiewicz marked this conversation as resolved.
Show resolved Hide resolved

const projectDirectory = await setProjectDirectory(directory);

const loader = getLoader({text: 'Downloading template'});
Expand All @@ -100,7 +120,11 @@ async function createFromTemplate({
try {
loader.start();

await installTemplatePackage(templateUri, templateSourceDir, npm);
await installTemplatePackage(
templateUri,
templateSourceDir,
packageManager,
);

loader.succeed();
loader.start('Copying template');
Expand Down Expand Up @@ -137,7 +161,7 @@ async function createFromTemplate({

if (!skipInstall) {
await installDependencies({
npm,
packageManager,
loader,
root: projectDirectory,
});
Expand All @@ -153,18 +177,18 @@ async function createFromTemplate({
}

async function installDependencies({
npm,
packageManager,
loader,
root,
}: {
npm?: boolean;
packageManager: PackageManager.PackageManager;
loader: Loader;
root: string;
}) {
loader.start('Installing dependencies');

await PackageManager.installAll({
preferYarn: !npm,
packageManager,
silent: true,
root,
});
Expand All @@ -176,6 +200,24 @@ async function installDependencies({
loader.succeed();
}

function checkPackageManagerAvailability(options: Options) {
TMisiukiewicz marked this conversation as resolved.
Show resolved Hide resolved
if (options.pm === 'bun') {
const isBunAvailable = getBunVersionIfAvailable();

return isBunAvailable;
} else if (options.pm === 'npm') {
const isNpmAvailable = getNpmVersionIfAvailable();

return isNpmAvailable;
} else if (options.pm === 'yarn') {
const isYarnAvailable = getYarnVersionIfAvailable();

return isYarnAvailable;
TMisiukiewicz marked this conversation as resolved.
Show resolved Hide resolved
}

return true;
}

function createTemplateUri(options: Options, version: string): string {
const isTypescriptTemplate =
options.template === 'react-native-template-typescript';
Expand All @@ -202,13 +244,32 @@ async function createProject(
projectName,
templateUri,
npm: options.npm,
pm: options.pm,
directory,
projectTitle: options.title,
skipInstall: options.skipInstall,
packageName: options.packageName,
});
}

function userAgentPackageManager() {
const userAgent = process.env.npm_config_user_agent;
TMisiukiewicz marked this conversation as resolved.
Show resolved Hide resolved

if (userAgent) {
if (userAgent.startsWith('yarn')) {
return 'yarn';
}
if (userAgent.startsWith('npm')) {
return 'npm';
}
if (userAgent.startsWith('bun')) {
return 'bun';
}
}

return 'npm';
}

export default (async function initialize(
[projectName]: Array<string>,
options: Options,
Expand All @@ -223,6 +284,13 @@ export default (async function initialize(
const version = options.version || DEFAULT_VERSION;
const directoryName = path.relative(root, options.directory || projectName);

if (!checkPackageManagerAvailability(options)) {
logger.error(
'Seems like the package manager you want to use is not installed. Please install it or choose another package manager.',
);
return;
}

await createProject(projectName, directoryName, version, options);

const projectFolder = path.join(root, directoryName);
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/init/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ export type TemplateConfig = {
export async function installTemplatePackage(
templateName: string,
root: string,
npm?: boolean,
packageManager: PackageManager.PackageManager,
) {
logger.debug(`Installing template from ${templateName}`);

await PackageManager.init({
preferYarn: !npm,
packageManager,
silent: true,
root,
});

return PackageManager.install([templateName], {
preferYarn: !npm,
packageManager,
silent: true,
root,
});
Expand Down
Loading