Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Wollard committed Nov 1, 2016
2 parents b4af18c + 4650bd2 commit 75382c3
Show file tree
Hide file tree
Showing 16 changed files with 2,411 additions and 124 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ logs
results

npm-debug.log
node_modules
.DS_Store
coverage
*.js
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "6"
12 changes: 12 additions & 0 deletions __tests__/catch-invalid-plist.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plist = require 'simple-plist'

it "Throws an error on improperly formatted plist", =>
doIt = ->
plist.readFileSync "#{__dirname}/test-xml1-invalid.plist"

expect(doIt).toThrow /has errors$/


it "returns an empty object when the file is zero bytes", =>
obj = plist.readFileSync "#{__dirname}/test-xml1-invalid-2.plist"
expect(obj).toEqual {}
8 changes: 8 additions & 0 deletions __tests__/parse.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plist = require 'simple-plist'

describe "String parsing", =>
it "can parse a string", =>
doc = plist.readFileSync "#{__dirname}/test-binary1.plist"
plistString = plist.stringify doc
parsedDoc = plist.parse(plistString)
expect(parsedDoc).toEqual(doc)
30 changes: 30 additions & 0 deletions __tests__/readFile-binary1.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
plist = require 'simple-plist'
filePath = "#{__dirname}/test-binary1.plist"

describe 'readFileSync can properly load and read a binary file', =>
contents = plist.readFileSync filePath
it "has the proper values", =>
expect(contents.Name).toBe "John Doe"
expect(contents['Birth Year']).toBe(1942)
expect(contents['Travel Log']).toEqual(
[
'Tokyo, Honshu, Japan'
'Philadelphia, PA'
'Recife, Pernambuco, Brazil'
]
)



describe "readFile works asynchronously", =>
it "has the proper values", =>
contents = plist.readFile filePath, (contents)=>
expect(contents.Name).toBe "John Doe"
expect(contents['Birth Year']).toBe(1942)
expect(contents['Travel Log']).toEqual(
[
'Tokyo, Honshu, Japan'
'Philadelphia, PA'
'Recife, Pernambuco, Brazil'
]
)
30 changes: 30 additions & 0 deletions __tests__/readFile-xml1.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
plist = require 'simple-plist'
filePath = "#{__dirname}/test-xml1.plist"

describe 'readFileSync can properly load and read a file', =>
contents = plist.readFileSync filePath
it "has the proper values", =>
expect(contents.Name).toBe "John Doe"
expect(contents['Birth Year']).toBe(1942)
expect(contents['Travel Log']).toEqual(
[
'Tokyo, Honshu, Japan'
'Philadelphia, PA'
'Recife, Pernambuco, Brazil'
]
)



describe "readFile works asynchronously", =>
it "has the proper values", =>
contents = plist.readFile filePath, (err, contents)=>
expect(contents.Name).toBe "John Doe"
expect(contents['Birth Year']).toBe(1942)
expect(contents['Travel Log']).toEqual(
[
'Tokyo, Honshu, Japan'
'Philadelphia, PA'
'Recife, Pernambuco, Brazil'
]
)
Binary file added __tests__/test-binary1.plist
Binary file not shown.
Empty file.
17 changes: 17 additions & 0 deletions __tests__/test-xml1-invalid.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
🙄<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Birth Year</key>
<integer>1942</integer>
<key>Name</key>
<string>John Doe</string>
<key>Travel Log</key>
<array>
<string>Tokyo, Honshu, Japan</string>
<string>Philadelphia, PA</string>
<string>Recife, Pernambuco, Brazil</string>
</array>
</dict>
</plist>
17 changes: 17 additions & 0 deletions __tests__/test-xml1.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Birth Year</key>
<integer>1942</integer>
<key>Name</key>
<string>John Doe</string>
<key>Travel Log</key>
<array>
<string>Tokyo, Honshu, Japan</string>
<string>Philadelphia, PA</string>
<string>Recife, Pernambuco, Brazil</string>
</array>
</dict>
</plist>
25 changes: 25 additions & 0 deletions __tests__/writeFile-binary1.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plist = require 'simple-plist'
filePath = "#{__dirname}/write-test-binary1.plist"
testObj = {
Name: 'John Doe'
"Birth Year": 1942
"Travel Log": [
'Tokyo, Honshu, Japan'
'Philadelphia, PA'
'Recife, Pernambuco, Brazil'
]
}


describe 'writeBinaryFileSync can properly load and read a file', =>
plist.writeBinaryFileSync filePath, testObj
it "has the proper values", =>
plist.readFile filePath, (contents)=>
expect(contents).toEqual testObj


