fix(cli): resolve `cli-server-api` starting from `react-native` (#2148)

This commit is contained in:
Tommy Nguyen 2023-01-25 15:27:21 +01:00 коммит произвёл GitHub
Родитель a6fce65f60
Коммит 341d28472f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 25 добавлений и 4 удалений

Просмотреть файл

@ -0,0 +1,5 @@
---
"@rnx-kit/cli": patch
---
Resolve `@react-native-community/cli-server-api` starting from `react-native` to avoid relying hoisting

Просмотреть файл

@ -40,15 +40,23 @@
"readline": "^1.3.0"
},
"peerDependencies": {
"@react-native-community/cli": ">=5.0.1",
"@react-native-community/cli-server-api": ">=5.0.1",
"jest-cli": "^26.0 || ^27.0"
"jest-cli": "^26.0 || ^27.0",
"react-native": ">=0.64"
},
"peerDependenciesMeta": {
"@react-native-community/cli": {
"optional": true
},
"@react-native-community/cli-server-api": {
"optional": true
},
"jest-cli": {
"optional": true
},
"react-native": {
"optional": true
}
},
"devDependencies": {

Просмотреть файл

@ -47,10 +47,14 @@ export type CLIStartOptions = {
interactive: boolean;
};
function friendlyRequire<T>(module: string): T {
function friendlyRequire<T>(...modules: string[]): T {
try {
return require(module) as T;
const modulePath = modules.reduce((startDir, module) => {
return require.resolve(module, { paths: [startDir] });
}, process.cwd());
return require(modulePath) as T;
} catch (_) {
const module = modules[modules.length - 1];
throw new Error(
`Cannot find module '${module}'. This probably means that '@rnx-kit/cli' is not compatible with the version of '@react-native-community/cli' that you are currently using. Please update to the latest version and try again. If the issue still persists after the update, please file a bug at https://github.com/microsoft/rnx-kit/issues.`
);
@ -72,7 +76,11 @@ export async function rnxStart(
const { createDevServerMiddleware, indexPageMiddleware } = friendlyRequire<
typeof CliServerApi
>("@react-native-community/cli-server-api");
>(
"react-native",
"@react-native-community/cli",
"@react-native-community/cli-server-api"
);
// interactive mode requires raw access to stdin
let interactive = cliOptions.interactive;