Skip to content

Commit 1821893

Browse files
committed
Initial commit
0 parents  commit 1821893

File tree

17 files changed

+853
-0
lines changed

17 files changed

+853
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
workbench

.jshintrc

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"-W093": true,
3+
"asi": false,
4+
"bitwise": true,
5+
"boss": false,
6+
"browser": false,
7+
"camelcase": true,
8+
"couch": false,
9+
"curly": true,
10+
"debug": false,
11+
"devel": true,
12+
"dojo": false,
13+
"eqeqeq": true,
14+
"eqnull": false,
15+
"esnext": true,
16+
"evil": false,
17+
"expr": true,
18+
"forin": false,
19+
"freeze": true,
20+
"funcscope": true,
21+
"gcl": false,
22+
"globalstrict": true,
23+
"immed": false,
24+
"indent": 2,
25+
"iterator": false,
26+
"jquery": false,
27+
"lastsemic": false,
28+
"latedef": false,
29+
"laxbreak": true,
30+
"laxcomma": false,
31+
"loopfunc": false,
32+
"maxcomplexity": false,
33+
"maxdepth": false,
34+
"maxerr": 50,
35+
"maxlen": 80,
36+
"maxparams": false,
37+
"maxstatements": false,
38+
"mootools": false,
39+
"moz": false,
40+
"multistr": false,
41+
"newcap": true,
42+
"noarg": true,
43+
"node": true,
44+
"noempty": true,
45+
"nonbsp": true,
46+
"nonew": true,
47+
"nonstandard": false,
48+
"notypeof": false,
49+
"noyield": false,
50+
"phantom": false,
51+
"plusplus": false,
52+
"predef": [
53+
"jasmine",
54+
"describe",
55+
"beforeEach",
56+
"it",
57+
"jest",
58+
"pit",
59+
"expect",
60+
"rootRequire"
61+
],
62+
"proto": false,
63+
"prototypejs": false,
64+
"quotmark": true,
65+
"rhino": false,
66+
"scripturl": false,
67+
"shadow": false,
68+
"smarttabs": false,
69+
"strict": true,
70+
"sub": false,
71+
"supernew": false,
72+
"trailing": true,
73+
"undef": true,
74+
"unused": true,
75+
"validthis": false,
76+
"worker": false,
77+
"wsh": false,
78+
"yui": false
79+
}

FS.android.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var warning = require('warning');
4+
5+
var FS = {
6+
test: function() {
7+
warning("Not yet implemented for Android.");
8+
}
9+
};
10+
11+
module.exports = FS;

FS.ios.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
var RNFSManager = require('NativeModules').RNFSManager;
4+
var Promise = require('bluebird');
5+
var base64 = require('base-64');
6+
7+
var _readDir = Promise.promisify(RNFSManager.readDir);
8+
var _stat = Promise.promisify(RNFSManager.stat);
9+
var _readFile = Promise.promisify(RNFSManager.readFile);
10+
11+
var convertError = (err) => {
12+
var error = new Error(err.description);
13+
error.code = err.code;
14+
throw error;
15+
};
16+
17+
var NSFileTypeRegular = RNFSManager.NSFileTypeRegular;
18+
var NSFileTypeDirectory = RNFSManager.NSFileTypeDirectory;
19+
20+
var RNFS = {
21+
22+
readDir(path, rootDir) {
23+
return _readDir(path, rootDir)
24+
.catch(convertError);
25+
},
26+
27+
stat(filepath) {
28+
return _stat(filepath)
29+
.then((result) => {
30+
return {
31+
'ctime': new Date(result.ctime*1000),
32+
'mtime': new Date(result.mtime*1000),
33+
'size': result.size,
34+
isFile: () => result.type === NSFileTypeRegular,
35+
isDirectory: () => result.type === NSFileTypeDirectory,
36+
};
37+
})
38+
.catch(convertError);
39+
},
40+
41+
readFile(filepath, shouldDecode) {
42+
var p = _readFile(filepath);
43+
44+
if (shouldDecode !== false) {
45+
p = p.then((data) => {
46+
return base64.decode(data);
47+
});
48+
}
49+
50+
return p.catch(convertError);
51+
},
52+
53+
MainBundle: RNFSManager.MainBundleDirectory,
54+
CachesDirectory: RNFSManager.NSCachesDirectory,
55+
DocumentDirectory: RNFSManager.NSDocumentDirectory
56+
};
57+
58+
module.exports = RNFS;

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Johannes Lumpe
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

