|
1 | 1 | import fs from 'fs-extra';
|
2 |
| -import path from 'path'; |
| 2 | +import getLatestVersion from 'get-latest-version'; |
3 | 3 | import https from 'https';
|
4 |
| -import { spawn } from '../utils/spawn'; |
5 |
| -import sortObjectKeys from '../utils/sortObjectKeys'; |
| 4 | +import path from 'path'; |
6 | 5 | import type { TemplateConfiguration } from '../template';
|
| 6 | +import sortObjectKeys from '../utils/sortObjectKeys'; |
| 7 | +import { spawn } from '../utils/spawn'; |
7 | 8 |
|
8 | 9 | const FILES_TO_DELETE = [
|
9 | 10 | '__tests__',
|
@@ -51,60 +52,70 @@ export default async function generateExampleApp({
|
51 | 52 | }) {
|
52 | 53 | const directory = path.join(root, 'example');
|
53 | 54 |
|
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 |
| - |
97 | 55 | let args: string[] = [];
|
98 | 56 |
|
99 | 57 | switch (config.example) {
|
100 | 58 | 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 | + ]; |
102 | 74 | break;
|
103 | 75 | 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 | + } |
105 | 109 | break;
|
106 | 110 | 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 | + ]; |
108 | 119 | break;
|
109 | 120 | }
|
110 | 121 |
|
|
0 commit comments