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

Windows Compatibility Preparation #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Redux Persist FS Storage

## Forked for RN-Windows support, read the instructions how to install it

> [Redux Persist](https://github.com/rt2zz/redux-persist/) storage engine for React Native file system

Inspired by [redux-persist-filesystem-storage](https://github.com/robwalkerco/redux-persist-filesystem-storage), this module works as adapter between [react-native-fs](https://github.com/itinance/react-native-fs) and [redux-persist](https://github.com/rt2zz/redux-persist/).
Expand All @@ -10,12 +12,14 @@ Inspired by [redux-persist-filesystem-storage](https://github.com/robwalkerco/re
yarn add react-native-fs redux-persist-fs-storage
```

This will install `react-native-fs` as dependency. So make sure to link it natively:
This will install `react-native-fs` as dependency. So make sure to link it natively (if you're below RN 0.60):

```
react-native link react-native-fs
```

After that, replace the react-native-fs and redux-persist-fs-storage versions with the `wwimmo/react-native-fs` and `wwimmo/redux-persist-fs-storage` .git links and npm install/yarn again.

See `react-native-fs`'s [documentation](https://github.com/itinance/react-native-fs) for details.

### Usage
Expand Down
75 changes: 48 additions & 27 deletions index.js
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: "\\",
});
Comment on lines +7 to +11
Copy link

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 use path.sep if you need access to the separator

Copy link
Author

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?


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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're able to use path then I think this is the same as path.normalize with a trailing separator?

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();
Expand All @@ -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))) {
Expand All @@ -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;
});

Expand Down
4 changes: 2 additions & 2 deletions package.json
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",
Expand All @@ -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"
Expand Down
16 changes: 9 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
base-64@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/base-64/-/base-64-0.1.0.tgz#780a99c84e7d600260361511c4877613bf24f6bb"
integrity sha512-Y5gU45svrR5tI2Vt/X9GPd3L0HNIKzGu202EjxrXMpuc2V2CiKgemAbUUsqYmZJvPtCXoUKjNZwBJzsNScUbXA==

react-native-fs@2.12:
version "2.12.0"
resolved "https://registry.yarnpkg.com/react-native-fs/-/react-native-fs-2.12.0.tgz#77b7893d9f9a9318f577031a3447d1aace022020"
"react-native-fs@https://github.com/wwimmo/react-native-fs.git":
version "2.18.0"
resolved "https://github.com/wwimmo/react-native-fs.git#3d196ea45f53a1e4b74d34b74a1bb4f8cbae966a"
dependencies:
base-64 "^0.1.0"
utf8 "^2.1.1"
utf8 "^3.0.0"

utf8@^2.1.1:
version "2.1.2"
resolved "https://registry.yarnpkg.com/utf8/-/utf8-2.1.2.tgz#1fa0d9270e9be850d9b05027f63519bf46457d96"
utf8@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/utf8/-/utf8-3.0.0.tgz#f052eed1364d696e769ef058b183df88c87f69d1"
integrity sha512-E8VjFIQ/TyQgp+TZfS6l8yp/xWppSAHzidGiRrqe4bK4XP9pTRyKFgGJpO3SN7zdX4DeomTrwaseCHovfpFcqQ==