Skip to content

Commit eebf3cb

Browse files
committed
fix: fix handling react native version for test app
1 parent 5bc7888 commit eebf3cb

File tree

5 files changed

+259
-62
lines changed

5 files changed

+259
-62
lines changed

packages/create-react-native-library/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dedent": "^0.7.0",
4545
"ejs": "^3.1.8",
4646
"fs-extra": "^10.1.0",
47+
"get-latest-version": "^5.1.0",
4748
"github-username": "^6.0.0",
4849
"kleur": "^4.1.4",
4950
"ora": "^5.4.1",

packages/create-react-native-library/src/exampleApp/generateExampleApp.ts

+60-49
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import fs from 'fs-extra';
2-
import path from 'path';
2+
import getLatestVersion from 'get-latest-version';
33
import https from 'https';
4-
import { spawn } from '../utils/spawn';
5-
import sortObjectKeys from '../utils/sortObjectKeys';
4+
import path from 'path';
65
import type { TemplateConfiguration } from '../template';
6+
import sortObjectKeys from '../utils/sortObjectKeys';
7+
import { spawn } from '../utils/spawn';
78

89
const FILES_TO_DELETE = [
910
'__tests__',
@@ -51,60 +52,70 @@ export default async function generateExampleApp({
5152
}) {
5253
const directory = path.join(root, 'example');
5354

54-
// `npx --package react-native-test-app@latest init --name ${projectName}Example --destination example --version ${reactNativeVersion}`
55-
const testAppArgs = [
56-
'--package',
57-
`react-native-test-app@latest`,
58-
'init',
59-
'--name',
60-
`${config.project.name}Example`,
61-
`--destination`,
62-
directory,
63-
...(reactNativeVersion !== 'latest'
64-
? ['--version', reactNativeVersion]
65-
: []),
66-
'--platform',
67-
'ios',
68-
'--platform',
69-
'android',
70-
];
71-
72-
// `npx @react-native-community/cli init <projectName> --directory example --skip-install`
73-
const vanillaArgs = [
74-
`@react-native-community/cli`,
75-
'init',
76-
`${config.project.name}Example`,
77-
'--package-name',
78-
`${config.project.package}.example`,
79-
'--directory',
80-
directory,
81-
'--version',
82-
reactNativeVersion,
83-
'--skip-install',
84-
'--pm',
85-
'npm',
86-
];
87-
88-
// `npx create-expo-app example --no-install --template blank`
89-
const expoArgs = [
90-
'create-expo-app@latest',
91-
directory,
92-
'--no-install',
93-
'--template',
94-
'blank',
95-
];
96-
9755
let args: string[] = [];
9856

9957
switch (config.example) {
10058
case 'vanilla':
101-
args = vanillaArgs;
59+
// `npx @react-native-community/cli init <projectName> --directory example --skip-install`
60+
args = [
61+
`@react-native-community/cli`,
62+
'init',
63+
`${config.project.name}Example`,
64+
'--package-name',
65+
`${config.project.package}.example`,
66+
'--directory',
67+
directory,
68+
'--version',
69+
reactNativeVersion,
70+
'--skip-install',
71+
'--pm',
72+
'npm',
73+
];
10274
break;
10375
case 'test-app':
104-
args = testAppArgs;
76+
{
77+
// Test App requires React Native version to be a semver version
78+
const matchedReactNativeVersion = /(\d+\.\d+[-.0-9a-z]*)/.test(
79+
reactNativeVersion
80+
)
81+
? reactNativeVersion
82+
: await getLatestVersion('react-native', {
83+
range: reactNativeVersion,
84+
});
85+
86+
if (!matchedReactNativeVersion) {
87+
throw new Error(
88+
`Could not find a matching version for react-native: ${reactNativeVersion}`
89+
);
90+
}
91+
92+
// `npx --package react-native-test-app@latest init --name ${projectName}Example --destination example --version ${reactNativeVersion}`
93+
args = [
94+
'--package',
95+
`react-native-test-app@latest`,
96+
'init',
97+
'--name',
98+
`${config.project.name}Example`,
99+
`--destination`,
100+
directory,
101+
'--version',
102+
matchedReactNativeVersion,
103+
'--platform',
104+
'ios',
105+
'--platform',
106+
'android',
107+
];
108+
}
105109
break;
106110
case 'expo':
107-
args = expoArgs;
111+
// `npx create-expo-app example --no-install --template blank`
112+
args = [
113+
'create-expo-app@latest',
114+
directory,
115+
'--no-install',
116+
'--template',
117+
'blank',
118+
];
108119
break;
109120
}
110121

packages/create-react-native-library/src/inform.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function printUsedRNVersion(
107107
version: string,
108108
config: TemplateConfiguration
109109
) {
110-
if (config.example === 'vanilla') {
110+
if (config.example === 'vanilla' || config.example === 'test-app') {
111111
console.log(
112112
`${kleur.blue('ℹ')} Using untested ${kleur.cyan(
113113
`react-native@${version}`

packages/create-react-native-library/src/input.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ const EXAMPLE_CHOICES = (
5050
title: 'Test App by Microsoft',
5151
value: 'test-app',
5252
description: "app's native code is abstracted away",
53-
// The test app is disabled for now until proper
54-
// Codegen spec shipping is implemented
53+
// Test App currently doesn't work with React Native 0.79.2
54+
// due to missing `Gemfile` in the template
5555
disabled: !process.env.CRNL_ENABLE_TEST_APP,
5656
},
5757
{

0 commit comments

Comments
 (0)