JP Richardson
12 years ago
6 changed files with 148 additions and 4 deletions
@ -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; |
@ -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…
Reference in new issue