Skip to content

Commit

Permalink
feat: allow providing example type via --example flag
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Jun 27, 2024
1 parent c61bb71 commit c53fbb7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 42 deletions.
33 changes: 22 additions & 11 deletions packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -273,9 +279,10 @@ const args: Record<ArgName, yargs.Options> = {
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}`,
Expand Down Expand Up @@ -632,7 +639,11 @@ async function create(argv: yargs.Arguments<any>) {
: 'legacy';

const example =
hasExample && !local ? (type === 'library' ? 'expo' : 'native') : 'none';
hasExample && !local
? type === 'library'
? ExampleType.Expo
: hasExample
: null;

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

Expand Down Expand Up @@ -718,7 +729,7 @@ async function create(argv: yargs.Arguments<any>) {
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`
);
Expand All @@ -733,7 +744,7 @@ async function create(argv: yargs.Arguments<any>) {

const spinner = ora().start();

if (example !== 'none') {
if (example) {
spinner.text = 'Generating example app';

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

if (example !== 'none') {
if (example) {
await copyDir(COMMON_EXAMPLE_FILES, folder);
}
}
Expand All @@ -762,7 +773,7 @@ async function create(argv: yargs.Arguments<any>) {
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')
Expand All @@ -771,7 +782,7 @@ async function create(argv: yargs.Arguments<any>) {

await copyDir(NATIVE_COMMON_FILES, folder);

if (example !== 'none') {
if (example) {
await copyDir(NATIVE_COMMON_EXAMPLE_FILES, folder);
}

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

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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__',
Expand Down Expand Up @@ -55,41 +56,68 @@ export default async function generateExampleApp({
arch,
reactNativeVersion = 'latest',
}: {
type: 'expo' | 'native';
type: ExampleType;
dest: string;
slug: string;
projectName: string;
arch: 'new' | 'mixed' | 'legacy';
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 <projectName> --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' },
Expand Down Expand Up @@ -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);
}

Expand All @@ -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<string, string>;
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit c53fbb7

Please sign in to comment.