Skip to content

Commit

Permalink
getWriteStream
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph committed Mar 15, 2015
1 parent c062045 commit 2dbee57
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/get-write-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var path = require('path')
var fs = require('graceful-fs')
var mkdir = require('./mkdir')

function getWriteStream (file, options, callback) {
if (typeof options === 'function') {
callback = options
options = null
}

var dir = path.dirname(file)
fs.exists(dir, function(itDoes) {
if (itDoes) return callback(null, fs.createWriteStream(file, options))

mkdir.mkdirs(dir, function(err) {
if (err) return callback(err)

callback(null, fs.createWriteStream(file, options))
})
})
}

function getWriteStreamSync (file, options) {
var dir = path.dirname(file)
if (!fs.existsSync(dir)) {
mkdir.mkdirsSync(dir)
}
return fs.createWriteStream(file, options)
}

module.exports = {
getWriteStream: getWriteStream,
getWriteStreamSync: getWriteStreamSync
}
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ fs.ensureFileSync = create.createFileSync
fs.ensureDir = mkdir.mkdirs
fs.ensureDirSync = mkdir.mkdirsSync

var getWriteStream = require('./get-write-stream')
fs.getWriteStream = getWriteStream.getWriteStream
fs.getWriteStreamSync = getWriteStream.getWriteStreamSync

var move = require('./move')
fs.move = function(src, dest, opts, callback) {
Expand Down
81 changes: 81 additions & 0 deletions test/get-write-stream.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
var assert = require('assert')
var fs = require('fs')
var path = require('path')
var testutil = require('testutil')
var fse = require('../')

/* global afterEach, beforeEach, describe, it */

var TEST_DIR = ''

describe('getWriteStream', function () {
beforeEach(function() {
TEST_DIR = testutil.createTestDir('fs-extra')
})

afterEach(function(done) {
fse.remove(TEST_DIR, done)
})

describe('+ getWriteStream', function() {
describe('> when the file and directory does not exist', function() {
it('should create the stream', function(done) {
var file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
fse.getWriteStream(file, function(err, stream) {
if (err) return done(err)
stream.end('hi jp', 'utf8', function(){
assert(fs.existsSync(file))
assert.equal(fs.readFileSync(file, 'utf8'), 'hi jp')
done()
})
})
})
})

describe('> when the file does exist', function() {
it('should still modify the file', function(done) {
var file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world')
fse.getWriteStream(file, function(err, stream) {
if (err) return done(err)
stream.end('hi jp', 'utf8', function(){
assert(fs.existsSync(file))
assert.equal(fs.readFileSync(file, 'utf8'), 'hi jp')
done()
})
})
})
})
})

describe('+ getWriteStreamSync', function() {
describe('> when the file and directory does not exist', function() {
it('should create the stream', function(done) {
var file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
stream = fse.getWriteStreamSync(file)
stream.end('hi jp', 'utf8', function(){
assert(fs.existsSync(file))
assert.equal(fs.readFileSync(file, 'utf8'), 'hi jp')
done()
})
})
})

describe('> when the file does exist', function() {
it('should still modify the file', function(done) {
var file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world')
stream = fse.getWriteStreamSync(file)
stream.end('hi jp', 'utf8', function(){
assert(fs.existsSync(file))
assert.equal(fs.readFileSync(file, 'utf8'), 'hi jp')
done()
})
})
})
})
})

0 comments on commit 2dbee57

Please sign in to comment.