-
Notifications
You must be signed in to change notification settings - Fork 212
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
Merged
satya164
merged 23 commits into
callstack:main
from
atlj:@atlj/integrate-react-native-init
Sep 30, 2022
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9f1febf
feat: generate example app via `react-native init`
atlj a06282b
chore(example template): remove android files
atlj 3d2482f
chore(example template): remove unused root files
atlj 98f8e40
chore(example template): remove ios files
atlj 795d980
chore(example template): remove android buildscripts
atlj 93223d1
refactor: move rn example app generator to utils
atlj 25bb21b
chore(example template): rename index.tsx to index.ts
atlj 8bd1521
feat: enable new architecture on turbo module libraries
atlj e1aacb6
refactor: rename app exampleAppGenerators to generateRNApp
atlj 910769d
refactor: don't specify RN version for example apps
atlj 8d3e444
refactor(example generator): extract files to delete to constant
atlj 3e1548b
refactor(rn example generator): extract packages to add and remove
atlj 2f01afb
fix: native example code is generated on expo projects
atlj f7fb47d
refactor(example generator): change isTurbomodule flag as isNewArch
atlj 950881a
docs(example generator): remove version param from init command
atlj 6fd32f3
feat: set root package's react and react native versions from example…
atlj 9e3a313
chore: add a patch for react-native 0.70.0 for codegen on ios
atlj 72097b4
fix(example template): tests cant import `App.tsx`
atlj 5a23737
fix(example generator): react-test-renderer and react-native types ar…
atlj a7f8e6d
fix: remove unnecessary parts from patch file
atlj f086680
feat: use react-native init without any templates
atlj 949ecb1
feat: show spinner while generating example app
atlj 1b69ed0
Merge branch 'main' into @atlj/integrate-react-native-init
satya164 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
packages/create-react-native-library/src/utils/generateRNApp.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import fs from 'fs'; | ||
import spawn from 'cross-spawn'; | ||
import path from 'path'; | ||
|
||
const FILES_TO_DELETE = [ | ||
'.eslintrc.js', | ||
'.gitignore', | ||
'.prettierrc.js', | ||
'App.js', | ||
'index.js', | ||
'.flowconfig', | ||
'.buckconfig', | ||
]; | ||
|
||
const FOLDERS_TO_DELETE = ['__tests__']; | ||
|
||
const PACKAGES_TO_REMOVE = [ | ||
'@react-native-community/eslint-config', | ||
'babel-jest', | ||
'eslint', | ||
'jest', | ||
'react-test-renderer', | ||
]; | ||
|
||
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 async function generateRNApp({ | ||
dest, | ||
projectName, | ||
isNewArch, | ||
}: { | ||
dest: string; | ||
projectName: string; | ||
isNewArch: boolean; | ||
}) { | ||
// Generate the example app's base using `npx react-native init <projectName>Example --directory example --skip-install` | ||
const createRNAppProcess = spawn( | ||
'npx', | ||
[ | ||
'react-native', | ||
'init', | ||
`${projectName}Example`, | ||
'--directory', | ||
path.join(dest, 'example'), | ||
'--skip-install', | ||
], | ||
{ | ||
cwd: dest, | ||
} | ||
); | ||
await new Promise((resolve, reject) => { | ||
createRNAppProcess.once('error', reject); | ||
createRNAppProcess.once('close', resolve); | ||
}); | ||
|
||
// Remove unnecessary files | ||
FILES_TO_DELETE.forEach((file) => { | ||
try { | ||
fs.unlinkSync(path.join(dest, 'example', file)); | ||
} catch (e) { | ||
//ignore | ||
} | ||
}); | ||
|
||
// Remove unnecessary folders | ||
FOLDERS_TO_DELETE.forEach((folder) => { | ||
try { | ||
fs.rmSync(path.join(dest, 'example', folder), { | ||
recursive: true, | ||
}); | ||
} 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 on new architecture, enable new arch for IOS and Android | ||
if (isNewArch) { | ||
// 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 | ||
); | ||
} | ||
} |
49 changes: 0 additions & 49 deletions
49
...te-react-native-library/templates/example-turbo/example/patches/react-native+0.68.2.patch
This file was deleted.
Oops, something went wrong.
134 changes: 134 additions & 0 deletions
134
...te-react-native-library/templates/example-turbo/example/patches/react-native+0.70.0.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
diff --git a/node_modules/react-native/scripts/codegen/generate-artifacts-executor.js b/node_modules/react-native/scripts/codegen/generate-artifacts-executor.js | ||
index 458495b..6fc398a 100644 | ||
--- a/node_modules/react-native/scripts/codegen/generate-artifacts-executor.js | ||
+++ b/node_modules/react-native/scripts/codegen/generate-artifacts-executor.js | ||
@@ -204,6 +204,55 @@ function handleThirdPartyLibraries( | ||
}); | ||
} | ||
|
||
+function handleLibrariesFromReactNativeConfig( | ||
+ libraries, | ||
+ codegenConfigKey, | ||
+ codegenConfigFilename, | ||
+ appRootDir, | ||
+) { | ||
+ const rnConfigFileName = 'react-native.config.js'; | ||
+ | ||
+ console.log( | ||
+ `\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in ${rnConfigFileName}`, | ||
+ ); | ||
+ | ||
+ const rnConfigFilePath = path.join(appRootDir, rnConfigFileName); | ||
+ | ||
+ if (fs.existsSync(rnConfigFilePath)) { | ||
+ const rnConfig = require(rnConfigFilePath); | ||
+ | ||
+ if (rnConfig.dependencies != null) { | ||
+ Object.keys(rnConfig.dependencies).forEach(name => { | ||
+ const dependencyConfig = rnConfig.dependencies[name]; | ||
+ | ||
+ if (dependencyConfig.root) { | ||
+ const codegenConfigFileDir = path.resolve( | ||
+ appRootDir, | ||
+ dependencyConfig.root, | ||
+ ); | ||
+ const configFilePath = path.join( | ||
+ codegenConfigFileDir, | ||
+ codegenConfigFilename, | ||
+ ); | ||
+ const pkgJsonPath = path.join(codegenConfigFileDir, 'package.json'); | ||
+ | ||
+ if (fs.existsSync(configFilePath)) { | ||
+ const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath)); | ||
+ const configFile = JSON.parse(fs.readFileSync(configFilePath)); | ||
+ extractLibrariesFromJSON( | ||
+ configFile, | ||
+ libraries, | ||
+ codegenConfigKey, | ||
+ pkgJson.name, | ||
+ codegenConfigFileDir, | ||
+ ); | ||
+ } | ||
+ } | ||
+ }); | ||
+ } | ||
+ } | ||
+} | ||
+ | ||
function handleInAppLibraries( | ||
libraries, | ||
pkgJson, | ||
@@ -362,6 +411,39 @@ function createComponentProvider( | ||
} | ||
} | ||
|
||
+function findCodegenEnabledLibraries( | ||
+ appRootDir, | ||
+ baseCodegenConfigFileDir, | ||
+ codegenConfigFilename, | ||
+ codegenConfigKey, | ||
+) { | ||
+ const pkgJson = readPackageJSON(appRootDir); | ||
+ const dependencies = {...pkgJson.dependencies, ...pkgJson.devDependencies}; | ||
+ const libraries = []; | ||
+ | ||
+ handleReactNativeCodeLibraries( | ||
+ libraries, | ||
+ codegenConfigFilename, | ||
+ codegenConfigKey, | ||
+ ); | ||
+ handleThirdPartyLibraries( | ||
+ libraries, | ||
+ baseCodegenConfigFileDir, | ||
+ dependencies, | ||
+ codegenConfigFilename, | ||
+ codegenConfigKey, | ||
+ ); | ||
+ handleLibrariesFromReactNativeConfig( | ||
+ libraries, | ||
+ codegenConfigKey, | ||
+ codegenConfigFilename, | ||
+ appRootDir, | ||
+ ); | ||
+ handleInAppLibraries(libraries, pkgJson, codegenConfigKey, appRootDir); | ||
+ | ||
+ return libraries; | ||
+} | ||
+ | ||
// It removes all the empty files and empty folders | ||
// it finds, starting from `filepath`, recursively. | ||
// | ||
@@ -429,23 +511,12 @@ function execute( | ||
} | ||
|
||
try { | ||
- const pkgJson = readPackageJSON(appRootDir); | ||
- const dependencies = {...pkgJson.dependencies, ...pkgJson.devDependencies}; | ||
- const libraries = []; | ||
- | ||
- handleReactNativeCodeLibraries( | ||
- libraries, | ||
- codegenConfigFilename, | ||
- codegenConfigKey, | ||
- ); | ||
- handleThirdPartyLibraries( | ||
- libraries, | ||
+ const libraries = findCodegenEnabledLibraries( | ||
+ appRootDir, | ||
baseCodegenConfigFileDir, | ||
- dependencies, | ||
codegenConfigFilename, | ||
codegenConfigKey, | ||
); | ||
- handleInAppLibraries(libraries, pkgJson, codegenConfigKey, appRootDir); | ||
|
||
if (libraries.length === 0) { | ||
console.log('[Codegen] No codegen-enabled libraries found.'); | ||
@@ -482,6 +553,7 @@ module.exports = { | ||
execute: execute, | ||
// exported for testing purposes only: | ||
_extractLibrariesFromJSON: extractLibrariesFromJSON, | ||
+ _findCodegenEnabledLibraries: findCodegenEnabledLibraries, | ||
_executeNodeScript: executeNodeScript, | ||
_generateCode: generateCode, | ||
_cleanupEmptyFilesAndFolders: cleanupEmptyFilesAndFolders, |
2 changes: 0 additions & 2 deletions
2
packages/create-react-native-library/templates/example/example/$.bundle/config
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/create-react-native-library/templates/example/example/$.ruby-version
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can call it
generateExampleApp
.WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was like this then got changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exampleAppGenerators
is different 😅I think
generateReactNativeExample
or something like that is good, we'd also needgenerateExpoExample