Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds basic (string, buffer) support for ensureFile content #166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions lib/ensure/__tests__/ensure-content.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
var assert = require('assert')
var util = require('util')
var path = require('path')
var os = require('os')
var fs = require('graceful-fs')
var CWD = process.cwd()
var fse = require(CWD)
var ensureFile = fse.ensureFile
var ensureFileSync = fse.ensureFileSync

/* global afterEach, describe, it, after, before */

var TEST_DIR

describe('fse-ensure-file', function () {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')

var tests = [
// [[srcpath, dstpath], fs.writeFile expect, ensureFile expect]
[['./foo.txt', 'foo'], 'file-success', 'file-success']
]

before(function () {
fse.emptyDirSync(TEST_DIR)
process.chdir(TEST_DIR)
})

// beforeEach(function () {
// fs.writeFileSync('./foo.txt', 'foo\n')
// fse.mkdirsSync('empty-dir')
// fse.mkdirsSync('dir-foo')
// fs.writeFileSync('dir-foo/foo.txt', 'dir-foo\n')
// fse.mkdirsSync('dir-bar')
// fs.writeFileSync('dir-bar/bar.txt', 'dir-bar\n')
// fse.mkdirsSync('real-alpha/real-beta/real-gamma')
// })

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

after(function () {
process.chdir(CWD)
fse.removeSync(TEST_DIR)
})

function fileSuccess (args, fn) {
var dstpath = args[0]
var content = args[1]
var should = util.format('should create file `%s` with content `%s`', dstpath, content)
it(should, function (done) {
var callback = function (err) {
if (err) return done(err)
var fileContent = fs.readFileSync(dstpath, 'utf8')
assert.equal(content, fileContent)
return done()
}
args.push(callback)
return fn.apply(null, args)
})
}

function fileSuccessSync (args, fn) {
var dstpath = args[0]
var content = args[1]
var should = util.format('should create file `%s` with content `%s`', dstpath, content)
it(should, function () {
fn.apply(null, args)
var fileContent = fs.readFileSync(dstpath, 'utf8')
assert.equal(content, fileContent)
})
}

describe('fs.writeFile()', function () {
var fn = fs.writeFile
tests.forEach(function (test) {
var args = test[0].slice(0)
var nativeBehavior = test[1]
// var newBehavior = test[2]
if (nativeBehavior === 'file-success') fileSuccess(args, fn)
// if (nativeBehavior === 'file-error') fileError(args, fn)
})
})

describe('ensureFile()', function () {
var fn = ensureFile
tests.forEach(function (test) {
var args = test[0].slice(0)
// var nativeBehavior = test[1]
var newBehavior = test[2]
if (newBehavior === 'file-success') fileSuccess(args, fn)
// if (newBehavior === 'file-error') fileError(args, fn)
})
})

describe('fs.writeFileSync()', function () {
var fn = fs.writeFileSync
tests.forEach(function (test) {
var args = test[0].slice(0)
var nativeBehavior = test[1]
// var newBehavior = test[2]
if (nativeBehavior === 'file-success') fileSuccessSync(args, fn)
// if (nativeBehavior === 'file-error') fileErrorSync(args, fn)
})
})

describe('ensureFileSync()', function () {
var fn = ensureFileSync
tests.forEach(function (test) {
var args = test[0].slice(0)
// var nativeBehavior = test[1]
var newBehavior = test[2]
if (newBehavior === 'file-success') fileSuccessSync(args, fn)
// if (newBehavior === 'file-error') fileErrorSync(args, fn)
})
})

})
13 changes: 9 additions & 4 deletions lib/ensure/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ var path = require('path')
var fs = require('graceful-fs')
var mkdir = require('../mkdirs')

function createFile (file, callback) {
function createFile (file, content, callback) {
callback = (typeof content === 'function') ? content : callback
content = (typeof content === 'function') ? '' : content

function makeFile () {
fs.writeFile(file, '', function (err) {
fs.writeFile(file, content, function (err) {
if (err) return callback(err)
callback()
})
Expand All @@ -23,15 +26,17 @@ function createFile (file, callback) {
})
}

function createFileSync (file) {
function createFileSync (file, content) {
content = (typeof content === 'function') ? '' : content

if (fs.existsSync(file)) return

var dir = path.dirname(file)
if (!fs.existsSync(dir)) {
mkdir.mkdirsSync(dir)
}

fs.writeFileSync(file, '')
fs.writeFileSync(file, content)
}

module.exports = {
Expand Down