Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add ability to use react-native-test-app for example/ app #572

Merged
merged 11 commits into from
Jul 3, 2024
23 changes: 13 additions & 10 deletions packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ type Answers = {
repoUrl: string;
languages: ProjectLanguages;
type?: ProjectType;
example?: boolean;
example?: ExampleType;
reactNativeVersion?: string;
withRecommendedOptions?: boolean;
};

export type ExampleType = 'vanilla' | 'test-app' | 'expo';

const LANGUAGE_CHOICES: {
title: string;
value: ProjectLanguages;
Expand Down Expand Up @@ -273,9 +275,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 +635,7 @@ async function create(argv: yargs.Arguments<any>) {
: 'legacy';

const example =
hasExample && !local ? (type === 'library' ? 'expo' : 'native') : 'none';
hasExample && !local ? (type === 'library' ? 'expo' : hasExample) : null;
szymonrybczak marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down Expand Up @@ -733,7 +736,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 +756,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 +765,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 +774,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 +797,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 type { ExampleType } from './../index';

const FILES_TO_DELETE = [
'__tests__',
Expand Down Expand Up @@ -55,39 +56,70 @@ 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 react-native init <projectName> --directory example --skip-install`
[
'react-native@latest',
'init',
`${projectName}Example`,
'--directory',
directory,
'--version',
reactNativeVersion,
'--skip-install',
'--npm',
]
: // `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 'vanilla':
args = vanillaArgs;
break;
case 'test-app':
args = testAppArgs;
break;
case 'expo':
args = expoArgs;
break;
}

await spawn('npx', args, {
cwd: dest,
env: { ...process.env, npm_config_yes: 'true' },
});

Expand Down Expand Up @@ -117,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 !== 'expo') {
Object.assign(scripts, SCRIPTS_TO_ADD);
}

Expand Down Expand Up @@ -172,7 +204,7 @@ export default async function generateExampleApp({
spaces: 2,
});

if (type === 'native') {
if (type !== 'expo') {
let gradleProperties = await fs.readFile(
path.join(directory, 'android', 'gradle.properties'),
'utf8'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const path = require('path');
const pak = require('../package.json');
const { configureProjects } = require('react-native-test-app');

module.exports = {
project: configureProjects({
android: {
sourceDir: 'android',
},
ios: {
sourceDir: 'ios',
},
}),
szymonrybczak marked this conversation as resolved.
Show resolved Hide resolved
dependencies: {
[pak.name]: {
root: path.join(__dirname, '..'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const path = require('path');
const pak = require('../package.json');
const { configureProjects } = require('react-native-test-app');

module.exports = {
project: configureProjects({
android: {
sourceDir: 'android',
},
ios: {
sourceDir: 'ios',
},
}),
szymonrybczak marked this conversation as resolved.
Show resolved Hide resolved
dependencies: {
[pak.name]: {
root: path.join(__dirname, '..'),
Expand Down
Loading