Skip to content

Commit c53fbb7

Browse files
committed
feat: allow providing example type via --example flag
1 parent c61bb71 commit c53fbb7

File tree

2 files changed

+81
-42
lines changed

2 files changed

+81
-42
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,17 @@ type Answers = {
139139
repoUrl: string;
140140
languages: ProjectLanguages;
141141
type?: ProjectType;
142-
example?: boolean;
142+
example?: ExampleType;
143143
reactNativeVersion?: string;
144144
withRecommendedOptions?: boolean;
145145
};
146146

147+
export enum ExampleType {
148+
Vanilla = 'vanilla',
149+
TestApp = 'test-app',
150+
Expo = 'expo',
151+
}
152+
147153
const LANGUAGE_CHOICES: {
148154
title: string;
149155
value: ProjectLanguages;
@@ -273,9 +279,10 @@ const args: Record<ArgName, yargs.Options> = {
273279
type: 'boolean',
274280
},
275281
'example': {
276-
description: 'Whether to create an example app',
277-
type: 'boolean',
278-
default: true,
282+
description: 'Type of the app to create',
283+
type: 'string',
284+
choices: ['vanilla', 'test-app'],
285+
default: 'vanilla',
279286
},
280287
'with-recommended-options': {
281288
description: `Whether to use the recommended template. ${RECOMMENDED_TEMPLATE.description}`,
@@ -632,7 +639,11 @@ async function create(argv: yargs.Arguments<any>) {
632639
: 'legacy';
633640

634641
const example =
635-
hasExample && !local ? (type === 'library' ? 'expo' : 'native') : 'none';
642+
hasExample && !local
643+
? type === 'library'
644+
? ExampleType.Expo
645+
: hasExample
646+
: null;
636647

637648
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
638649

@@ -718,7 +729,7 @@ async function create(argv: yargs.Arguments<any>) {
718729
await fs.mkdirp(folder);
719730

720731
if (reactNativeVersion != null) {
721-
if (example === 'expo') {
732+
if (example === ExampleType.Expo) {
722733
console.warn(
723734
`${kleur.yellow('⚠')} Ignoring --react-native-version for Expo example`
724735
);
@@ -733,7 +744,7 @@ async function create(argv: yargs.Arguments<any>) {
733744

734745
const spinner = ora().start();
735746

736-
if (example !== 'none') {
747+
if (example) {
737748
spinner.text = 'Generating example app';
738749

739750
await generateExampleApp({
@@ -753,7 +764,7 @@ async function create(argv: yargs.Arguments<any>) {
753764
} else {
754765
await copyDir(COMMON_FILES, folder);
755766

756-
if (example !== 'none') {
767+
if (example) {
757768
await copyDir(COMMON_EXAMPLE_FILES, folder);
758769
}
759770
}
@@ -762,7 +773,7 @@ async function create(argv: yargs.Arguments<any>) {
762773
await copyDir(JS_FILES, folder);
763774
await copyDir(EXPO_FILES, folder);
764775
} else {
765-
if (example !== 'none') {
776+
if (example) {
766777
await copyDir(
767778
path.join(EXAMPLE_FILES, 'example'),
768779
path.join(folder, 'example')
@@ -771,7 +782,7 @@ async function create(argv: yargs.Arguments<any>) {
771782

772783
await copyDir(NATIVE_COMMON_FILES, folder);
773784

774-
if (example !== 'none') {
785+
if (example) {
775786
await copyDir(NATIVE_COMMON_EXAMPLE_FILES, folder);
776787
}
777788

@@ -794,7 +805,7 @@ async function create(argv: yargs.Arguments<any>) {
794805
}
795806
}
796807

797-
if (example !== 'none') {
808+
if (example) {
798809
// Set `react` and `react-native` versions of root `package.json` from example `package.json`
799810
const examplePackageJson = await fs.readJSON(
800811
path.join(folder, 'example', 'package.json')

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

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs-extra';
22
import path from 'path';
33
import https from 'https';
44
import { spawn } from './spawn';
5+
import { ExampleType } from './../index';
56

67
const FILES_TO_DELETE = [
78
'__tests__',
@@ -55,41 +56,68 @@ export default async function generateExampleApp({
5556
arch,
5657
reactNativeVersion = 'latest',
5758
}: {
58-
type: 'expo' | 'native';
59+
type: ExampleType;
5960
dest: string;
6061
slug: string;
6162
projectName: string;
6263
arch: 'new' | 'mixed' | 'legacy';
6364
reactNativeVersion?: string;
6465
}) {
6566
const directory = path.join(dest, 'example');
66-
const args =
67-
type === 'native'
68-
? // `npx --package react-native-test-app@latest init --name ${projectName}Example --destination example --version ${reactNativeVersion}`
69-
[
70-
'--package',
71-
`react-native-test-app@latest`,
72-
'init',
73-
'--name',
74-
`${projectName}Example`,
75-
`--destination`,
76-
directory,
77-
...(reactNativeVersion !== 'latest'
78-
? ['--version', reactNativeVersion]
79-
: []),
80-
'--platform',
81-
'ios',
82-
'--platform',
83-
'android',
84-
]
85-
: // `npx create-expo-app example --no-install --template blank`
86-
[
87-
'create-expo-app@latest',
88-
directory,
89-
'--no-install',
90-
'--template',
91-
'blank',
92-
];
67+
68+
// `npx --package react-native-test-app@latest init --name ${projectName}Example --destination example --version ${reactNativeVersion}`
69+
const testAppArgs = [
70+
'--package',
71+
`react-native-test-app@latest`,
72+
'init',
73+
'--name',
74+
`${projectName}Example`,
75+
`--destination`,
76+
directory,
77+
...(reactNativeVersion !== 'latest'
78+
? ['--version', reactNativeVersion]
79+
: []),
80+
'--platform',
81+
'ios',
82+
'--platform',
83+
'android',
84+
];
85+
86+
// `npx react-native init <projectName> --directory example --skip-install`
87+
const vanillaArgs = [
88+
'react-native@latest',
89+
'init',
90+
`${projectName}Example`,
91+
'--directory',
92+
directory,
93+
'--version',
94+
reactNativeVersion,
95+
'--skip-install',
96+
'--npm',
97+
];
98+
99+
// `npx create-expo-app example --no-install --template blank`
100+
const expoArgs = [
101+
'create-expo-app@latest',
102+
directory,
103+
'--no-install',
104+
'--template',
105+
'blank',
106+
];
107+
108+
let args: string[] = [];
109+
110+
switch (type) {
111+
case ExampleType.Vanilla:
112+
args = vanillaArgs;
113+
break;
114+
case ExampleType.TestApp:
115+
args = testAppArgs;
116+
break;
117+
case ExampleType.Expo:
118+
args = expoArgs;
119+
break;
120+
}
93121

94122
await spawn('npx', args, {
95123
env: { ...process.env, npm_config_yes: 'true' },
@@ -121,7 +149,7 @@ export default async function generateExampleApp({
121149
'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`,
122150
};
123151

124-
if (type === 'native') {
152+
if (type !== ExampleType.Expo) {
125153
Object.assign(scripts, SCRIPTS_TO_ADD);
126154
}
127155

@@ -132,7 +160,7 @@ export default async function generateExampleApp({
132160

133161
Object.assign(devDependencies, PACKAGES_TO_ADD_DEV);
134162

135-
if (type === 'expo') {
163+
if (type === ExampleType.Expo) {
136164
const sdkVersion = dependencies.expo.split('.')[0].replace(/[^\d]/, '');
137165

138166
let bundledNativeModules: Record<string, string>;
@@ -176,7 +204,7 @@ export default async function generateExampleApp({
176204
spaces: 2,
177205
});
178206

179-
if (type === 'native') {
207+
if (type !== ExampleType.Expo) {
180208
let gradleProperties = await fs.readFile(
181209
path.join(directory, 'android', 'gradle.properties'),
182210
'utf8'

0 commit comments

Comments
 (0)