diff --git a/packages/create-react-native-library/src/index.ts b/packages/create-react-native-library/src/index.ts index c72ad435e..02c0cf9cc 100644 --- a/packages/create-react-native-library/src/index.ts +++ b/packages/create-react-native-library/src/index.ts @@ -139,11 +139,17 @@ type Answers = { repoUrl: string; languages: ProjectLanguages; type?: ProjectType; - example?: boolean; + example?: ExampleType; reactNativeVersion?: string; withRecommendedOptions?: boolean; }; +export enum ExampleType { + Vanilla = 'vanilla', + TestApp = 'test-app', + Expo = 'expo', +} + const LANGUAGE_CHOICES: { title: string; value: ProjectLanguages; @@ -273,9 +279,10 @@ const args: Record = { type: 'boolean', }, 'example': { - description: 'Whether to create an example app', - type: 'boolean', - default: true, + description: 'Type of the app to create', + type: 'string', + choices: ['vanilla', 'test-app'], + default: 'vanilla', }, 'with-recommended-options': { description: `Whether to use the recommended template. ${RECOMMENDED_TEMPLATE.description}`, @@ -632,7 +639,11 @@ async function create(argv: yargs.Arguments) { : 'legacy'; const example = - hasExample && !local ? (type === 'library' ? 'expo' : 'native') : 'none'; + hasExample && !local + ? type === 'library' + ? ExampleType.Expo + : hasExample + : null; const project = slug.replace(/^(react-native-|@[^/]+\/)/, ''); @@ -718,7 +729,7 @@ async function create(argv: yargs.Arguments) { await fs.mkdirp(folder); if (reactNativeVersion != null) { - if (example === 'expo') { + if (example === ExampleType.Expo) { console.warn( `${kleur.yellow('⚠')} Ignoring --react-native-version for Expo example` ); @@ -733,7 +744,7 @@ async function create(argv: yargs.Arguments) { const spinner = ora().start(); - if (example !== 'none') { + if (example) { spinner.text = 'Generating example app'; await generateExampleApp({ @@ -753,7 +764,7 @@ async function create(argv: yargs.Arguments) { } else { await copyDir(COMMON_FILES, folder); - if (example !== 'none') { + if (example) { await copyDir(COMMON_EXAMPLE_FILES, folder); } } @@ -762,7 +773,7 @@ async function create(argv: yargs.Arguments) { await copyDir(JS_FILES, folder); await copyDir(EXPO_FILES, folder); } else { - if (example !== 'none') { + if (example) { await copyDir( path.join(EXAMPLE_FILES, 'example'), path.join(folder, 'example') @@ -771,7 +782,7 @@ async function create(argv: yargs.Arguments) { await copyDir(NATIVE_COMMON_FILES, folder); - if (example !== 'none') { + if (example) { await copyDir(NATIVE_COMMON_EXAMPLE_FILES, folder); } @@ -794,7 +805,7 @@ async function create(argv: yargs.Arguments) { } } - if (example !== 'none') { + if (example) { // Set `react` and `react-native` versions of root `package.json` from example `package.json` const examplePackageJson = await fs.readJSON( path.join(folder, 'example', 'package.json') diff --git a/packages/create-react-native-library/src/utils/generateExampleApp.ts b/packages/create-react-native-library/src/utils/generateExampleApp.ts index 0525c93f6..07b5d8136 100644 --- a/packages/create-react-native-library/src/utils/generateExampleApp.ts +++ b/packages/create-react-native-library/src/utils/generateExampleApp.ts @@ -2,6 +2,7 @@ import fs from 'fs-extra'; import path from 'path'; import https from 'https'; import { spawn } from './spawn'; +import { ExampleType } from './../index'; const FILES_TO_DELETE = [ '__tests__', @@ -55,7 +56,7 @@ export default async function generateExampleApp({ arch, reactNativeVersion = 'latest', }: { - type: 'expo' | 'native'; + type: ExampleType; dest: string; slug: string; projectName: string; @@ -63,33 +64,60 @@ export default async function generateExampleApp({ reactNativeVersion?: string; }) { const directory = path.join(dest, 'example'); - const args = - type === 'native' - ? // `npx --package react-native-test-app@latest init --name ${projectName}Example --destination example --version ${reactNativeVersion}` - [ - '--package', - `react-native-test-app@latest`, - 'init', - '--name', - `${projectName}Example`, - `--destination`, - directory, - ...(reactNativeVersion !== 'latest' - ? ['--version', reactNativeVersion] - : []), - '--platform', - 'ios', - '--platform', - 'android', - ] - : // `npx create-expo-app example --no-install --template blank` - [ - 'create-expo-app@latest', - directory, - '--no-install', - '--template', - 'blank', - ]; + + // `npx --package react-native-test-app@latest init --name ${projectName}Example --destination example --version ${reactNativeVersion}` + const testAppArgs = [ + '--package', + `react-native-test-app@latest`, + 'init', + '--name', + `${projectName}Example`, + `--destination`, + directory, + ...(reactNativeVersion !== 'latest' + ? ['--version', reactNativeVersion] + : []), + '--platform', + 'ios', + '--platform', + 'android', + ]; + + // `npx react-native init --directory example --skip-install` + const vanillaArgs = [ + 'react-native@latest', + 'init', + `${projectName}Example`, + '--directory', + directory, + '--version', + reactNativeVersion, + '--skip-install', + '--npm', + ]; + + // `npx create-expo-app example --no-install --template blank` + const expoArgs = [ + 'create-expo-app@latest', + directory, + '--no-install', + '--template', + 'blank', + ]; + + let args: string[] = []; + + switch (type) { + case ExampleType.Vanilla: + args = vanillaArgs; + break; + case ExampleType.TestApp: + args = testAppArgs; + break; + case ExampleType.Expo: + args = expoArgs; + break; + } await spawn('npx', args, { env: { ...process.env, npm_config_yes: 'true' }, @@ -121,7 +149,7 @@ export default async function generateExampleApp({ 'build:ios': `cd ios && xcodebuild -workspace ${projectName}Example.xcworkspace -scheme ${projectName}Example -configuration Debug -sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO`, }; - if (type === 'native') { + if (type !== ExampleType.Expo) { Object.assign(scripts, SCRIPTS_TO_ADD); } @@ -132,7 +160,7 @@ export default async function generateExampleApp({ Object.assign(devDependencies, PACKAGES_TO_ADD_DEV); - if (type === 'expo') { + if (type === ExampleType.Expo) { const sdkVersion = dependencies.expo.split('.')[0].replace(/[^\d]/, ''); let bundledNativeModules: Record; @@ -176,7 +204,7 @@ export default async function generateExampleApp({ spaces: 2, }); - if (type === 'native') { + if (type !== ExampleType.Expo) { let gradleProperties = await fs.readFile( path.join(directory, 'android', 'gradle.properties'), 'utf8'