-
Notifications
You must be signed in to change notification settings - Fork 215
chore: use react-native init for generating example app #271
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
Changes from 13 commits
9f1febf
a06282b
3d2482f
98f8e40
795d980
93223d1
25bb21b
8bd1521
e1aacb6
910769d
8d3e444
3e1548b
2f01afb
f7fb47d
950881a
6fd32f3
9e3a313
72097b4
5a23737
a7f8e6d
f086680
949ecb1
1b69ed0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import fs from 'fs'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think |
||
import spawn from 'cross-spawn'; | ||
import path from 'path'; | ||
|
||
const FILES_TO_DELETE = [ | ||
'.eslintrc.js', | ||
'tsconfig.json', | ||
'.gitignore', | ||
'.git', | ||
'.prettierrc.js', | ||
'index.js', | ||
'App.tsx', | ||
]; | ||
|
||
const PACKAGES_TO_REMOVE = [ | ||
'@react-native-community/eslint-config', | ||
'@tsconfig/react-native', | ||
'@types/jest', | ||
'@types/react-native', | ||
'@types/react-test-renderer', | ||
'@typescript-eslint/eslint-plugin', | ||
'@typescript-eslint/parser', | ||
'babel-jest', | ||
'eslint', | ||
'jest', | ||
'react-test-renderer', | ||
'typescript', | ||
]; | ||
|
||
const PACKAGES_TO_ADD = { | ||
'babel-plugin-module-resolver': '^4.1.0', | ||
'metro-react-native-babel-preset': '^0.72.1', | ||
'patch-package': '^6.4.7', | ||
'postinstall-postinstall': '^2.1.0', | ||
}; | ||
|
||
export default function generateRNApp({ | ||
dest, | ||
projectName, | ||
isTurboModule, | ||
}: { | ||
dest: string; | ||
projectName: string; | ||
isTurboModule: boolean; | ||
}) { | ||
// Generate the example app's base using `npx react-native init <projectName>Example --template react-native-template-typescript --directory example --skip-install --version <version>` | ||
const createRNAppProcess = spawn.sync( | ||
'npx', | ||
[ | ||
'react-native', | ||
'init', | ||
`${projectName}Example`, | ||
'--template', | ||
'react-native-template-typescript', | ||
atlj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'--directory', | ||
path.join(dest, 'example'), | ||
'--skip-install', | ||
], | ||
{ | ||
cwd: dest, | ||
} | ||
); | ||
if (createRNAppProcess.error) { | ||
throw createRNAppProcess.error; | ||
} | ||
|
||
// Remove unnecessary files | ||
FILES_TO_DELETE.forEach((file) => { | ||
try { | ||
fs.unlinkSync(path.join(dest, 'example', file)); | ||
} catch (e) { | ||
// ignore | ||
} | ||
}); | ||
|
||
// Patch the example app's package.json | ||
const examplePackageJson = JSON.parse( | ||
fs.readFileSync(path.join(dest, 'example', 'package.json'), 'utf8') | ||
); | ||
examplePackageJson.scripts = { | ||
...examplePackageJson.scripts, | ||
test: undefined, | ||
lint: undefined, | ||
|
||
pods: 'pod-install --quiet', | ||
postinstall: 'patch-package', | ||
}; | ||
PACKAGES_TO_REMOVE.forEach((pkg) => { | ||
examplePackageJson.devDependencies[pkg] = undefined; | ||
}); | ||
examplePackageJson.devDependencies = { | ||
...examplePackageJson.devDependencies, | ||
...PACKAGES_TO_ADD, | ||
}; | ||
examplePackageJson.jest = undefined; | ||
fs.writeFileSync( | ||
path.join(dest, 'example', 'package.json'), | ||
JSON.stringify(examplePackageJson, null, 2) | ||
); | ||
|
||
// If the library is a TurboModule, enable new arch for IOS and Android | ||
if (isTurboModule) { | ||
// Android | ||
// Change newArchEnabled=false to newArchEnabled=true in example/android/gradle.properties | ||
const gradleProperties = fs | ||
.readFileSync( | ||
path.join(dest, 'example', 'android', 'gradle.properties'), | ||
'utf8' | ||
) | ||
.replace('newArchEnabled=false', 'newArchEnabled=true'); | ||
fs.writeFileSync( | ||
path.join(dest, 'example', 'android', 'gradle.properties'), | ||
gradleProperties | ||
); | ||
|
||
// IOS | ||
// Add ENV['RCT_NEW_ARCH_ENABLED'] = 1 on top of example/ios/Podfile | ||
const podfile = fs.readFileSync( | ||
path.join(dest, 'example', 'ios', 'Podfile'), | ||
'utf8' | ||
); | ||
fs.writeFileSync( | ||
path.join(dest, 'example', 'ios', 'Podfile'), | ||
"ENV['RCT_NEW_ARCH_ENABLED'] = '1'\n" + podfile | ||
); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.