Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions packages/cli/src/commands/init/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
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.`,

Check warning on line 65 in packages/cli/src/commands/init/version.ts

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
);
}
return `${TEMPLATE_PACKAGE_COMMUNITY}@${templateVersion}`;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/tools/__tests__/npm-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'}},
Expand All @@ -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 () => {
Expand Down
18 changes: 1 addition & 17 deletions packages/cli/src/tools/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TemplateVersion | undefined> {
Expand All @@ -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)) {
Expand All @@ -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
Expand All @@ -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;
}
Loading