Browse Source

Added touch/touchSync methods.

ci/travis-osximage v0.3.2
JP Richardson 12 years ago
parent
commit
4e57a14644
  1. 4
      CHANGELOG.md
  2. 21
      README.md
  3. 4
      lib/index.js
  4. 53
      lib/touch.js
  5. 9
      package.json
  6. 61
      test/touch.test.js

4
CHANGELOG.md

@ -1,3 +1,7 @@
0.3.2 / 2012-11-01
------------------
* Added `touch()` and `touchSync()` methods.
0.3.1 / 2012-10-11
------------------
* Fixed some stray globals.

21
README.md

@ -149,6 +149,25 @@ fs.mkdir('/tmp/node/cant/do/this', function(err){
```
### touch() / touchSync()
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**.
Example:
```javascript
var fs = require('fs')
, file = '/tmp/this/path/does/not/exist/file.txt'
fs.touch(file, function(err) {
console.log(err); //null
//file has now been created, including the directory it is to be placed in
})
### Methods from [jsonfile][jsonfile]
@ -184,7 +203,7 @@ fs.writeJSONFile('./package.json', {name: 'fs-extra'}, function(err){
### exists() / existsSync()
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.
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.

4
lib/index.js

@ -27,6 +27,10 @@ fs.mkdirsSync = mkdir.mkdirsSync
fs.mkdirp = mkdir.mkdirs
fs.mkdirpSync = mkdir.mkdirsSync
var touch = require('./touch')
fs.touch = touch.touch;
fs.touchSync = touch.touchSync;
fs.readJsonFile = jsonFile.readFile;
fs.readJSONFile = jsonFile.readFile;
fs.readJsonFileSync = jsonFile.readFileSync;

53
lib/touch.js

@ -0,0 +1,53 @@
var mkdir = require('./mkdir')
, path = require('path')
, fs = require('fs')
, exists = fs.exists || path.exists
, existsSync = fs.existsSync || path.existsSync
function touch (file, callback) {
exists(file, function(fileExists) {
if (fileExists)
return callback(null);
else {
var dir = path.dirname(file);
function makeFile() {
fs.writeFile(file, '', function(err) {
if (err)
callback(err)
else
callback(null);
})
}
exists(dir, function(dirExists) {
if (!dirExists) {
mkdir.mkdirs(dir, function(err) {
if (err)
callback(err)
else
makeFile();
})
} else {
makeFile();
}
})
}
})
}
function touchSync (file) {
if (existsSync(file))
return;
var dir = path.dirname(file);
if (!existsSync(dir))
mkdir.mkdirsSync(dir);
fs.writeFileSync(file, '');
}
module.exports.touch = touch;
module.exports.touchSync = touchSync;

9
package.json

@ -1,6 +1,6 @@
{
"name": "fs-extra",
"version": "0.3.1",
"version": "0.3.2",
"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"
@ -17,13 +17,16 @@
"directory",
"extra",
"mkdirp",
"mkdir",
"mkdirs",
"recursive",
"json",
"read",
"write",
"extra",
"delete",
"remove"
"remove",
"touch"
],
"author": "JP Richardson <jprichardson@gmail.com>",
"licenses": [
@ -41,7 +44,7 @@
"devDependencies": {
"mocha": "1.4.x",
"path-extra": "0.0.x",
"testutil": "~0.2.2"
"testutil": "~0.2.4"
},
"main": "./lib/index",
"scripts": {

61
test/touch.test.js

@ -0,0 +1,61 @@
var testutil = require('testutil')
, fs = require('../')
, path = require('path')
var TEST_DIR = '';
describe('fs-extra', function () {
beforeEach(function(done) {
TEST_DIR = testutil.createTestDir('fs-extra');
done();
})
describe('+ touch', function() {
describe('> when the file and directory does not exist', function() {
it('should create the file', function(done) {
var file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt');
F (fs.existsSync(file));
fs.touch(file, function(err) {
F (err);
T (fs.existsSync(file));
done();
})
})
})
describe('> when the file does exist', function() {
it('should not modify the file', function(done) {
var file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt');
fs.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world');
fs.touch(file, function(err) {
F (err);
T (fs.readFileSync(file, 'utf8') === 'hello world');
done();
})
})
})
})
describe('+ touchSync', function() {
describe('> when the file and directory does not exist', function() {
it('should create the file', function() {
var file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt');
F (fs.existsSync(file));
fs.touchSync(file);
T (fs.existsSync(file));
})
})
describe('> when the file does exist', function() {
it('should not modify the file', function() {
var file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt');
fs.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world');
fs.touchSync(file);
T (fs.readFileSync(file, 'utf8') === 'hello world');
})
})
})
})
Loading…
Cancel
Save