-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c062045
commit 2dbee57
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |