Browse Source

Removed readTextFile. Shortened JSON methods.

ci/travis-osximage
JP Richardson 12 years ago
parent
commit
2e8db7abe7
  1. 1
      .travis.yml
  2. 6
      CHANGELOG.md
  3. 167
      README.md
  4. 14
      lib/index.js
  5. 2
      package.json
  6. 10
      test/read.test.js

1
.travis.yml

@ -2,3 +2,4 @@ language: node_js
node_js:
- 0.6
- 0.8
- 0.9

6
CHANGELOG.md

@ -1,3 +1,9 @@
0.5.0 / 2013-02-03
------------------
* Removed `readTextFile`.
* Renamed `readJSONFile` to `readJSON` and `readJson`, same with write.
* Restructured documentation a bit. Added roadmap.
0.4.0 / 2013-01-28
------------------
* Set default spaces in `jsonfile` from 4 to 2.

167
README.md

@ -10,38 +10,10 @@ This module adds a few extra file system methods that aren't included in the nat
Why?
----
I got tired of including `mkdirp` and `rimraf` in most of my projects.
I got tired of including `mkdirp`, `rimraf`, and `cp -r` in most of my projects.
Naming
------
I put a lot of thought into the naming of these function. Inspired by @coolaj86's request. So he deserves much of the credit for raising the issue. See discussion(s) here:
* https://github.com/jprichardson/node-fs-extra/issues/2
* https://github.com/flatiron/utile/issues/11
* https://github.com/ryanmcgrath/wrench-js/issues/29
* https://github.com/substack/node-mkdirp/issues/17
First, I believe that in as many cases as possible, the [Node.js naming schemes](http://nodejs.org/api/fs.html) should be chosen. However, there are problems with the Node.js own naming schemes.
For example, `fs.readFile()` and `fs.readdir()`: the **F** is capitalized in *File* and the **d** is not capitalized in *dir*. Perhaps a bit pedantic, but they should still be consistent. Also, Node.js has chosen a lot of POSIX naming schemes, which I believe is great. See: `fs.mkdir()`, `fs.rmdir()`, `fs.chown()`, etc.
We have a dilemma though. How do you consistently name methods that perform the following POSIX commands: `cp`, `cp -r`, `mkdir -p`, and `rm -rf`?
My perspective: when in doubt, err on the side of simplicity. Consider that for a moment. A directory is just a hierarchical grouping of directories and files. So when you want to copy it or remove it, in most cases you'll want to copy or remove all of its contents. When you want to create a directory, if the directory that it's suppose to be contained in does not exist, then in most cases you'll want to create that too.
So, if you want to remove a file or a directory regardless of whether it has contents, just call `fs.remove(path)` or its alias `fs.delete(path)`. If you want to copy a file or a directory whether it has contents, just call `fs.copy(source, destination)`. If you want to create a directory regardless of whether its parent directories exist, just call `fs.mkdirs(path)` or `fs.mkdirp(path)`.
Compromise
----------
If you feel that this module should add functionality, please let me know. If you don't like the naming scheme, let me know that as well. I'm willing to work with the community so that we can develop a logical grouping of file system functions that aren't found Node.js.
Installation
------------
@ -97,37 +69,39 @@ fs.copy('/tmp/mydir', '/tmp/mynewdir', function(err){
```
### createFile(file, callback)
### remove(dir, callback) / delete(dir, callback)
Removes a file or directory. The directory can have contents. Like `rm -rf`.
Creates a file. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is **NOT MODIFIED**.
Sync: `removeSync()` / `deleteSync()`
Sync: `createFileSync()`
Examples:
Example:
```javascript
var fs = require('fs-extra');
var fs = require('fs-extra')
, file = '/tmp/this/path/does/not/exist/file.txt'
fs.remove('/tmp/myfile', function(err){
if (err) {
console.error(err);
}
else {
console.log("success!")
}
});
fs.createFile(file, function(err) {
console.log(err); //null
fs.removeSync('/home/jprichardson'); //I just deleted my entire HOME directory.
//file has now been created, including the directory it is to be placed in
})
```
### exists() / existsSync()
These methods are actually from `path` in v0.6. 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.
### mkdirs(dir, callback) / mkdirp(dir, callback)
### mkdirs(dir, callback)
Creates a directory. If the parent hierarchy doesn't exist, it's created. Like `mkdir -p`.
Alias: `mkdirp()`
Sync: `mkdirsSync()` / `mkdirpSync()`
@ -156,27 +130,6 @@ fs.mkdir('/tmp/node/cant/do/this', function(err){
```
### createFile(file, callback)
Creates a file. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is **NOT MODIFIED**.
Sync: `createFileSync()`
Example:
```javascript
var fs = require('fs-extra')
, file = '/tmp/this/path/does/not/exist/file.txt'
fs.createFile(file, function(err) {
console.log(err); //null
//file has now been created, including the directory it is to be placed in
})
```
### outputFile(file, data, callback)
Almost the same as `writeFile`, except that if the directory does not exist, it's created.
@ -200,63 +153,107 @@ fs.outputFile(file, 'hello!' function(err) {
```
### readTextFile(file, callback)
### readJson(file, callback)
Reads a JSON file and then parses it into an object.
Exactly the same as `readFile(file, 'utf8', callback)`.
Alias: `readJSON()`
Sync: `readTextFileSync()`.
Sync: `readJsonSync()`, `readJSONSync()`
Example:
```javascript
var fs = require('fs-extra')
, file = '/etc/passwd'
var fs = require('fs-extra');
fs.readTextFile(file, function(err, data) { //instead of fs.readFile(file, 'utf8', callback)
console.log(data); // (contents of your /etc/passwd) file
})
fs.readJson('./package.json', function(err, packageObj) {
console.log(packageObj.version); //0.1.3
});
```
### remove(dir, callback)
### Methods from [jsonfile][jsonfile]
Removes a file or directory. The directory can have contents. Like `rm -rf`.
### fs.readJSONFile() / fs.readJSONFileSync()
Alias: `delete()`
Reads a JSON file and then parses it into an object.
Sync: `removeSync()` / `deleteSync()`
Example:
Examples:
```javascript
var fs = require('fs-extra');
fs.readJSONFile('./package.json', function(err, packageObj) {
console.log(packageObj.version); //0.1.3
fs.remove('/tmp/myfile', function(err){
if (err) {
console.error(err);
}
else {
console.log("success!")
}
});
fs.removeSync('/home/jprichardson'); //I just deleted my entire HOME directory.
```
### fs.writeJSONFile() / fs.writeJSONFileSync()
### writeJson(file, object, callback)
Writes an object to a JSON file.
Alias: `writeJSON()`
Sync: `writeJsonSync()`, `writeJSONSync()`
Example:
```javascript
var fs = require('fs-extra');
fs.writeJSONFile('./package.json', {name: 'fs-extra'}, function(err){
fs.writeJson('./package.json', {name: 'fs-extra'}, function(err){
console.log(err);
});
```
### exists() / existsSync()
Roadmap
-------
This contains items that I'm considering doing. I'd love community feedback.
* File system walker. I really like this one: https://github.com/daaku/nodejs-walker
* File/directory tree watcher. There are quite a few.
* Method to move files.
* Copy sync.
* Thinking about moving `rimraf`, `ncp`, and `mkdirps` code into this library. I'd like fs-extra to be a stable library that module authors
can depend upon. A bunch of other dependencies kinda sucks for modules/libraries.
* Change documentation to use the `fse` prefix instead of `fs`. This may encourage people to start using `fse` as a prefix and hence make their code clearer that they're not using the native `fs`. I'm very undecided on this one since `fs-extra` is a drop in replacement for the native `fs`.
These methods are actually from `path` in v0.6. 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.
Naming
------
I put a lot of thought into the naming of these function. Inspired by @coolaj86's request. So he deserves much of the credit for raising the issue. See discussion(s) here:
* https://github.com/jprichardson/node-fs-extra/issues/2
* https://github.com/flatiron/utile/issues/11
* https://github.com/ryanmcgrath/wrench-js/issues/29
* https://github.com/substack/node-mkdirp/issues/17
First, I believe that in as many cases as possible, the [Node.js naming schemes](http://nodejs.org/api/fs.html) should be chosen. However, there are problems with the Node.js own naming schemes.
For example, `fs.readFile()` and `fs.readdir()`: the **F** is capitalized in *File* and the **d** is not capitalized in *dir*. Perhaps a bit pedantic, but they should still be consistent. Also, Node.js has chosen a lot of POSIX naming schemes, which I believe is great. See: `fs.mkdir()`, `fs.rmdir()`, `fs.chown()`, etc.
We have a dilemma though. How do you consistently name methods that perform the following POSIX commands: `cp`, `cp -r`, `mkdir -p`, and `rm -rf`?
My perspective: when in doubt, err on the side of simplicity. Consider that for a moment. A directory is just a hierarchical grouping of directories and files. So when you want to copy it or remove it, in most cases you'll want to copy or remove all of its contents. When you want to create a directory, if the directory that it's suppose to be contained in does not exist, then in most cases you'll want to create that too.
So, if you want to remove a file or a directory regardless of whether it has contents, just call `fs.remove(path)` or its alias `fs.delete(path)`. If you want to copy a file or a directory whether it has contents, just call `fs.copy(source, destination)`. If you want to create a directory regardless of whether its parent directories exist, just call `fs.mkdirs(path)` or `fs.mkdirp(path)`.
@ -274,7 +271,7 @@ License
Licensed under MIT
Copyright (c) 2011-2012 JP Richardson
Copyright (c) 2011-2013 JP Richardson
[1]: http://nodejs.org/docs/latest/api/fs.html

14
lib/index.js

@ -58,13 +58,13 @@ fs.outputFileSync = output.outputFileSync;
// read
fs.readTextFile = function(file, callback) {
/*fs.readTextFile = function(file, callback) {
return fs.readFile(file, 'utf8', callback)
}
fs.readTextFileSync = function(file, callback) {
return fs.readFileSync(file, 'utf8')
}
}*/
// json files
@ -73,11 +73,21 @@ fs.readJSONFile = jsonFile.readFile;
fs.readJsonFileSync = jsonFile.readFileSync;
fs.readJSONFileSync = jsonFile.readFileSync;
fs.readJson = jsonFile.readFile;
fs.readJSON = jsonFile.readFile;
fs.readJsonSync = jsonFile.readFileSync;
fs.readJSONSync = jsonFile.readFileSync;
fs.writeJsonFile = jsonFile.writeFile;
fs.writeJSONFile = jsonFile.writeFile;
fs.writeJsonFileSync = jsonFile.writeFileSync;
fs.writeJSONFileSync = jsonFile.writeFileSync;
fs.writeJson = jsonFile.writeFile;
fs.writeJSON = jsonFile.writeFile;
fs.writeJsonSync = jsonFile.writeFileSync;
fs.writeJSONSync = jsonFile.writeFileSync;
//make compatible for Node v0.8
if (typeof fs.exists == 'undefined')

2
package.json

@ -1,6 +1,6 @@
{
"name": "fs-extra",
"version": "0.4.0",
"version": "0.5.0",
"description": "fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.",
"homepage": [
"https://github.com/jprichardson/node-fs-extra"

10
test/read.test.js

@ -13,7 +13,7 @@ describe('fs-extra', function() {
fs.remove(DIR, done);
})
describe('+ readJSONFile', function() {
describe('+ readJSON', function() {
it('should read a file and parse the json', function(done) {
var obj1 = {
firstName: 'JP',
@ -23,7 +23,7 @@ describe('fs-extra', function() {
fs.writeFileSync(file, JSON.stringify(obj1));
fs.readJSONFile(file, function(err, obj2) {
fs.readJSON(file, function(err, obj2) {
F (err != null);
T (obj1.firstName === obj2.firstName);
T (obj1.lastName === obj2.lastName);
@ -35,7 +35,7 @@ describe('fs-extra', function() {
it('should error if it cant parse the json', function(done) {
var file = path.join(DIR, 'file2.json');
fs.writeFileSync(file, '%asdfasdff444');
fs.readJSONFile(file, function(err, obj) {
fs.readJSON(file, function(err, obj) {
T (err != null);
F (obj);
done();
@ -43,7 +43,7 @@ describe('fs-extra', function() {
})
})
describe('+ readTextFile', function() {
/*(describe('+ readTextFile', function() {
it('should read the text file', function(done) {
var file = path.join(DIR, 'readtext.txt')
fs.writeFileSync(file, "hello")
@ -62,7 +62,7 @@ describe('fs-extra', function() {
var data = fs.readTextFileSync(file)
EQ (data, 'hello')
})
})
})*/
})

Loading…
Cancel
Save