From 4e57a146442cb52fe9084b045340f33cd1820a32 Mon Sep 17 00:00:00 2001 From: JP Richardson Date: Fri, 2 Nov 2012 10:53:37 -0500 Subject: [PATCH] Added touch/touchSync methods. --- CHANGELOG.md | 4 +++ README.md | 21 +++++++++++++++- lib/index.js | 4 +++ lib/touch.js | 53 ++++++++++++++++++++++++++++++++++++++++ package.json | 9 ++++--- test/touch.test.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 lib/touch.js create mode 100644 test/touch.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ba7861..b81de5a 100644 --- a/CHANGELOG.md +++ b/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. diff --git a/README.md b/README.md index 0a038cd..934d024 100644 --- a/README.md +++ b/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. diff --git a/lib/index.js b/lib/index.js index dd13937..702a2ad 100644 --- a/lib/index.js +++ b/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; diff --git a/lib/touch.js b/lib/touch.js new file mode 100644 index 0000000..e490113 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/package.json b/package.json index 0112f55..261f9c0 100755 --- a/package.json +++ b/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 ", "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": { diff --git a/test/touch.test.js b/test/touch.test.js new file mode 100644 index 0000000..7708e7d --- /dev/null +++ b/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'); + }) + }) + }) + +}) \ No newline at end of file