Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Pass options to fs.writeFile and fs.writeFileSync
Browse files Browse the repository at this point in the history
  • Loading branch information
smashwilson committed Sep 11, 2017
1 parent 74235eb commit df2ab4e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
28 changes: 24 additions & 4 deletions spec/cson-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
path = require 'path'
fs = require 'fs'
fs = require 'fs-plus'
temp = require 'temp'
CSON = require '../lib/cson'
parser = require 'cson-parser'
Expand Down Expand Up @@ -458,12 +458,32 @@ describe "CSON", ->

callback(null, "{}")

CSON.readFile("/bar/blarg.cson", {encoding: 'cuneiform', allowDuplicateKeys: false}, callback)
cb = jasmine.createSpy 'callback'
CSON.readFile("/bar/blarg.cson", {encoding: 'cuneiform', allowDuplicateKeys: false}, cb)

expect(fs.readFile).toHaveBeenCalled()
expect(parser.parse.calls[0].args[0]).toEqual "{}"
expect(typeof parser.parse.calls[0].args[1]).toEqual "function"
expect(cb).toHaveBeenCalledWith null, {}

it "passes options to the writeFileSync call"
it "passes options to the writeFileSync call", ->
spyOn(fs, 'writeFileSync').andCallFake (filePath, payload, fileOptions) ->
expect(filePath).toEqual "/stuff/wat.cson"
expect(fileOptions).toEqual {mode: 0o755}

it "passes options to the writeFile call"
CSON.writeFileSync("/stuff/wat.cson", {data: 'yep'}, {mode: 0o755})

expect(fs.writeFileSync).toHaveBeenCalled()
expect(fs.writeFileSync.calls[0].args[2]).toEqual {mode: 0o755}

it "passes options to the writeFile call", ->
spyOn(fs, 'writeFile').andCallFake (filePath, payload, fileOptions, callback) ->
expect(filePath).toEqual "/eh/stuff.cson"
expect(fileOptions).toEqual {flag: 'x'}
callback(null)

cb = jasmine.createSpy 'callback'
CSON.writeFile("/eh/stuff.cson", {}, {flag: 'x'}, cb)

expect(fs.writeFile).toHaveBeenCalled()
expect(cb).toHaveBeenCalledWith null
11 changes: 7 additions & 4 deletions src/cson.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ module.exports =
else
parseContents(objectPath, null, contents, parseOptions, callback)

writeFile: (objectPath, object, callback) ->
writeFile: (objectPath, object, options, callback) ->
if arguments.length < 4
callback = options
options = {}
callback ?= ->

try
Expand All @@ -156,10 +159,10 @@ module.exports =
callback(error)
return

fs.writeFile(objectPath, "#{contents}\n", callback)
fs.writeFile(objectPath, "#{contents}\n", options, callback)

writeFileSync: (objectPath, object) ->
fs.writeFileSync(objectPath, "#{@stringifyPath(objectPath, object)}\n")
writeFileSync: (objectPath, object, options = undefined) ->
fs.writeFileSync(objectPath, "#{@stringifyPath(objectPath, object)}\n", options)

stringifyPath: (objectPath, object, visitor, space) ->
if path.extname(objectPath) is '.cson'
Expand Down

0 comments on commit df2ab4e

Please sign in to comment.