Skip to content

Commit

Permalink
create --pm flag and deprecate --npm
Browse files Browse the repository at this point in the history
  • Loading branch information
TMisiukiewicz committed Sep 13, 2023
1 parent 92d8c85 commit 873c7e4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 38 deletions.
6 changes: 4 additions & 2 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ 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

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

Force use of bun during initialization
Use specific package manager to initialize the project. Available options: `yarn`, `npm`, `bun`. Default: `yarn`

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

Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export default {
description: 'Forces using npm for initialization',
},
{
name: '--bun',
description: 'Forces using bun for initialization',
name: '--pm <string>',
description:
'Use specific package manager to initialize the project. Available options: `yarn`, `npm`, `bun`. Default: `yarn`',
},
{
name: '--directory <string>',
Expand Down
47 changes: 26 additions & 21 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ 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;
bun?: boolean;
pm: PackageManager.PackageManager;
directory?: string;
displayName?: string;
title?: string;
Expand All @@ -42,7 +43,7 @@ interface TemplateOptions {
projectName: string;
templateUri: string;
npm?: boolean;
bun?: boolean;
pm: PackageManager.PackageManager;
directory: string;
projectTitle?: string;
skipInstall?: boolean;
Expand Down Expand Up @@ -86,7 +87,7 @@ async function createFromTemplate({
projectName,
templateUri,
npm,
bun,
pm = 'yarn',
directory,
projectTitle,
skipInstall,
Expand All @@ -95,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';
}

const projectDirectory = await setProjectDirectory(directory);

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

let packageManager: PackageManager.PackageManager = 'yarn';

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

await installTemplatePackage(
templateUri,
templateSourceDir,
Expand Down Expand Up @@ -174,7 +181,7 @@ async function installDependencies({
loader,
root,
}: {
packageManager?: PackageManager.PackageManager;
packageManager: PackageManager.PackageManager;
loader: Loader;
root: string;
}) {
Expand All @@ -194,14 +201,18 @@ async function installDependencies({
}

function checkPackageManagerAvailability(options: Options) {
if (options.bun) {
if (options.pm === 'bun') {
const isBunAvailable = getBunVersionIfAvailable();

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

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

return isYarnAvailable;
}

return true;
Expand Down Expand Up @@ -233,7 +244,7 @@ async function createProject(
projectName,
templateUri,
npm: options.npm,
bun: options.bun,
pm: options.pm,
directory,
projectTitle: options.title,
skipInstall: options.skipInstall,
Expand Down Expand Up @@ -280,12 +291,6 @@ export default (async function initialize(
return;
}

const packageManager = userAgentPackageManager();

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

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

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

Expand Down
20 changes: 16 additions & 4 deletions packages/cli/src/tools/__tests__/packageManager-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('yarn', () => {
jest
.spyOn(yarn, 'getYarnVersionIfAvailable')
.mockImplementation(() => true);
jest.spyOn(yarn, 'isProjectUsingYarn').mockImplementation(() => true);

jest.spyOn(logger, 'isVerbose').mockImplementation(() => false);
});
Expand Down Expand Up @@ -154,7 +155,10 @@ describe('bun', () => {

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

expect(execa).toHaveBeenCalledWith(
'npm',
Expand All @@ -164,9 +168,12 @@ it('should use npm if yarn is not available', () => {
});

it('should use npm if project is not using yarn', () => {
jest.spyOn(yarn, 'isProjectUsingYarn').mockImplementation(() => false);
jest.spyOn(yarn, 'isProjectUsingYarn').mockImplementation(() => undefined);

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

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

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

Expand All @@ -200,7 +208,11 @@ test.each([
jest.spyOn(yarn, 'isProjectUsingYarn').mockImplementation(() => true);
jest.spyOn(logger, 'isVerbose').mockImplementation(() => isVerbose);

PackageManager.install(PACKAGES, {root: PROJECT_ROOT, silent: true});
PackageManager.install(PACKAGES, {
packageManager: 'yarn',
root: PROJECT_ROOT,
silent: true,
});

expect(execa).toHaveBeenCalledWith('yarn', ['add', ...PACKAGES], {
stdio: stdioType,
Expand Down
24 changes: 16 additions & 8 deletions packages/cli/src/tools/packageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import execa from 'execa';
import {logger} from '@react-native-community/cli-tools';
import {getYarnVersionIfAvailable, isProjectUsingYarn} from './yarn';
import {getBunVersionIfAvailable, isProjectUsingBun} from './bun';
import {getNpmVersionIfAvailable, isProjectUsingNpm} from './npm';

export type PackageManager = keyof typeof packageManagers;

type Options = {
packageManager?: PackageManager;
packageManager: PackageManager;
silent?: boolean;
root: string;
};
Expand Down Expand Up @@ -61,22 +62,29 @@ function executeCommand(
});
}

function shouldUseYarn(options: Options) {
if (options && options.packageManager === 'yarn') {
return options.packageManager === 'yarn' && getYarnVersionIfAvailable();
export function shouldUseYarn(options: Options) {
if (options.packageManager === 'yarn') {
return getYarnVersionIfAvailable();
}

return isProjectUsingYarn(options.root) && getYarnVersionIfAvailable();
}

function shouldUseBun(options: Options) {
if (options && options.packageManager === 'bun') {
return options.packageManager === 'bun' && getBunVersionIfAvailable();
export function shouldUseBun(options: Options) {
if (options.packageManager === 'bun') {
return getBunVersionIfAvailable();
}

return isProjectUsingBun(options.root) && getBunVersionIfAvailable();
}

export function shouldUseNpm(options: Options) {
if (options.packageManager === 'npm') {
return getNpmVersionIfAvailable();
}

return isProjectUsingNpm(options.root) && getNpmVersionIfAvailable();
}

export function init(options: Options) {
return configurePackageManager([], 'init', options);
}
Expand Down

0 comments on commit 873c7e4

Please sign in to comment.