Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneslumpe committed May 8, 2015
0 parents commit 1821893
Show file tree
Hide file tree
Showing 17 changed files with 853 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
workbench
79 changes: 79 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"-W093": true,
"asi": false,
"bitwise": true,
"boss": false,
"browser": false,
"camelcase": true,
"couch": false,
"curly": true,
"debug": false,
"devel": true,
"dojo": false,
"eqeqeq": true,
"eqnull": false,
"esnext": true,
"evil": false,
"expr": true,
"forin": false,
"freeze": true,
"funcscope": true,
"gcl": false,
"globalstrict": true,
"immed": false,
"indent": 2,
"iterator": false,
"jquery": false,
"lastsemic": false,
"latedef": false,
"laxbreak": true,
"laxcomma": false,
"loopfunc": false,
"maxcomplexity": false,
"maxdepth": false,
"maxerr": 50,
"maxlen": 80,
"maxparams": false,
"maxstatements": false,
"mootools": false,
"moz": false,
"multistr": false,
"newcap": true,
"noarg": true,
"node": true,
"noempty": true,
"nonbsp": true,
"nonew": true,
"nonstandard": false,
"notypeof": false,
"noyield": false,
"phantom": false,
"plusplus": false,
"predef": [
"jasmine",
"describe",
"beforeEach",
"it",
"jest",
"pit",
"expect",
"rootRequire"
],
"proto": false,
"prototypejs": false,
"quotmark": true,
"rhino": false,
"scripturl": false,
"shadow": false,
"smarttabs": false,
"strict": true,
"sub": false,
"supernew": false,
"trailing": true,
"undef": true,
"unused": true,
"validthis": false,
"worker": false,
"wsh": false,
"yui": false
}
11 changes: 11 additions & 0 deletions FS.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var warning = require('warning');

var FS = {
test: function() {
warning("Not yet implemented for Android.");
}
};

module.exports = FS;
58 changes: 58 additions & 0 deletions FS.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

var RNFSManager = require('NativeModules').RNFSManager;
var Promise = require('bluebird');
var base64 = require('base-64');

var _readDir = Promise.promisify(RNFSManager.readDir);
var _stat = Promise.promisify(RNFSManager.stat);
var _readFile = Promise.promisify(RNFSManager.readFile);

var convertError = (err) => {
var error = new Error(err.description);
error.code = err.code;
throw error;
};

var NSFileTypeRegular = RNFSManager.NSFileTypeRegular;
var NSFileTypeDirectory = RNFSManager.NSFileTypeDirectory;

var RNFS = {

readDir(path, rootDir) {
return _readDir(path, rootDir)
.catch(convertError);
},

stat(filepath) {
return _stat(filepath)
.then((result) => {
return {
'ctime': new Date(result.ctime*1000),
'mtime': new Date(result.mtime*1000),
'size': result.size,
isFile: () => result.type === NSFileTypeRegular,
isDirectory: () => result.type === NSFileTypeDirectory,
};
})
.catch(convertError);
},

readFile(filepath, shouldDecode) {
var p = _readFile(filepath);

if (shouldDecode !== false) {
p = p.then((data) => {
return base64.decode(data);
});
}

return p.catch(convertError);
},

MainBundle: RNFSManager.MainBundleDirectory,
CachesDirectory: RNFSManager.NSCachesDirectory,
DocumentDirectory: RNFSManager.NSDocumentDirectory
};

module.exports = RNFS;
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Johannes Lumpe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions NSArray+Map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// NSArray+Map.h
// RNFS
//
// taken from http://stackoverflow.com/questions/6127638/nsarray-equivalent-of-map

#import <Foundation/Foundation.h>

@interface NSArray (Map)

- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block;

@end
17 changes: 17 additions & 0 deletions NSArray+Map.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSArray+Map.m
// RNFS

#import "NSArray+Map.h"

@implementation NSArray (Map)

- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[result addObject:block(obj, idx)];
}];
return result;
}

@end
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
## react-native-fs

Native filesystem access for react-native

Note: this project is under development and functionality will improve over time. Currently it provides only the bare minimum of functionality.

Renaming, copying, and creating files will follow soon.

## Usage

First you need to install react-native-fs:

```javascript
npm install react-native-fs --save
```

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

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.

Run your project (Cmd+R)

## Examples

### Basic

```javascript
// require the module
var RNFS = require('react-native-fs');

// get a list of files and directories in the main bundle
RNFS.readDir('/', RNFS.MainBundle)
.then((result) => {
console.log('GOT RESULT', result);

// stat the first file
return Promise.all([RNFS.stat(result[0].path), result[0].path]);
})
.then((statResult) => {
if (statResult[0].isFile()) {
// if we have a file, read it
return RNFS.readFile(statResult[1]);
}

return 'no file';
})
.then((contents) => {
// log the file contents
console.log(contents);
})
.catch((err) => {
console.log(err.message, err.code);
});
```

## API

### `promise readDir(path, directory)`

Reads the contents of `path` in `directory`.
`path` is a string and `directory` is one of the following:
`RNFS.MainBundle`, `RNFS.CachesDirectory`, `RNFS.DocumentDirectory`

The returned promise resolves with an array of objects with the following properties:

`name` (`String`), The name of the item
`path` (`String`), The absolute path to the item

### `promise stat(path)`

Stats an item at `path`.
The promise resolves with an object with the following properties:
`ctime` (`Date`) - The creation date of the item
`mtime` (`Date`) - The modification date of the item
`size` (`Number`) - The size of the item in bytes
`isFile` (`Function`) - Returns true when the item is a file
`isDirectory` (`Function`) - Returns true when the item is a directory

### `promise readFile(path, shouldDecode)`

Reads the file at `path` and - by default - decodes the transferred base64 string. If `shouldDecode` is `false`, the base64 encoded string is returned

Note: you will take quite a performance hit if you are reading big files
Loading

0 comments on commit 1821893

Please sign in to comment.