Skip to content

Commit

Permalink
Merge pull request #118 from charlierudolph/cr-createWriteStreamP
Browse files Browse the repository at this point in the history
createOutputStream
  • Loading branch information
jprichardson committed Mar 27, 2015
2 parents c896505 + af6402c commit 3d9c5ae
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/create-output-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 createOutputStream (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 createOutputStreamSync (file, options) {
var dir = path.dirname(file)
if (!fs.existsSync(dir)) {
mkdir.mkdirsSync(dir)
}
return fs.createWriteStream(file, options)
}

module.exports = {
createOutputStream: createOutputStream,
createOutputStreamSync: createOutputStreamSync
}
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 createOutputStream = require('./create-output-stream')
fs.createOutputStream = createOutputStream.createOutputStream
fs.createOutputStreamSync = createOutputStream.createOutputStreamSync

var move = require('./move')
fs.move = function(src, dest, opts, callback) {
Expand Down
81 changes: 81 additions & 0 deletions test/create-output-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('createOutputStream', function () {
beforeEach(function() {
TEST_DIR = testutil.createTestDir('fs-extra')
})

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

describe('+ createOutputStream', 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.createOutputStream(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.createOutputStream(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('+ createOutputStreamSync', 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.createOutputStreamSync(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.createOutputStreamSync(file)
stream.end('hi jp', 'utf8', function(){
assert(fs.existsSync(file))
assert.equal(fs.readFileSync(file, 'utf8'), 'hi jp')
done()
})
})
})
})
})

0 comments on commit 3d9c5ae

Please sign in to comment.