Skip to content

Commit b1830b8

Browse files
authored
fix: react-native RC2 fixes (#2424)
1. Removing ~ npm version range from the template looking for installs > 0.75.0. This effectively forces version pinning against the react native release version. For example: npx @react-native-community/cli init Foo --version 0.75.0-rc2 Would install @react-native-community/[email protected] template only. We have a plan to do something more comprehensive, but this is affecting the release and had to be paused. There are 2 exceptions: - nightly - always points to the nightly tag - 0.75.0-rc.1 - points to 0.75.0-rc.1.1 to unblock the RC1 release 2. Fixed a bug which disabled using --template and --version for installs > 0.75.0, as the release candidates were < 0.75.0.
1 parent 6546159 commit b1830b8

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

packages/cli/src/commands/init/init.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -436,29 +436,21 @@ async function createTemplateUri(
436436
);
437437

438438
if (!useLegacyTemplate) {
439-
// This will use ~ to grab the latest patch version.
440-
//
441-
// Because we allow for many PATCHes to occur for a version of react-native, it's not possible to know
442-
// (without inspecting the template's npm metadata) which template works with which react-native version.
443-
// We are guaranteed that the latest version of react-native will work with the latest version of the
444-
// template on that version.
445-
//
446-
// Example:
447-
//
448-
// Suppose react-native 0.75.0 was released, and 0.74.8 was the previous version. If I ran
449-
// `init --version 0.74.8` it would be guaranteed to work with the latest version of the template
450-
// matching the MAJOR.MINOR, e.g. 0.74.21.
451439
if (/nightly/.test(version)) {
452440
logger.debug(
453441
"[template]: you're using a nightly version of react-native",
454442
);
455443
// Template nightly versions and react-native@nightly versions don't match (template releases at a much
456-
// lower cadence). The naming scheme we use for nightlies doesn't support '~'. We have to assume the user
457-
// is running against the latest nightly:
444+
// lower cadence). We have to assume the user is running against the latest nightly by pointing to the tag.
458445
return `${TEMPLATE_PACKAGE_COMMUNITY}@nightly`;
459446
}
460447

461-
return `${TEMPLATE_PACKAGE_COMMUNITY}@~${version}`;
448+
// Special case to unblock a release. This will need to be cleaned up in the future once 0.75 RC2 is out.
449+
if (version === '0.75.0-rc.1') {
450+
return `${TEMPLATE_PACKAGE_COMMUNITY}@0.75.0-rc.1.1`;
451+
}
452+
453+
return `${TEMPLATE_PACKAGE_COMMUNITY}@${version}`;
462454
}
463455

464456
logger.debug(
@@ -496,12 +488,6 @@ async function createProject(
496488
// 2. `--template` will always win over `--version` for the template.
497489
//
498490
// 3. For version < 0.75, the template ships with react-native.
499-
if (semver.valid(version) == null) {
500-
throw new Error(
501-
`--version=${options.version} -> '${version}' isn't valid SEMVER`,
502-
);
503-
}
504-
505491
const templateUri = await createTemplateUri(options, version);
506492

507493
logger.debug(`Template: '${templateUri}'`);
@@ -548,15 +534,29 @@ export default (async function initialize(
548534

549535
validateProjectName(projectName);
550536

551-
const version = await npmResolveConcreteVersion(
552-
options.platformName ?? 'react-native',
553-
options.version ?? DEFAULT_VERSION,
554-
);
537+
let version = options.version ?? DEFAULT_VERSION;
538+
539+
try {
540+
const updatedVersion = await npmResolveConcreteVersion(
541+
options.platformName ?? 'react-native',
542+
version,
543+
);
544+
logger.debug(`Mapped: ${version} -> ${updatedVersion}`);
545+
version = updatedVersion;
546+
} catch (e) {
547+
logger.debug(
548+
`Failed to get concrete version from '${version}': `,
549+
e as any,
550+
);
551+
}
555552

556553
// From 0.75 it actually is useful to be able to specify both the template and react-native version.
557554
// This should only be used by people who know what they're doing.
558555
if (!!options.template && !!options.version) {
559-
if (semver.gte(version, TEMPLATE_COMMUNITY_REACT_NATIVE_VERSION)) {
556+
// 0.75.0-nightly-20240618-5df5ed1a8' -> 0.75.0
557+
// 0.75.0-rc.1 -> 0.75.0
558+
const semverVersion = semver.coerce(version)?.version ?? version;
559+
if (semver.gte(semverVersion, TEMPLATE_COMMUNITY_REACT_NATIVE_VERSION)) {
560560
logger.warn(
561561
`Use ${chalk.bold('--template')} and ${chalk.bold(
562562
'--version',

packages/cli/src/tools/npm.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,20 @@ export async function npmResolveConcreteVersion(
4242
): Promise<string> {
4343
const url = new URL(registry);
4444
url.pathname = `${packageName}/${tagOrVersion}`;
45-
const json: any = await fetch(url).then((resp) => resp.json());
45+
const resp = await fetch(url);
46+
if (
47+
[
48+
200, // OK
49+
301, // Moved Permanemently
50+
302, // Found
51+
304, // Not Modified
52+
307, // Temporary Redirect
53+
308, // Permanent Redirect
54+
].indexOf(resp.status) === -1
55+
) {
56+
throw new Error(`Unknown version ${packageName}@${tagOrVersion}`);
57+
}
58+
const json: any = await resp.json();
4659
return json.version;
4760
}
4861

0 commit comments

Comments
 (0)