Skip to content

Commit 73a47a8

Browse files
authored
fix: Import Expo FS conditionally to work with Expo 54 (#699)
## Description Expo 54 introduces a new FileSystem API, deprecating the ones used in our codebase. The old APIs can still be accessed under `expo-file-system/legacy`. This is a temporary fix to work with old Expo versions. ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [x] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [ ] Other (chores, tests, code style improvements etc.) ### Tested on - [x] iOS - [x] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. -->
1 parent 2d1ac22 commit 73a47a8

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
import { documentDirectory } from 'expo-file-system';
1+
import { importLegacyExpoFSModules } from '../utils/ResourceFetcher';
2+
3+
const { documentDirectory } = importLegacyExpoFSModules();
24

35
export const RNEDirectory = `${documentDirectory}react-native-executorch/`;

packages/react-native-executorch/src/controllers/LLMController.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { ResourceSource } from '../types/common';
2-
import { ResourceFetcher } from '../utils/ResourceFetcher';
2+
import {
3+
importLegacyExpoFSModules,
4+
ResourceFetcher,
5+
} from '../utils/ResourceFetcher';
36
import { ETError, getError } from '../Error';
47
import { Template } from '@huggingface/jinja';
58
import { DEFAULT_CHAT_CONFIG } from '../constants/llmDefaults';
6-
import { readAsStringAsync } from 'expo-file-system';
79
import {
810
ChatConfig,
911
GenerationConfig,
@@ -14,6 +16,7 @@ import {
1416
} from '../types/llm';
1517
import { parseToolCall } from '../utils/llm';
1618
import { Logger } from '../common/Logger';
19+
const { readAsStringAsync } = importLegacyExpoFSModules();
1720

1821
export class LLMController {
1922
private nativeModule: any;

packages/react-native-executorch/src/utils/ResourceFetcher.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,27 @@
2727
* - Implements linked list behavior via the `.next` attribute
2828
* - Automatically processes subsequent downloads when `.next` contains a valid resource
2929
*/
30-
import {
30+
import type * as FileSystemTypes from 'expo-file-system';
31+
32+
export function importLegacyExpoFSModules() {
33+
let FileSystem: typeof FileSystemTypes;
34+
35+
try {
36+
const expoPkg = require('expo/package.json');
37+
const sdkVersion = expoPkg.version.split('.')[0];
38+
39+
if (Number(sdkVersion) > 53) {
40+
FileSystem = require('expo-file-system/legacy');
41+
} else {
42+
FileSystem = require('expo-file-system');
43+
}
44+
} catch (e) {
45+
throw new Error('Expo must be installed to use react-native-executorch');
46+
}
47+
return FileSystem;
48+
}
49+
50+
const {
3151
cacheDirectory,
3252
copyAsync,
3353
createDownloadResumable,
@@ -37,7 +57,8 @@ import {
3757
EncodingType,
3858
deleteAsync,
3959
readDirectoryAsync,
40-
} from 'expo-file-system';
60+
} = importLegacyExpoFSModules();
61+
4162
import { Asset } from 'expo-asset';
4263
import { Platform } from 'react-native';
4364
import { RNEDirectory } from '../constants/directories';

packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
/**
2-
* @internal
3-
*/
4-
5-
import {
6-
DownloadResumable,
7-
getInfoAsync,
8-
makeDirectoryAsync,
9-
} from 'expo-file-system';
1+
import type * as FileSystemTypes from 'expo-file-system';
102
import { RNEDirectory } from '../constants/directories';
113
import { ResourceSource } from '../types/common';
124
import { Asset } from 'expo-asset';
135
import { Logger } from '../common/Logger';
6+
import { importLegacyExpoFSModules } from './ResourceFetcher';
7+
8+
/**
9+
* @internal
10+
*/
11+
const { getInfoAsync, makeDirectoryAsync } = importLegacyExpoFSModules();
1412

1513
export const enum HTTP_CODE {
1614
OK = 200,
@@ -42,7 +40,7 @@ export interface ResourceSourceExtended {
4240
}
4341

4442
export interface DownloadResource {
45-
downloadResumable: DownloadResumable;
43+
downloadResumable: FileSystemTypes.DownloadResumable;
4644
status: DownloadStatus;
4745
extendedInfo: ResourceSourceExtended;
4846
}
@@ -75,7 +73,7 @@ export namespace ResourceFetcherUtils {
7573
let totalLength = 0;
7674
let previousFilesTotalLength = 0;
7775
for (const source of sources) {
78-
const type = await ResourceFetcherUtils.getType(source);
76+
const type = ResourceFetcherUtils.getType(source);
7977
let length = 0;
8078
try {
8179
if (type === SourceType.REMOTE_FILE && typeof source === 'string') {

0 commit comments

Comments
 (0)