Created
October 16, 2018 01:32
-
-
Save pfgithub/20f98c096d71dc0fbc0f690e07af9bfe to your computer and use it in GitHub Desktop.
simple plist thing
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
var bplistCreator, bplistParser, plist; | |
bplistParser = require('bplist-parser'); | |
bplistCreator = require('bplist-creator'); | |
plist = require('plist'); | |
// 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 = function(contents) { | |
if (contents.length === 0) { | |
return {}; | |
} | |
return exports.parse(contents, aFile); | |
}; | |
exports.writeFileSync = function(anObject) { | |
var data; | |
data = plist.build(anObject); | |
return data; | |
}; | |
exports.writeBinaryFileSync = function(anObject) { | |
var data; | |
data = bplistCreator(anObject); | |
return data; | |
}; | |
exports.stringify = function(anObject) { | |
return plist.build(anObject); | |
}; | |
exports.parse = function(aStringOrBuffer, aFile) { | |
var e, firstByte, results; | |
firstByte = aStringOrBuffer[0]; | |
try { | |
if (firstByte === 60 || firstByte === '<') { | |
results = plist.parse(aStringOrBuffer.toString()); | |
} else if (firstByte === 98) { | |
results = bplistParser.parseBuffer(aStringOrBuffer)[0]; | |
} else { | |
if (aFile != null) { | |
throw new Error(`Unable to determine format for '${aFile}'`); | |
} else { | |
throw new Error("Unable to determine format for plist aStringOrBuffer"); | |
} | |
results = {}; | |
} | |
} catch (error) { | |
e = error; | |
throw new Error(`${aFile} has errors`); | |
} | |
return results; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment