-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRFileStorage.mm
More file actions
64 lines (51 loc) · 1.75 KB
/
Copy pathIRFileStorage.mm
File metadata and controls
64 lines (51 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// IRFileStorage.mm
// Reactotron-macOS
//
#import <Foundation/Foundation.h>
#import <AppSpec/AppSpec.h>
@interface IRFileStorage : NativeIRFileStorageSpecBase <NativeIRFileStorageSpec>
@end
@implementation IRFileStorage
RCT_EXPORT_MODULE()
static BOOL ensureDirectoryExists(NSString *path) {
if (path.length == 0) return NO;
BOOL isDir = NO;
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:path isDirectory:&isDir]) {
return isDir;
}
NSError *error = nil;
BOOL ok = [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
return ok && error == nil;
}
// Spec methods (sync)
- (NSString *)read:(NSString *)path {
if (path == nil || path.length == 0) return @"";
NSData *data = [NSData dataWithContentsOfFile:path];
if (data == nil) return @"";
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return str ?: @"";
}
- (void)write:(NSString *)path data:(NSString *)data {
if (path == nil || data == nil) return;
NSString *dir = [path stringByDeletingLastPathComponent];
ensureDirectoryExists(dir);
NSError *err = nil;
[data writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&err];
}
- (void)remove:(NSString *)path {
if (path == nil) return;
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:path]) return;
NSError *err = nil;
[fm removeItemAtPath:path error:&err];
}
- (void)ensureDir:(NSString *)path {
if (path == nil) return;
ensureDirectoryExists(path);
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params {
return std::make_shared<facebook::react::NativeIRFileStorageSpecJSI>(params);
}
@end