describe 'writeBinaryFile works asynchronously', =>
it "has the proper values", =>
plist.writeBinaryFile filePath, testObj, =>
plist.readFile filePath, (contents)=>
expect(contents).toEqual testObj
25 changes: 25 additions & 0 deletions __tests__/writeFile-xml1.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plist = require 'simple-plist'
filePath = "#{__dirname}/write-test-xml1.plist"
testObj = {
Name: 'John Doe'
"Birth Year": 1942
"Travel Log": [
'Tokyo, Honshu, Japan'
'Philadelphia, PA'
'Recife, Pernambuco, Brazil'
]
}


describe 'writeFileSync can properly load and read a file', =>
plist.writeFileSync filePath, testObj
it "has the proper values", =>
plist.readFile filePath, (contents)=>
expect(contents).toEqual testObj


describe 'writeFile works asynchronously', =>
it "has the proper values", =>
plist.writeFile filePath, testObj, =>
plist.readFile filePath, (contents)=>
expect(contents).toEqual testObj
52 changes: 30 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
{
"name": "simple-plist",
"author": "Joe Wollard",
"license": "MIT",
"homepage": "https://github.com/wollardj/node-simple-plist.git",
"repository": {
"type": "git",
"url": "https://github.com/wollardj/node-simple-plist.git"
},
"version": "0.1.4",
"description": "A wrapper utility for interacting with plist data.",
"main": "simple-plist.js",
"keywords": [
"plist",
"binary",
"bplist",
"xml"
],
"dependencies": {
"plist": "1.2.0",
"bplist-parser": "0.0.6",
"bplist-creator": "0.0.4"
}
"name": "simple-plist",
"author": "Joe Wollard",
"license": "MIT",
"homepage": "https://github.com/wollardj/node-simple-plist.git",
"repository": {
"type": "git",
"url": "https://github.com/wollardj/node-simple-plist.git"
},
"version": "0.2.0",
"description": "A wrapper utility for interacting with plist data.",
"main": "simple-plist.js",
"keywords": [
"plist",
"binary",
"bplist",
"xml"
],
"scripts": {
"test": "npm run-script clean && ./node_modules/.bin/coffee -c . && ./node_modules/.bin/jest --coverage",
"clean": "rm -rf *.js __tests__/*.js __tests__/write-test* coverage"
},
"dependencies": {
"bplist-creator": "0.0.7",
"bplist-parser": "0.1.1",
"plist": "2.0.1"
},
"devDependencies": {
"coffee-script": "^1.11.1",
"jest": "^16.0.2"
}
}
80 changes: 80 additions & 0 deletions simple-plist.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
bplistParser = require 'bplist-parser'
bplistCreator = require 'bplist-creator'
plist = require 'plist'
fs = require 'fs'

# reveal the underlying modules
exports.plist = plist
exports.bplistCreator = bplistCreator
exports.bplistParser = bplistParser


# Parses the given file and returns its contents as a native JavaScript object.
exports.readFileSync = (aFile)->
contents = fs.readFileSync aFile

if contents.length is 0
return {}
exports.parse contents, aFile


exports.readFile = (aFile, callback)->
fs.readFile aFile, (err, contents)->
if err
callback err
else
try
results = exports.parse contents, aFile
callback null, results
catch err
callback err


exports.writeFileSync = (aFile, anObject, options)->
data = plist.build anObject
fs.writeFileSync aFile, data, options


exports.writeFile = (aFile, anObject, options, callback)->
if arguments.length is 3 and typeof options is 'function'
callback = options
options = undefined
data = plist.build anObject
fs.writeFile aFile, data, options, callback


exports.writeBinaryFileSync = (aFile, anObject, options)->
data = bplistCreator anObject
fs.writeFileSync aFile, data, options


exports.writeBinaryFile = (aFile, anObject, options, callback)->
if arguments.length is 3 and typeof options is 'function'
callback = options
options = undefined

data = bplistCreator anObject
fs.writeFile aFile, data, options, callback


exports.stringify = (anObject)->
plist.build anObject


exports.parse = (aStringOrBuffer, aFile)->
firstByte = aStringOrBuffer[0]
try
if firstByte in [60, '<']
results = plist.parse aStringOrBuffer.toString()
else if firstByte is 98
results = bplistParser.parseBuffer(aStringOrBuffer)[0]
else
if aFile?
throw new Error "Unable to determine format for '#{aFile}'"
else
throw new Error "Unable to determine format for plist aStringOrBuffer"
results = {}
catch e
throw new Error "#{aFile} has errors"

results

0 comments on commit 75382c3

Please sign in to comment.