-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathgenerateExampleApp.ts
214 lines (183 loc) · 5.48 KB
/
generateExampleApp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import fs from 'fs-extra';
import path from 'path';
import https from 'https';
import { spawn } from './spawn';
const FILES_TO_DELETE = [
'__tests__',
'.buckconfig',
'.eslintrc.js',
'.flowconfig',
'.git',
'.gitignore',
'.prettierrc.js',
'App.js',
'App.tsx',
'index.js',
'tsconfig.json',
];
const PACKAGES_TO_REMOVE = [
'@react-native/eslint-config',
'@tsconfig/react-native',
'@types/jest',
'@types/react',
'@types/react-test-renderer',
'@typescript-eslint/eslint-plugin',
'@typescript-eslint/parser',
'babel-jest',
'eslint',
'jest',
'prettier',
'react-test-renderer',
'typescript',
];
const ADD_DUMMY_SWIFT_FILE = path.resolve(
__dirname,
'../../src/add-dummy-swift-file.rb'
);
const PACKAGES_TO_ADD_DEV = {
'babel-plugin-module-resolver': '^5.0.0',
};
const PACKAGES_TO_ADD_WEB = {
'react-dom': '18.2.0',
'react-native-web': '~0.18.10',
};
const PACKAGES_TO_ADD_WEB_DEV = {
'@expo/webpack-config': '^18.0.1',
'babel-loader': '^8.1.0',
};
export default async function generateExampleApp({
type,
dest,
slug,
projectName,
arch,
reactNativeVersion = 'latest',
swift = false,
}: {
type: 'expo' | 'native';
dest: string;
slug: string;
projectName: string;
arch: 'new' | 'mixed' | 'legacy';
reactNativeVersion?: string;
swift?: boolean;
}) {
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`
['create-expo-app@latest', directory, '--no-install'];
await spawn('npx', args, {
cwd: dest,
env: { ...process.env, npm_config_yes: 'true' },
});
if (swift && process.platform === 'darwin') {
await spawn(
'ruby',
[
ADD_DUMMY_SWIFT_FILE,
path.join(directory, 'ios', `${projectName}Example.xcodeproj`),
`${projectName}Example`,
],
{}
);
}
// Remove unnecessary files and folders
for (const file of FILES_TO_DELETE) {
await fs.remove(path.join(directory, file));
}
// Patch the example app's package.json
const pkg = JSON.parse(
await fs.readFile(path.join(directory, 'package.json'), 'utf8')
);
pkg.name = `${slug}-example`;
// Remove Jest config for now
delete pkg.jest;
const { scripts, dependencies, devDependencies } = pkg;
delete scripts.test;
delete scripts.lint;
const SCRIPTS_TO_ADD = {
'build:android':
'cd android && ./gradlew assembleDebug --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a',
'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') {
Object.assign(scripts, SCRIPTS_TO_ADD);
}
PACKAGES_TO_REMOVE.forEach((name) => {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete devDependencies[name];
});
Object.assign(devDependencies, PACKAGES_TO_ADD_DEV);
if (type === 'expo') {
const sdkVersion = dependencies.expo.split('.')[0].replace(/[^\d]/, '');
let bundledNativeModules: Record<string, string>;
try {
bundledNativeModules = await new Promise((resolve, reject) => {
https
.get(
`https://raw.githubusercontent.com/expo/expo/sdk-${sdkVersion}/packages/expo/bundledNativeModules.json`,
(res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(e);
}
});
}
)
.on('error', reject);
});
} catch (e) {
bundledNativeModules = {};
}
Object.entries(PACKAGES_TO_ADD_WEB).forEach(([name, version]) => {
dependencies[name] = bundledNativeModules[name] || version;
});
Object.entries(PACKAGES_TO_ADD_WEB_DEV).forEach(([name, version]) => {
devDependencies[name] = bundledNativeModules[name] || version;
});
scripts.web = 'expo start --web';
}
await fs.writeJSON(path.join(directory, 'package.json'), pkg, {
spaces: 2,
});
// If the library is on new architecture, enable new arch for iOS and Android
if (arch === 'new') {
// Android
// Change newArchEnabled=false to newArchEnabled=true in example/android/gradle.properties
const gradleProperties = await fs.readFile(
path.join(directory, 'android', 'gradle.properties'),
'utf8'
);
await fs.writeFile(
path.join(directory, 'android', 'gradle.properties'),
gradleProperties.replace('newArchEnabled=false', 'newArchEnabled=true')
);
// iOS
// Add ENV['RCT_NEW_ARCH_ENABLED'] = 1 on top of example/ios/Podfile
const podfile = await fs.readFile(
path.join(directory, 'ios', 'Podfile'),
'utf8'
);
await fs.writeFile(
path.join(directory, 'ios', 'Podfile'),
"ENV['RCT_NEW_ARCH_ENABLED'] = '1'\n\n" + podfile
);
}
}