fix(generate): add support for Expo Router app directory prompts
Description
- Issues: fixes #2903
This PR addresses a user who creates screens within an ignited app using expo-router. It checks for the existence of src/app (the expo router configuration) and then determines where to put the screen in 4 ways:
- An override provided in the CLI command
-
destinationDirset from the screen generator template - If neither of those exists, it prompts the user to provide a dir path, if none is provided or they decide to not create that directory, it defaults to
src/app/
Screenshots
| Scenario | Result |
|---|---|
| Using CLI Override for directory | |
| Using directory set in template front matter | |
| Setting via prompts - chooses to create dir | |
Setting via prompts - detects src/app and user just taps enter |
|
Setting via prompts - goes to default src/app |
Checklist
- [X]
README.mdand other relevant documentation has been updated with my changes - [X] I have manually tested this, including by generating a new app locally (see docs).
Sorry about not chiming in on the original issue!
I have some thoughts.
- I think the screen should be separated from the route.
npx ignite-cli g screen DetailScreen
The screen should always (IMO) be in src/app/screens/detail-screen.tsx (if it's an Expo Router app)
- We need a route generator
npx ignite-cli g route listings/detail DetailScreen
This creates a route file at app/listings/detail.tsx which imports the specified screen.
This would allow us to have multiple routes that can import the same screen.
It also eliminates this specialized generator logic which I think is going away from the spirit of the simple generators we want for Ignite. Ignite's generators got too complicated in earlier versions, which is why I rewrote them to be super simple in Ignite 6. The whole CLI was just over 900 lines of code at that point (vs 3k+ for previous versions), and I really don't want to continue to bloat it with additional logic if we can avoid that.
I made some updates to this to help progress it along.
Now, for expo-router projects, we restored the screen generator to keep it being placed in src/screens, matching react navigation ignite projects (aside from the root dir)
In addition to that, upon a new project it will generate a route generator template which you can use like so:
npx ignite-cli g route welcome --dir=src/app/some/route
resulting in
src/app/some/route/welcome.tsx and generates
import { WelcomeScreen } from "@/screens/WelcomeScreen"
export default function Welcome() {
return <WelcomeScreen />
}