NSArray+Map.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// NSArray+Map.h
3+
// RNFS
4+
//
5+
// taken from http://stackoverflow.com/questions/6127638/nsarray-equivalent-of-map
6+
7+
#import <Foundation/Foundation.h>
8+
9+
@interface NSArray (Map)
10+
11+
- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block;
12+
13+
@end

NSArray+Map.m

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// NSArray+Map.m
3+
// RNFS
4+
5+
#import "NSArray+Map.h"
6+
7+
@implementation NSArray (Map)
8+
9+
- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block {
10+
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
11+
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
12+
[result addObject:block(obj, idx)];
13+
}];
14+
return result;
15+
}
16+
17+
@end

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
## react-native-fs
2+
3+
Native filesystem access for react-native
4+
5+
Note: this project is under development and functionality will improve over time. Currently it provides only the bare minimum of functionality.
6+
7+
Renaming, copying, and creating files will follow soon.
8+
9+
## Usage
10+
11+
First you need to install react-native-fs:
12+
13+
```javascript
14+
npm install react-native-fs --save
15+
```
16+
17+
In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name] Go to node_modules ➜ react-native-keyboardevents and add the .xcodeproj file
18+
19+
In XCode, in the project navigator, select your project. Add the lib*.a from the RNFS project to your project's Build Phases ➜ Link Binary With Libraries Click .xcodeproj file you added before in the project navigator and go the Build Settings tab. Make sure 'All' is toggled on (instead of 'Basic'). Look for Header Search Paths and make sure it contains both $(SRCROOT)/../react-native/React and $(SRCROOT)/../../React - mark both as recursive.
20+
21+
Run your project (Cmd+R)
22+
23+
## Examples
24+
25+
### Basic
26+
27+
```javascript
28+
// require the module
29+
var RNFS = require('react-native-fs');
30+
31+
// get a list of files and directories in the main bundle
32+
RNFS.readDir('/', RNFS.MainBundle)
33+
.then((result) => {
34+
console.log('GOT RESULT', result);
35+
36+
// stat the first file
37+
return Promise.all([RNFS.stat(result[0].path), result[0].path]);
38+
})
39+
.then((statResult) => {
40+
if (statResult[0].isFile()) {
41+
// if we have a file, read it
42+
return RNFS.readFile(statResult[1]);
43+
}
44+
45+
return 'no file';
46+
})
47+
.then((contents) => {
48+
// log the file contents
49+
console.log(contents);
50+
})
51+
.catch((err) => {
52+
console.log(err.message, err.code);
53+
});
54+
```
55+
56+
## API
57+
58+
### `promise readDir(path, directory)`
59+
60+
Reads the contents of `path` in `directory`.
61+
`path` is a string and `directory` is one of the following:
62+
`RNFS.MainBundle`, `RNFS.CachesDirectory`, `RNFS.DocumentDirectory`
63+
64+
The returned promise resolves with an array of objects with the following properties:
65+
66+
`name` (`String`), The name of the item
67+
`path` (`String`), The absolute path to the item
68+
69+
### `promise stat(path)`
70+
71+
Stats an item at `path`.
72+
The promise resolves with an object with the following properties:
73+
`ctime` (`Date`) - The creation date of the item
74+
`mtime` (`Date`) - The modification date of the item
75+
`size` (`Number`) - The size of the item in bytes
76+
`isFile` (`Function`) - Returns true when the item is a file
77+
`isDirectory` (`Function`) - Returns true when the item is a directory
78+
79+
### `promise readFile(path, shouldDecode)`
80+
81+
Reads the file at `path` and - by default - decodes the transferred base64 string. If `shouldDecode` is `false`, the base64 encoded string is returned
82+
83+
Note: you will take quite a performance hit if you are reading big files

0 commit comments

Comments
 (0)