Browse Source

Rewrote main module in JavaScript. Added Travis-CI. Added jsonfile dependence.

ci/travis-osximage
JP Richardson 12 years ago
parent
commit
179033d0fa
  1. 4
      .travis.yml
  2. 6
      CHANGELOG.md
  3. 92
      README.md
  4. 90
      lib/copy.js
  5. 11
      lib/create.js
  6. 81
      lib/index.js
  7. 11
      lib/mkdir.js
  8. 24
      lib/read.js
  9. 42
      lib/remove.js
  10. 73
      package.json
  11. 32
      src/copy.coffee
  12. 35
      src/index.coffee
  13. 4
      src/mkdir.coffee
  14. 13
      src/read.coffee
  15. 25
      src/remove.coffee

4
.travis.yml

@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.6
- 0.8

6
CHANGELOG.md

@ -1,3 +1,9 @@
0.2.0 / 2012-09-10
------------------
* Rewrote module into JavaScript. (Must still rewrite tests into JavaScript)
* Added all methods of [jsonfile][https://github.com/jprichardson/node-jsonfile]
* Added Travis-CI.
0.1.3 / 2012-08-13
------------------
* Added method `readJSONFile`.

92
README.md

@ -1,3 +1,5 @@
[![build status](https://secure.travis-ci.org/jprichardson/node-fs-extra.png)](http://travis-ci.org/jprichardson/node-fs-extra)
Node.js: fs-extra
=================
@ -12,22 +14,6 @@ I got tired of including `mkdirp` and `rimraf` in most of my projects.
Installation
------------
npm install fs-extra
Usage
-----
```javascript
var fs = require('fs-extra');
```
Naming
------
@ -50,7 +36,6 @@ So, if you want to remove a file or a directory regardless of whether it has con
Compromise
----------
@ -58,12 +43,28 @@ If you feel that this module should add functionality, please let me know. If yo
Installation
------------
npm install fs-extra
Usage
-----
```javascript
var fs = require('fs-extra');
```
Methods
-------
**NOTE:** You can still use the native Node.js methods. They are copied over to `fs-extra`.
### copy()
Copy a file or directory. The directory can have contents. Like `cp -r`. There isn't a synchronous version implemented yet.
@ -93,6 +94,7 @@ fs.copy('/tmp/mydir', '/tmp/mynewdir'function(err){
```
### remove()
Removes a file or directory. The directory can have contents. Like `rm -rf`.
@ -117,6 +119,7 @@ fs.removeSync('/home/jprichardson'); //I just deleted my entire HOME directory.
```
### mkdir()
Creates a directory. If the parent hierarchy doesn't exist, it's created. Like `mkdir -p`.
@ -145,7 +148,11 @@ fs.mkdir('/tmp/node/cant/do/this', function(err){
});
```
### fs.readJSONFile()
### Methods from [jsonfile][jsonfile]
### fs.readJSONFile() / fs.readJSONFileSync()
Reads a JSON file and then parses it into an object.
@ -159,12 +166,46 @@ fs.readJSONFile('./package.json', function(err, packageObj) {
});
```
### fs.writeJSONFile() / fs.writeJSONFileSync()
Writes an object to a JSON file.
Example:
```javascript
var fs = require('fs-extra');
fs.writeJSONFile('./package.json', {name: 'fs-extra'}, function(err){
console.log(err);
});
```
### exists() / existsSync()
These methods are actually from `path`. But in Node v0.8 they are moved from `path` to `fs`. So you might as well start future proofing your code now.
These methods are actually from `path`. But in Node v0.8 they are moved from `path` to `fs`. So you can use this module to help make your modules v0.6 and v0.8 compatible.
## License
TODO
----
* Remove all CoffeeScript from tests.
Author
------
`node-fs-extra` was written by [JP Richardson][aboutjp]. You should follow him on Twitter [@jprichardson][twitter]. Also read his coding blog [Procbits][procbits]. If you write software with others, you should checkout [Gitpilot][gitpilot] to make collaboration with Git simple.
License
-------
Licensed under MIT
@ -172,3 +213,14 @@ Copyright (c) 2011-2012 JP Richardson
[1]: http://nodejs.org/docs/latest/api/fs.html
[jsonfile]: https://github.com/jprichardson/node-jsonfile
[aboutjp]: http://about.me/jprichardson
[twitter]: http://twitter.com/jprichardson
[procbits]: http://procbits.com
[gitpilot]: http://gitpilot.com

90
lib/copy.js

@ -1,48 +1,42 @@
// Generated by CoffeeScript 1.3.3
(function() {
var BUF_LENGTH, copy, copyFile, copyFileSync, fs, ncp, _buff;
fs = require('fs');
ncp = require('ncp').ncp;
BUF_LENGTH = 64 * 1024;
_buff = new Buffer(BUF_LENGTH);
copyFileSync = function(srcFile, destFile) {
var bytesRead, fdr, fdw, pos;
fdr = fs.openSync(srcFile, 'r');
fdw = fs.openSync(destFile, 'w');
bytesRead = 1;
pos = 0;
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos);
fs.writeSync(fdw, _buff, 0, bytesRead);
pos += bytesRead;
}
fs.closeSync(fdr);
return fs.closeSync(fdw);
};
copyFile = function(srcFile, destFile, cb) {
var fdr, fdw;
fdr = fs.createReadStream(srcFile);
fdw = fs.createWriteStream(destFile);
fdr.on('end', function() {
return cb(null);
});
return fdr.pipe(fdw);
};
copy = function(source, dest, callback) {
return ncp(source, dest, callback);
};
module.exports.copyFileSync = copyFileSync;
module.exports.copyFile = copyFile;
module.exports.copy = copy;
}).call(this);
var fs = require('fs')
, ncp = require('ncp').ncp;
var BUF_LENGTH = 64 * 1024;
var _buff = new Buffer(BUF_LENGTH);
copyFileSync = function(srcFile, destFile) {
var bytesRead, fdr, fdw, pos;
fdr = fs.openSync(srcFile, 'r');
fdw = fs.openSync(destFile, 'w');
bytesRead = 1;
pos = 0;
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos);
fs.writeSync(fdw, _buff, 0, bytesRead);
pos += bytesRead;
}
fs.closeSync(fdr);
return fs.closeSync(fdw);
};
copyFile = function(srcFile, destFile, cb) {
var fdr, fdw;
fdr = fs.createReadStream(srcFile);
fdw = fs.createWriteStream(destFile);
fdr.on('end', function() {
return cb(null);
});
return fdr.pipe(fdw);
};
function copy(source, dest, callback) {
if (callback)
ncp(source, dest, callback);
else
ncp(source, dest, function(){});
};
module.exports.copyFileSync = copyFileSync;
module.exports.copyFile = copyFile;
module.exports.copy = copy;

11
lib/create.js

@ -0,0 +1,11 @@
var fs = require('fs');
function touch(path, callback) {
fs.writeFile(path, '', callback);
}
function touchSync(path) {
}
module.exports.touch = touch;

81
lib/index.js

@ -1,54 +1,45 @@
// Generated by CoffeeScript 1.3.3
(function() {
var copy, fs, fse, key, mkdir, path, read, remove, val;
fs = require('fs');
path = require('path');
fse = {};
for (key in fs) {
val = fs[key];
if (typeof val === 'function') {
fse[key] = val;
var fs = require('fs')
, path = require('path')
, jsonFile = require('jsonfile')
, fse = {};
for (var funcName in fs) {
var func = fs[funcName];
if (fs.hasOwnProperty(funcName)) {
if (typeof func == 'function')
fse[funcName] = func;
}
}
fs = fse;
copy = require('./copy');
fs.copy = copy.copy;
remove = require('./remove');
fs.remove = remove.remove;
fs.removeSync = remove.removeSync;
fs["delete"] = fs.remove;
fs.deleteSync = fs.removeSync;
mkdir = require('./mkdir');
}
fs.mkdir = mkdir.mkdir;
fs = fse;
fs.mkdirSync = mkdir.mkdirSync;
fs.copy = require('./copy').copy;
if (!(fs.exists != null)) {
fs.exists = path.exists;
}
var remove = require('./remove');
fs.remove = remove.remove;
fs.removeSync = remove.removeSync;
fs['delete'] = fs.remove
fs.deleteSync = fs.removeSync
if (!(fs.existsSync != null)) {
fs.existsSync = path.existsSync;
}
var mkdir = require('./mkdir')
fs.mkdir = mkdir.mkdir
fs.mkdirSync = mkdir.mkdirSync
read = require('./read');
fs.readJsonFile = jsonFile.readFile;
fs.readJSONFile = jsonFile.readFile;
fs.readJsonFileSync = jsonFile.readFileSync;
fs.readJSONFileSync = jsonFile.readFileSync;
fs.readJSONFile = read.readJSONFile;
fs.writeJsonFile = jsonFile.writeFile;
fs.writeJSONFile = jsonFile.writeFile;
fs.writeJsonFileSync = jsonFile.writeFileSync;
fs.writeJSONFileSync = jsonFile.writeFileSync;
module.exports = fs;
//make compatible for Node v0.8
if (typeof fs.exists == 'undefined')
fs.exists = path.exists
if (typeof fs.existsSync == 'undefined')
fs.existsSync = path.existsSync
}).call(this);
module.exports = fs
module.exports.jsonfile = jsonFile; //so users of fs-extra can modify jsonFile.spaces;

11
lib/mkdir.js

@ -1,11 +1,6 @@
// Generated by CoffeeScript 1.3.3
(function() {
var mkdirp;
var mkdirp = require('mkdirp');
mkdirp = require('mkdirp');
module.exports.mkdir = mkdirp;
module.exports.mkdirSync = mkdirp.sync;
module.exports.mkdir = mkdirp;
module.exports.mkdirSync = mkdirp.sync;
}).call(this);

24
lib/read.js

@ -1,24 +0,0 @@
// Generated by CoffeeScript 1.3.3
(function() {
var fs, readJSONFile;
fs = require('fs');
readJSONFile = function(file, callback) {
return fs.readFile(file, function(err, data) {
var obj;
obj = {};
try {
data = data.toString();
obj = JSON.parse(data);
} catch (error) {
callback(error, null);
return;
}
return callback(null, obj);
});
};
module.exports.readJSONFile = readJSONFile;
}).call(this);

42
lib/remove.js

@ -1,39 +1,17 @@
// Generated by CoffeeScript 1.3.3
(function() {
var fs, rimraf, rmrf, rmrfSync;
var rimraf = require('rimraf')
, fs = require('fs');
rimraf = require('rimraf');
fs = require('fs');
rmrfSync = function(dir) {
function rmrfSync(dir) {
return rimraf.sync(dir);
};
}
rmrf = function(dir, cb) {
function rmrf(dir, cb) {
if (cb != null) {
return rimraf(dir, cb);
return rimraf(dir, cb);
} else {
return rimraf(dir, (function() {}));
return rimraf(dir, (function() {}));
}
};
/*
remove = (path, callback) ->
fs.lstat path, (err, stats) ->
if stats.isDirectory()
rimraf(path, callback)
else
fs.unlink(path, callback)
*/
module.exports.rmrfSync = rmrfSync;
module.exports.rmrf = rmrf;
module.exports.remove = rmrf;
module.exports.removeSync = rmrfSync;
}
}).call(this);
module.exports.remove = rmrf;
module.exports.removeSync = rmrfSync;

73
package.json

@ -1,31 +1,46 @@
{
"name" : "fs-extra",
"version" : "0.1.3",
"description" : "fs-extra contains methods that aren't included in the vanilla Node.js fs package.",
"homepage" : [
"https://github.com/jprichardson/node-fs-extra"
],
"repository" : {
"type" : "git",
"url" : "https://github.com/jprichardson/node-fs-extra"
},
"keywords" : ["fs","file","file system", "copy", "directory", "extra", "mkdirp", "recursive"],
"author" : "JP Richardson <jprichardson@gmail.com>",
"licenses" : [ {
"type" : "MIT",
"url" : "http://github.com/jprichardson/node-fs-extra/raw/master/LICENSE"
}],
"dependencies" : {
"rimraf": "1.0.x",
"ncp": "0.2.x",
"mkdirp": "0.3.x"
},
"devDepdencies":{
"mocha": "1.3.x",
"growl": "1.5.x",
"coffee-script": "1.3.x",
"test-util": "0.1.x",
"path-extra": "0.0.x"
},
"main" : "./lib/index"
"name": "fs-extra",
"version": "0.2.0",
"description": "fs-extra contains methods that aren't included in the vanilla Node.js fs package.",
"homepage": [
"https://github.com/jprichardson/node-fs-extra"
],
"repository": {
"type": "git",
"url": "https://github.com/jprichardson/node-fs-extra"
},
"keywords": [
"fs",
"file",
"file system",
"copy",
"directory",
"extra",
"mkdirp",
"recursive"
],
"author": "JP Richardson <jprichardson@gmail.com>",
"licenses": [
{
"type": "MIT",
"url": "http://github.com/jprichardson/node-fs-extra/raw/master/LICENSE"
}
],
"dependencies": {
"rimraf": "1.0.x",
"ncp": "0.2.x",
"mkdirp": "0.3.x",
"jsonfile": "0.0.x"
},
"devDepdencies": {
"mocha": "1.3.x",
"growl": "1.5.x",
"coffee-script": "1.3.x",
"test-util": "0.1.x",
"path-extra": "0.0.x"
},
"main": "./lib/index",
"scripts": {
"test": "mocha test"
}
}

32
src/copy.coffee

@ -1,32 +0,0 @@
fs = require('fs')
ncp = require('ncp').ncp
BUF_LENGTH = 64*1024
_buff = new Buffer(BUF_LENGTH)
copyFileSync = (srcFile, destFile) ->
fdr = fs.openSync(srcFile, 'r')
fdw = fs.openSync(destFile, 'w')
bytesRead = 1
pos = 0
while bytesRead > 0
bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
fs.writeSync(fdw,_buff,0,bytesRead)
pos += bytesRead
fs.closeSync(fdr)
fs.closeSync(fdw)
copyFile = (srcFile, destFile, cb) ->
fdr = fs.createReadStream(srcFile)
fdw = fs.createWriteStream(destFile)
fdr.on 'end', ->
cb(null)
fdr.pipe(fdw)
copy = (source, dest, callback) ->
ncp(source, dest, callback)
module.exports.copyFileSync = copyFileSync
module.exports.copyFile = copyFile
module.exports.copy = copy

35
src/index.coffee

@ -1,35 +0,0 @@
fs = require('fs')
path = require('path')
fse = {}
for key,val of fs
if typeof val is 'function'
fse[key] = val
fs = fse
#fs-extra
copy = require('./copy')
fs.copy = copy.copy
remove = require('./remove')
fs.remove = remove.remove
fs.removeSync = remove.removeSync
fs.delete = fs.remove
fs.deleteSync = fs.removeSync
mkdir = require('./mkdir')
fs.mkdir = mkdir.mkdir
fs.mkdirSync = mkdir.mkdirSync
#make compatible for Node v0.8
if not fs.exists?
fs.exists = path.exists
if not fs.existsSync?
fs.existsSync = path.existsSync
read = require('./read')
fs.readJSONFile = read.readJSONFile
module.exports = fs

4
src/mkdir.coffee

@ -1,4 +0,0 @@
mkdirp = require('mkdirp')
module.exports.mkdir = mkdirp
module.exports.mkdirSync = mkdirp.sync

13
src/read.coffee

@ -1,13 +0,0 @@
fs = require('fs')
readJSONFile = (file, callback) ->
fs.readFile file, (err, data) ->
obj = {}
try
data = data.toString()
obj = JSON.parse(data)
catch error
callback(error, null); return
callback(null, obj)
module.exports.readJSONFile = readJSONFile

25
src/remove.coffee

@ -1,25 +0,0 @@
rimraf = require('rimraf')
fs = require('fs')
rmrfSync = (dir) ->
rimraf.sync(dir)
rmrf = (dir,cb) ->
if cb?
rimraf(dir,cb)
else #rimraf throughs an error without a callback
rimraf(dir, (->))
###
remove = (path, callback) ->
fs.lstat path, (err, stats) ->
if stats.isDirectory()
rimraf(path, callback)
else
fs.unlink(path, callback)
###
module.exports.rmrfSync = rmrfSync
module.exports.rmrf = rmrf
module.exports.remove = rmrf
module.exports.removeSync = rmrfSync
Loading…
Cancel
Save