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 1 commit
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
4 changes: 4 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Skip dependencies installation

Force use of npm during initialization

#### `--bun`
TMisiukiewicz marked this conversation as resolved.
Show resolved Hide resolved

Force use of bun during initialization

#### `--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
4 changes: 4 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,10 @@ export default {
name: '--npm',
description: 'Forces using npm for initialization',
},
{
name: '--bun',
description: 'Forces using bun for initialization',
},
{
name: '--directory <string>',
description: 'Uses a custom directory instead of `<projectName>`.',
Expand Down
50 changes: 45 additions & 5 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const DEFAULT_VERSION = 'latest';
type Options = {
template?: string;
npm?: boolean;
bun?: boolean;
directory?: string;
displayName?: string;
title?: string;
Expand All @@ -39,6 +40,7 @@ interface TemplateOptions {
projectName: string;
templateUri: string;
npm?: boolean;
bun?: boolean;
directory: string;
projectTitle?: string;
skipInstall?: boolean;
Expand Down Expand Up @@ -82,6 +84,7 @@ async function createFromTemplate({
projectName,
templateUri,
npm,
bun,
directory,
projectTitle,
skipInstall,
Expand All @@ -100,7 +103,19 @@ async function createFromTemplate({
try {
loader.start();

await installTemplatePackage(templateUri, templateSourceDir, npm);
let packageManager: PackageManager.PackageManager = 'yarn';

if (npm) {
packageManager = 'npm';
} else if (bun) {
packageManager = 'bun';
}

await installTemplatePackage(
templateUri,
templateSourceDir,
packageManager,
);

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

if (!skipInstall) {
await installDependencies({
npm,
packageManager,
loader,
root: projectDirectory,
});
Expand All @@ -153,18 +168,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 Down Expand Up @@ -202,13 +217,32 @@ async function createProject(
projectName,
templateUri,
npm: options.npm,
bun: options.bun,
directory,
projectTitle: options.title,
skipInstall: options.skipInstall,
packageName: options.packageName,
});
}

function resolvePackageManager() {
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 +257,12 @@ export default (async function initialize(
const version = options.version || DEFAULT_VERSION;
const directoryName = path.relative(root, options.directory || projectName);

const packageManager = resolvePackageManager();

if (packageManager === 'bun') {
options.bun = true;
}

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
74 changes: 66 additions & 8 deletions packages/cli/src/tools/__tests__/packageManager-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ describe('yarn', () => {
});

it('should install', () => {
PackageManager.install(PACKAGES, {preferYarn: true, root: PROJECT_ROOT});
PackageManager.install(PACKAGES, {
packageManager: 'yarn',
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith('yarn', ['add', ...PACKAGES], EXEC_OPTS);
});

it('should installDev', () => {
PackageManager.installDev(PACKAGES, {preferYarn: true, root: PROJECT_ROOT});
PackageManager.installDev(PACKAGES, {
packageManager: 'yarn',
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith(
'yarn',
Expand All @@ -38,7 +44,10 @@ describe('yarn', () => {
});

it('should uninstall', () => {
PackageManager.uninstall(PACKAGES, {preferYarn: true, root: PROJECT_ROOT});
PackageManager.uninstall(PACKAGES, {
packageManager: 'yarn',
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith(
'yarn',
Expand All @@ -50,7 +59,10 @@ describe('yarn', () => {

describe('npm', () => {
it('should install', () => {
PackageManager.install(PACKAGES, {preferYarn: false, root: PROJECT_ROOT});
PackageManager.install(PACKAGES, {
packageManager: 'npm',
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith(
'npm',
Expand All @@ -61,7 +73,7 @@ describe('npm', () => {

it('should installDev', () => {
PackageManager.installDev(PACKAGES, {
preferYarn: false,
packageManager: 'npm',
root: PROJECT_ROOT,
});

Expand All @@ -73,7 +85,10 @@ describe('npm', () => {
});

it('should uninstall', () => {
PackageManager.uninstall(PACKAGES, {preferYarn: false, root: PROJECT_ROOT});
PackageManager.uninstall(PACKAGES, {
packageManager: 'npm',
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith(
'npm',
Expand All @@ -83,9 +98,50 @@ describe('npm', () => {
});
});

describe('bun', () => {
TMisiukiewicz marked this conversation as resolved.
Show resolved Hide resolved
it('should install', () => {
PackageManager.install(PACKAGES, {
packageManager: 'bun',
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith(
'bun',
['add', '--exact', ...PACKAGES],
EXEC_OPTS,
);
});

it('should installDev', () => {
PackageManager.installDev(PACKAGES, {
packageManager: 'bun',
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith(
'bun',
['add', '--dev', '--exact', ...PACKAGES],
EXEC_OPTS,
);
});

it('should uninstall', () => {
PackageManager.uninstall(PACKAGES, {
packageManager: 'bun',
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith(
'bun',
['remove', ...PACKAGES],
EXEC_OPTS,
);
});
});

it('should use npm if yarn is not available', () => {
jest.spyOn(yarn, 'getYarnVersionIfAvailable').mockImplementation(() => false);
PackageManager.install(PACKAGES, {preferYarn: true, root: PROJECT_ROOT});
PackageManager.install(PACKAGES, {root: PROJECT_ROOT});

expect(execa).toHaveBeenCalledWith(
'npm',
Expand All @@ -111,7 +167,9 @@ it('should use yarn if project is using yarn', () => {
jest.spyOn(yarn, 'getYarnVersionIfAvailable').mockImplementation(() => true);
jest.spyOn(yarn, 'isProjectUsingYarn').mockImplementation(() => true);

PackageManager.install(PACKAGES, {root: PROJECT_ROOT});
PackageManager.install(PACKAGES, {
root: PROJECT_ROOT,
});

expect(execa).toHaveBeenCalledWith('yarn', ['add', ...PACKAGES], EXEC_OPTS);
expect(yarn.isProjectUsingYarn).toHaveBeenCalledWith(PROJECT_ROOT);
Expand Down
32 changes: 32 additions & 0 deletions packages/cli/src/tools/bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {logger} from '@react-native-community/cli-tools';
import {execSync} from 'child_process';
import findUp from 'find-up';
import semver from 'semver';

export function getBunVersionIfAvailable() {
let bunVersion;

try {
bunVersion = (
execSync('bun --version', {
stdio: [0, 'pipe', 'ignore'],
}).toString() || ''
).trim();
} catch (error) {
return null;
}

try {
if (semver.gte(bunVersion, '1.0.0')) {
return bunVersion;
}
return null;
} catch (error) {
logger.error(`Cannot parse bun version: ${bunVersion}`);
return null;
}
}

export function isProjectUsingBun(cwd: string) {
return findUp.sync('bun.lockb', {cwd});
}
Loading