Skip to content

Commit 908440a

Browse files
committed
fix: refactor Expo FileSystem access to use importLegacyExpoFSModules for Expo 53
Refs: software-mansion#699 (comment)
1 parent 8583112 commit 908440a

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

apps/speech-to-text/screens/SpeechToTextScreen.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ import {
1010
Platform,
1111
} from 'react-native';
1212
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
13-
import { useSpeechToText, WHISPER_TINY_EN } from 'react-native-executorch';
13+
import {
14+
importLegacyExpoFSModules,
15+
useSpeechToText,
16+
WHISPER_TINY_EN,
17+
} from 'react-native-executorch';
1418
import FontAwesome from '@expo/vector-icons/FontAwesome';
1519
import {
1620
AudioManager,
1721
AudioRecorder,
1822
AudioContext,
1923
} from 'react-native-audio-api';
20-
import * as FileSystem from 'expo-file-system/legacy';
2124
import SWMIcon from '../assets/swm_icon.svg';
2225
import DeviceInfo from 'react-native-device-info';
2326

27+
const { cacheDirectory, downloadAsync } = importLegacyExpoFSModules();
28+
2429
const isSimulator = DeviceInfo.isEmulatorSync();
2530

2631
export const SpeechToTextScreen = () => {
@@ -56,9 +61,9 @@ export const SpeechToTextScreen = () => {
5661
return;
5762
}
5863

59-
const { uri } = await FileSystem.downloadAsync(
64+
const { uri } = await downloadAsync(
6065
audioURL,
61-
FileSystem.cacheDirectory + 'audio_file'
66+
cacheDirectory + 'audio_file'
6267
);
6368

6469
const audioContext = new AudioContext({ sampleRate: 16000 });
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/legacy';
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,5 +1,8 @@
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';
@@ -13,7 +16,7 @@ import {
1316
} from '../types/llm';
1417
import { parseToolCall } from '../utils/llm';
1518
import { Logger } from '../common/Logger';
16-
import { readAsStringAsync } from 'expo-file-system/legacy';
19+
const { readAsStringAsync } = importLegacyExpoFSModules();
1720

1821
export class LLMController {
1922
private nativeModule: any;

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +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 type * as FileSystemTypes from 'expo-file-system';
3031

31-
import {
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 {
3251
cacheDirectory,
3352
copyAsync,
3453
createDownloadResumable,
@@ -38,7 +57,8 @@ import {
3857
EncodingType,
3958
deleteAsync,
4059
readDirectoryAsync,
41-
} from 'expo-file-system/legacy';
60+
} = importLegacyExpoFSModules();
61+
4262
import { Asset } from 'expo-asset';
4363
import { Platform } from 'react-native';
4464
import { RNEDirectory } from '../constants/directories';

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
import type * as FileSystemTypes from 'expo-file-system';
12
import { RNEDirectory } from '../constants/directories';
23
import { ResourceSource } from '../types/common';
34
import { Asset } from 'expo-asset';
45
import { Logger } from '../common/Logger';
6+
import { importLegacyExpoFSModules } from './ResourceFetcher';
57

68
/**
79
* @internal
810
*/
9-
import {
10-
getInfoAsync,
11-
makeDirectoryAsync,
12-
type DownloadResumable,
13-
} from 'expo-file-system/legacy';
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
}

0 commit comments

Comments
 (0)