forked from leethree/redux-persist-fs-storage
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Windows Compatibility Preparation #2
Open
creambyemute
wants to merge
8
commits into
ConnectedHomes:master
Choose a base branch
from
wwimmo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5805f9f
Update RN-FS to point to our repo for RN-Windows support
0fc6aff
Add OSPathSeparator and bump version
3a42383
Add logs to debug windows temporarily, bump version
c706ddf
Make redux-persist-fs-storage RN-Windows compatible
1a8a0f5
Update RN-FS Dependency
ebb3420
Update README.md
creambyemute dc87aeb
Update rn-fs dep
38f72a5
Update RNFS commit reference
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,37 @@ | ||
/* @flow */ | ||
import fs from 'react-native-fs'; | ||
import fs from "react-native-fs"; | ||
import { Platform } from "react-native"; | ||
|
||
export const DocumentDir = fs.DocumentDirectoryPath; | ||
export const CacheDir = fs.CachesDirectoryPath; | ||
const OSPathSeparator = Platform.select({ | ||
ios: "/", | ||
android: "/", | ||
windows: "\\", | ||
}); | ||
|
||
const resolvePath = (...paths: Array<string>) => | ||
'/' + | ||
paths | ||
.join('/') | ||
.split('/') | ||
.filter(part => part && part !== '.') | ||
.join('/'); | ||
const resolvePath = (...paths: Array<string>) => { | ||
if (Platform.OS === "windows") { | ||
return paths | ||
.join(OSPathSeparator) | ||
.split(OSPathSeparator) | ||
.filter((part) => part && part !== ".") | ||
.join(OSPathSeparator); | ||
} | ||
Comment on lines
+13
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you're able to use |
||
return ( | ||
OSPathSeparator + | ||
paths | ||
.join(OSPathSeparator) | ||
.split(OSPathSeparator) | ||
.filter((part) => part && part !== ".") | ||
.join(OSPathSeparator) | ||
); | ||
}; | ||
|
||
// Wrap function to support both Promise and callback | ||
async function withCallback<R>( | ||
callback?: ?(error: ?Error, result: R | void) => void, | ||
func: () => Promise<R>, | ||
func: () => Promise<R> | ||
): Promise<R | void> { | ||
try { | ||
const result = await func(); | ||
|
@@ -34,40 +50,42 @@ async function withCallback<R>( | |
|
||
const FSStorage = ( | ||
location?: string = DocumentDir, | ||
folder?: string = 'reduxPersist', | ||
excludeFromBackup?: boolean = true, | ||
folder?: string = "reduxPersist", | ||
excludeFromBackup?: boolean = true | ||
) => { | ||
const baseFolder = resolvePath(location, folder); | ||
|
||
const pathForKey = (key: string) => | ||
resolvePath(baseFolder, key.replace(/[;\\/:*?\"<>|&']/gi,'_')); | ||
resolvePath(baseFolder, key.replace(/[;\\/:*?\"<>|&']/gi, "_")); | ||
|
||
const setItem = ( | ||
key: string, | ||
value: string, | ||
callback?: ?(error: ?Error) => void, | ||
callback?: ?(error: ?Error) => void | ||
): Promise<void> => | ||
withCallback(callback, async () => { | ||
await fs.mkdir(baseFolder, { | ||
NSURLIsExcludedFromBackupKey: excludeFromBackup, | ||
}); | ||
await fs.writeFile(pathForKey(key), value, 'utf8'); | ||
const baseFolderExists = await fs.exists(baseFolder); | ||
if (!baseFolderExists) { | ||
await fs.mkdir(baseFolder, { | ||
NSURLIsExcludedFromBackupKey: excludeFromBackup, | ||
}); | ||
} | ||
await fs.writeFile(pathForKey(key), value, "utf8"); | ||
}); | ||
|
||
const getItem = ( | ||
key: string, | ||
callback?: ?(error: ?Error, result: ?string) => void, | ||
callback?: ?(error: ?Error, result: ?string) => void | ||
): Promise<?string> => | ||
withCallback(callback, async () => { | ||
if (await fs.exists(pathForKey(key))) { | ||
const data = await fs.readFile(pathForKey(key), 'utf8'); | ||
const data = await fs.readFile(pathForKey(key), "utf8"); | ||
return data; | ||
} | ||
}); | ||
|
||
const removeItem = ( | ||
key: string, | ||
callback?: ?(error: ?Error) => void, | ||
callback?: ?(error: ?Error) => void | ||
): Promise<void> => | ||
withCallback(callback, async () => { | ||
if (await fs.exists(pathForKey(key))) { | ||
|
@@ -76,16 +94,19 @@ const FSStorage = ( | |
}); | ||
|
||
const getAllKeys = ( | ||
callback?: ?(error: ?Error, keys: ?Array<string>) => void, | ||
callback?: ?(error: ?Error, keys: ?Array<string>) => void | ||
) => | ||
withCallback(callback, async () => { | ||
await fs.mkdir(baseFolder, { | ||
NSURLIsExcludedFromBackupKey: excludeFromBackup, | ||
}); | ||
const baseFolderExists = await fs.exists(baseFolder); | ||
if (!baseFolderExists) { | ||
await fs.mkdir(baseFolder, { | ||
NSURLIsExcludedFromBackupKey: excludeFromBackup, | ||
}); | ||
} | ||
const files = await fs.readDir(baseFolder); | ||
const fileNames = files | ||
.filter(file => file.isFile()) | ||
.map(file => decodeURIComponent(file.name)); | ||
.filter((file) => file.isFile()) | ||
.map((file) => decodeURIComponent(file.name)); | ||
return fileNames; | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@connected-home/redux-persist-fs-storage", | ||
"version": "2.1.0", | ||
"version": "2.1.5", | ||
"author": "Peter Bevan<[email protected]>", | ||
"description": "Redux-Persist storage engine for React Native file system", | ||
"repository": "https://github.com/ConnectedHomes/redux-persist-fs-storage.git", | ||
|
@@ -15,7 +15,7 @@ | |
"AsyncStorage" | ||
], | ||
"dependencies": { | ||
"react-native-fs": "2.16" | ||
"react-native-fs": "https://github.com/wwimmo/react-native-fs.git" | ||
}, | ||
"peerDependencies": { | ||
"react-native": ">=0.61.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to use
path
? if so you can just usepath.sep
if you need access to the separatorThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so as it's React-Native JS world and not node.js?