Skip to content

Instantly share code, notes, and snippets.

@pfgithub
Created October 16, 2018 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfgithub/20f98c096d71dc0fbc0f690e07af9bfe to your computer and use it in GitHub Desktop.
Save pfgithub/20f98c096d71dc0fbc0f690e07af9bfe to your computer and use it in GitHub Desktop.
simple plist thing
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