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

fix(cli): Expo Go compat deps #2508

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions boilerplate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@
},
"dependencies": {
"@expo-google-fonts/space-grotesk": "^0.2.2",
"@react-native-async-storage/async-storage": "1.18.2",
"@expo/metro-config": "^0.7.1",
"@expo/webpack-config": "18.1.2",
"@expo/metro-config": "~0.10.0",
"@expo/webpack-config": "^19.0.0",
"@react-native-async-storage/async-storage": "1.19.3",
"@react-navigation/bottom-tabs": "^6.3.2",
"@react-navigation/native": "^6.0.2",
"@react-navigation/native-stack": "^6.0.2",
"apisauce": "3.0.1",
"date-fns": "^2.30.0",
"expo": "^49.0.8",
"expo-application": "~5.3.0",
"expo": "^49.0.13",
"expo-application": "~5.4.0",
"expo-build-properties": "~0.8.3",
"expo-device": "~5.4.0",
"expo-file-system": "~15.4.2",
"expo-font": "~11.4.0",
"expo-device": "~5.6.0",
"expo-file-system": "~15.6.0",
"expo-font": "~11.6.0",
"expo-linking": "~5.0.2",
"expo-localization": "~14.3.0",
"expo-localization": "~14.5.0",
"expo-splash-screen": "~0.20.4",
"expo-status-bar": "~1.6.0",
"i18n-js": "3.9.2",
Expand Down Expand Up @@ -100,7 +100,7 @@
"regenerator-runtime": "^0.13.4",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.3"
"typescript": "^5.2.2"
},
"expo": {
"install": {
Expand Down
14 changes: 12 additions & 2 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EOL } from "os"
import { GluegunToolbox } from "../types"
import {
isAndroidInstalled,
Expand All @@ -24,7 +25,10 @@ import {
import type { ValidationsExports } from "../tools/validations"
import { boolFlag } from "../tools/flag"
import { cache } from "../tools/cache"
import { EOL } from "os"
import {
expoGoCompatExpectedVersions,
findAndUpdateDependencyVersions,
} from "../tools/expoGoCompatibility"

type Workflow = "expo" | "prebuild" | "manual"

Expand Down Expand Up @@ -525,6 +529,12 @@ module.exports = {
packageJsonRaw = packageJsonRaw
.replace(/start --android/g, "run:android")
.replace(/start --ios/g, "run:ios")
} else {
// Expo Go workflow, swap back to compatible Expo Go versions of modules
log("Changing some dependencies for Expo Go compatibility...")
log(JSON.stringify(expoGoCompatExpectedVersions))

packageJsonRaw = findAndUpdateDependencyVersions(packageJsonRaw, expoGoCompatExpectedVersions)
}

// - Then write it back out.
Expand All @@ -534,7 +544,7 @@ module.exports = {
// #endregion

// #region Run Packager Install
// pnpm/yarn/npm install it
// pnpm/yarn/npm/bun install it

// check if there is a dependency cache using a hash of the package.json
const boilerplatePackageJsonHash = cache.hash(read(path(boilerplatePath, "package.json")))
Expand Down
31 changes: 31 additions & 0 deletions src/tools/expoGoCompatibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// The expoGoCompat object specifies version overrides for packages that are not
// compatible with the Expo Go app. We use this object to specify the latest compatible
// version of each package when generating a new project using Expo Go.
// New ignited apps using prebuild will have more up-to-date versions of these packages.
export const expoGoCompatExpectedVersions = {
"@react-native-async-storage/async-storage": "1.18.2",
"expo-application": "~5.3.0",
"expo-device": "~5.4.0",
"expo-file-system": "~15.4.4",
"expo-font": "~11.4.0",
"expo-localization": "~14.3.0",
}

// This function takes a package.json file as a string and updates the versions of the
// dependencies specified in expoGoCompatExpectedVersions to the values in that object
// and returns the updated package.json as a string.
// This function is used when generating a new project using Expo Go.
export function findAndUpdateDependencyVersions(
packageJsonRaw: string,
dependencies: Record<string, string>,
): string {
let updatedPackageJson = packageJsonRaw

Object.keys(dependencies).forEach((depName) => {
const desiredVersion = dependencies[depName]
const regex = new RegExp(`"${depName}"\\s*:\\s*"[^"]+"`, "g")
updatedPackageJson = updatedPackageJson.replace(regex, `"${depName}": "${desiredVersion}"`)
})

return updatedPackageJson
}
17 changes: 17 additions & 0 deletions test/vanilla/ignite-new.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { filesystem } from "gluegun"
import * as tempy from "tempy"
import { runIgnite, runError, run } from "../_test-helpers"
import { expoGoCompatExpectedVersions } from "../../src/tools/expoGoCompatibility"

const APP_NAME = "Foo"
const originalDir = process.cwd()
Expand Down Expand Up @@ -79,6 +80,14 @@ describe("ignite new", () => {
expect(igniteJSON.scripts.ios).toBe("npx expo start --ios")
})

it("should have modified the package.json to have versions that work with expo go", () => {
const igniteJSON = filesystem.read(`${appPath}/package.json`, "json")
expect(igniteJSON).toHaveProperty("dependencies")
Object.keys(expoGoCompatExpectedVersions).forEach((key) => {
expect(igniteJSON.dependencies[key]).toBe(expoGoCompatExpectedVersions[key])
})
})

it("should have created app.tsx with default export and RootStore", () => {
const appJS = filesystem.read(`${appPath}/app/app.tsx`)
expect(appJS).toContain("export default App")
Expand Down Expand Up @@ -316,6 +325,14 @@ describe("ignite new", () => {
expect(igniteJSON.scripts.android).toBe("npx expo run:android")
expect(igniteJSON.scripts.ios).toBe("npx expo run:ios")
})

it("should NOT have modified the package.json to have versions that work with expo go", () => {
const igniteJSON = filesystem.read(`${appPath}/package.json`, "json")
expect(igniteJSON).toHaveProperty("dependencies")
Object.keys(expoGoCompatExpectedVersions).forEach((key) => {
expect(igniteJSON.dependencies[key]).not.toBe(expoGoCompatExpectedVersions[key])
})
})
})
})

Expand Down
Loading