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: separate package for PackageManager util #2462

Closed
Changes from all 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
4 changes: 4 additions & 0 deletions packages/cli-package-manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
29 changes: 29 additions & 0 deletions packages/cli-package-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@react-native-community/cli-package-manager",
"version": "14.0.1",
"license": "MIT",
"main": "build/index.js",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@react-native-community/cli-tools": "^14.0.0",
"execa": "^5.0.0",
"find-up": "^5.0.0",
"semver": "^7.5.2"
},
"devDependencies": {
"@react-native-community/cli-types": "14.0.0"
},
"files": [
"build",
"!*.d.ts",
"!*.map"
],
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-tools",
"repository": {
"type": "git",
"url": "https://github.com/react-native-community/cli.git",
"directory": "packages/cli-package-manager"
}
}
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import execa from 'execa';
import * as yarn from '../yarn';
import * as bun from '../bun';
import {logger} from '@react-native-community/cli-tools';
import * as PackageManager from '../packageManager';
import * as PackageManager from '..';

const PACKAGES = ['react', 'react-native'];
const PROJECT_ROOT = '/some/dir';
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/cli-package-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './bun';
export * from './npm';
export * from './yarn';
export * from './packageManager';
export * from './executeCommand';
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions packages/cli-package-manager/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [{"path": "../cli-types"}, {"path": "../cli-tools"}]
}
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
"@react-native-community/cli-server-api": "14.0.1",
"@react-native-community/cli-tools": "14.0.1",
"@react-native-community/cli-types": "14.0.1",
"@react-native-community/cli-package-manager": "14.0.1",
"chalk": "^4.1.2",
"commander": "^9.4.1",
"deepmerge": "^4.3.0",
13 changes: 9 additions & 4 deletions packages/cli/src/commands/init/__tests__/template.test.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ jest.mock('execa', () => jest.fn());
import execa from 'execa';
import path from 'path';
import fs from 'fs';
import * as PackageManger from '../../../tools/packageManager';
import * as PackageManger from '@react-native-community/cli-package-manager';
import {
installTemplatePackage,
getTemplateConfig,
@@ -19,9 +19,12 @@ afterEach(() => {
jest.clearAllMocks();
});

test('installTemplatePackage', async () => {
jest.spyOn(PackageManger, 'install').mockImplementationOnce(() => null);
jest.mock('@react-native-community/cli-package-manager', () => ({
init: jest.fn(() => null),
install: jest.fn(() => null),
}));

test('installTemplatePackage', async () => {
await installTemplatePackage(TEMPLATE_NAME, TEMPLATE_SOURCE_DIR, 'npm');

expect(PackageManger.install).toHaveBeenCalledWith([TEMPLATE_NAME], {
@@ -61,7 +64,9 @@ test('copyTemplate', async () => {
const CWD = '.';

jest.spyOn(path, 'resolve').mockImplementationOnce((...e) => e.join('/'));
jest.spyOn(copyFiles, 'default').mockImplementationOnce(() => null);
jest
.spyOn(copyFiles, 'default')
.mockImplementationOnce(() => Promise.resolve([]));
jest.spyOn(process, 'cwd').mockImplementationOnce(() => CWD);

await copyTemplate(TEMPLATE_NAME, TEMPLATE_DIR, TEMPLATE_SOURCE_DIR);
23 changes: 8 additions & 15 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
@@ -20,23 +20,16 @@ import {
executePostInitScript,
} from './template';
import {changePlaceholderInTemplate} from './editTemplate';
import * as PackageManager from '../../tools/packageManager';
import * as PackageManager from '@react-native-community/cli-package-manager';
import banner from './banner';
import TemplateAndVersionError from './errors/TemplateAndVersionError';
import {getBunVersionIfAvailable} from '../../tools/bun';
import {
getNpmVersionIfAvailable,
npmResolveConcreteVersion,
} from '../../tools/npm';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import {createHash} from 'crypto';
import {
createGitRepository,
checkGitInstallation,
checkIfFolderIsGitRepo,
} from './git';
import semver from 'semver';
import {executeCommand} from '../../tools/executeCommand';
import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError';

const DEFAULT_VERSION = 'latest';
@@ -89,21 +82,21 @@ const YARN_VERSION = '3.6.4';

const bumpYarnVersion = async (root: string) => {
try {
let yarnVersion = semver.parse(getYarnVersionIfAvailable());
let yarnVersion = semver.parse(PackageManager.getYarnVersionIfAvailable());

if (yarnVersion) {
// `yarn set` is unsupported until 1.22, however it's a alias (yarnpkg/yarn/pull/7862) calling `policies set-version`.
let setVersionArgs = ['set', 'version', YARN_VERSION];
if (yarnVersion.major === 1 && yarnVersion.minor < 22) {
setVersionArgs = ['policies', 'set-version', YARN_VERSION];
}
await executeCommand('yarn', setVersionArgs, {
await PackageManager.executeCommand('yarn', setVersionArgs, {
root,
silent: !logger.isVerbose(),
});

// 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(
await PackageManager.executeCommand(
'yarn',
['config', 'set', 'nodeLinker', 'node-modules'],
{root, silent: !logger.isVerbose()},
@@ -387,11 +380,11 @@ function checkPackageManagerAvailability(
packageManager: PackageManager.PackageManager,
) {
if (packageManager === 'bun') {
return getBunVersionIfAvailable();
return PackageManager.getBunVersionIfAvailable();
} else if (packageManager === 'npm') {
return getNpmVersionIfAvailable();
return PackageManager.getNpmVersionIfAvailable();
} else if (packageManager === 'yarn') {
return getYarnVersionIfAvailable();
return PackageManager.getYarnVersionIfAvailable();
}

return false;
@@ -532,7 +525,7 @@ export default (async function initialize(
let version = options.version ?? DEFAULT_VERSION;

try {
const updatedVersion = await npmResolveConcreteVersion(
const updatedVersion = await PackageManager.npmResolveConcreteVersion(
options.platformName ?? 'react-native',
version,
);
10 changes: 6 additions & 4 deletions packages/cli/src/commands/init/template.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import execa from 'execa';
import path from 'path';
import {logger, CLIError} from '@react-native-community/cli-tools';
import * as PackageManager from '../../tools/packageManager';
import * as PackageManager from '@react-native-community/cli-package-manager';
import copyFiles from '../../tools/copyFiles';
import replacePathSepForRegex from '../../tools/replacePathSepForRegex';
import fs from 'fs';
import chalk from 'chalk';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import {executeCommand} from '../../tools/executeCommand';
import {executeCommand} from '@react-native-community/cli-package-manager';

export type TemplateConfig = {
placeholderName: string;
@@ -30,7 +29,10 @@ export async function installTemplatePackage(
root,
});

if (packageManager === 'yarn' && getYarnVersionIfAvailable() !== null) {
if (
packageManager === 'yarn' &&
PackageManager.getYarnVersionIfAvailable() !== null
) {
const options = {
root,
silent: true,
3 changes: 2 additions & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
{"path": "../cli-server-api"},
{"path": "../cli-types"},
{"path": "../cli-debugger-ui"},
{"path": "../cli-tools"}
{"path": "../cli-tools"},
{"path": "../cli-package-manager"}
]
}
31 changes: 3 additions & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -10743,16 +10743,7 @@ string-natural-compare@^3.0.1:
resolved "https://registry.yarnpkg.com/string-natural-compare/-/string-natural-compare-3.0.1.tgz#7a42d58474454963759e8e8b7ae63d71c1e7fdf4"
integrity sha512-n3sPwynL1nwKi3WJ6AIsClwBMa0zTi54fn2oLU6ndfTSIO05xaznjSf15PcBZU6FNWbmN5Q6cxT4V5hGvB4taw==

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -10847,7 +10838,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -10868,13 +10859,6 @@ strip-ansi@^5.0.0, strip-ansi@^5.2.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.0.1.tgz#61740a08ce36b61e50e65653f07060d000975fb2"
@@ -11753,7 +11737,7 @@ wordwrap@^1.0.0:
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -11771,15 +11755,6 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"