diff --git a/packages/cli/src/commands/init/version.ts b/packages/cli/src/commands/init/version.ts index 2fb8dfcaf..356c42f95 100644 --- a/packages/cli/src/commands/init/version.ts +++ b/packages/cli/src/commands/init/version.ts @@ -59,6 +59,12 @@ export async function createTemplateUri( return `${TEMPLATE_PACKAGE_COMMUNITY}@nightly`; } const templateVersion = await getTemplateVersion(version); + if (templateVersion == null) { + throw new Error( + `Unable to find a template for react-native version '${version}'. ` + + `Please check that the version exists and has a corresponding template published to npm.`, + ); + } return `${TEMPLATE_PACKAGE_COMMUNITY}@${templateVersion}`; } diff --git a/packages/cli/src/tools/__tests__/npm-test.ts b/packages/cli/src/tools/__tests__/npm-test.ts index c60315d5f..a1ed08dc0 100644 --- a/packages/cli/src/tools/__tests__/npm-test.ts +++ b/packages/cli/src/tools/__tests__/npm-test.ts @@ -39,7 +39,7 @@ describe('getTemplateVersion', () => { expect(await getTemplateVersion(VERSION)).toEqual('1.2.3'); }); - it('should matching latest MAJOR.MINOR if MAJOR.MINOR.PATCH has no match', async () => { + it('should NOT match if MAJOR.MINOR.PATCH has no exact match', async () => { fetchReturn({ versions: { '3.2.1': {scripts: {version: '0.75.1'}}, @@ -51,7 +51,7 @@ describe('getTemplateVersion', () => { }, }); - expect(await getTemplateVersion('0.75.3')).toEqual('3.2.2'); + expect(await getTemplateVersion('0.75.3')).toEqual(undefined); }); it('should NOT matching when MAJOR.MINOR is not found', async () => { diff --git a/packages/cli/src/tools/npm.ts b/packages/cli/src/tools/npm.ts index 3ad234a80..34c2a9456 100644 --- a/packages/cli/src/tools/npm.ts +++ b/packages/cli/src/tools/npm.ts @@ -119,11 +119,6 @@ class Template { } } -const minorVersion = (version: string) => { - const v = semver.parse(version)!; - return `${v.major}.${v.minor}`; -}; - export async function getTemplateVersion( reactNativeVersion: string, ): Promise { @@ -139,11 +134,8 @@ export async function getTemplateVersion( // - IF there a match for React Native MAJOR.MINOR.PATCH? // - Yes: if there are >= 2 versions, pick the one last published. This lets us release // specific fixes for React Native versions. - // - ELSE, is there a match for React Native MINOR.PATCH? - // - Yes: if there are >= 2 versions, pick the one last published. This decouples us from - // React Native releases. // - No: we don't have a version of the template for a version of React Native. There should - // at a minimum be at last one version cut for each MINOR.PATCH since 0.75. Before this + // at a minimum be at least one version cut for each MAJOR.MINOR.PATCH since 0.75. Before this // the template was shipped with React Native const rnToTemplate: VersionedTemplates = {}; for (const [templateVersion, pkg] of Object.entries(json.versions)) { @@ -159,12 +151,8 @@ export async function getTemplateVersion( json.time[templateVersion], ); - const rnMinorVersion = minorVersion(rnVersion); - rnToTemplate[rnVersion] = rnToTemplate[rnVersion] ?? []; rnToTemplate[rnVersion].push(template); - rnToTemplate[rnMinorVersion] = rnToTemplate[rnMinorVersion] ?? []; - rnToTemplate[rnMinorVersion].push(template); } // Make sure the last published is the first one in each version of React Native @@ -177,9 +165,5 @@ export async function getTemplateVersion( if (reactNativeVersion in rnToTemplate) { return rnToTemplate[reactNativeVersion][0].version; } - const rnMinorVersion = minorVersion(reactNativeVersion); - if (rnMinorVersion in rnToTemplate) { - return rnToTemplate[rnMinorVersion][0].version; - } return; }