@appium/support
Version:
Support libs used across Appium packages
171 lines • 5.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parsePlistFile = parsePlistFile;
exports.updatePlistFile = updatePlistFile;
exports.createBinaryPlist = createBinaryPlist;
exports.parseBinaryPlist = parseBinaryPlist;
exports.createPlist = createPlist;
exports.parsePlist = parsePlist;
const plist_1 = __importDefault(require("plist"));
const bplist_creator_1 = __importDefault(require("bplist-creator"));
const bplist_parser_1 = require("bplist-parser");
const fs_1 = require("./fs");
const logger_1 = __importDefault(require("./logger"));
const lodash_1 = __importDefault(require("lodash"));
const BPLIST_IDENTIFIER = {
BUFFER: Buffer.from('bplist00'),
TEXT: 'bplist00',
};
const PLIST_IDENTIFIER = {
BUFFER: Buffer.from('<'),
TEXT: '<',
};
/**
* Parses a file in xml or binary format of plist
*
* @param plist - The plist file path
* @param mustExist - If set to false, this method will return an empty object when the file doesn't exist
* @param quiet - If set to false, the plist path will be logged in debug level
* @returns Parsed plist as a JS object
*/
async function parsePlistFile(plist, mustExist = true, quiet = true) {
if (!(await fs_1.fs.exists(plist))) {
if (mustExist) {
throw logger_1.default.errorWithException(`Plist file doesn't exist: '${plist}'`);
}
if (!quiet) {
logger_1.default.debug(`Plist file '${plist}' does not exist. Returning an empty plist.`);
}
return {};
}
let obj = {};
let type = 'binary';
try {
const parsed = await (0, bplist_parser_1.parseFile)(plist);
if (parsed.length) {
obj = parsed[0];
}
else {
throw new Error(`Binary file '${plist}' appears to be empty`);
}
}
catch {
try {
obj = await parseXmlPlistFile(plist);
type = 'xml';
}
catch (err) {
throw logger_1.default.errorWithException(`Could not parse plist file '${plist}' as XML: ${err.message}`);
}
}
if (!quiet) {
logger_1.default.debug(`Parsed plist file '${plist}' as ${type}`);
}
return obj;
}
/**
* Updates a plist file with the given fields
*
* @param plist - The plist file path
* @param updatedFields - The updated fields-value pairs
* @param binary - If set to false, the file will be created as a xml plist
* @param mustExist - If set to false, this method will update an empty plist
* @param quiet - If set to false, the plist path will be logged in debug level
*/
async function updatePlistFile(plist, updatedFields, binary = true, mustExist = true, quiet = true) {
let obj;
try {
obj = await parsePlistFile(plist, mustExist);
}
catch (err) {
throw logger_1.default.errorWithException(`Could not update plist: ${err.message}`);
}
lodash_1.default.extend(obj, updatedFields);
const newPlist = binary ? (0, bplist_creator_1.default)(obj) : plist_1.default.build(obj);
try {
await fs_1.fs.writeFile(plist, newPlist);
}
catch (err) {
throw logger_1.default.errorWithException(`Could not save plist: ${err.message}`);
}
if (!quiet) {
logger_1.default.debug(`Wrote plist file '${plist}'`);
}
}
/**
* Creates a binary plist Buffer from an object
*
* @param data - The object to be turned into a binary plist
* @returns Plist in the form of a binary buffer
*/
function createBinaryPlist(data) {
return (0, bplist_creator_1.default)(data);
}
/**
* Parses a Buffer into an Object
*
* @param data - The buffer of a binary plist
* @returns Array of parsed root objects (typically one element)
*/
function parseBinaryPlist(data) {
return (0, bplist_parser_1.parseBuffer)(data);
}
/**
* Creates a plist from an object
*
* @param object - The JS object to be turned into a plist
* @param binary - Set it to true for a binary plist
* @returns A buffer or a string depending on the binary parameter
*/
function createPlist(object, binary = false) {
if (binary) {
return createBinaryPlist(object);
}
return plist_1.default.build(object);
}
/**
* Parses a buffer or string into a JS object
*
* @param data - The plist as a string or Buffer
* @returns Parsed plist as a JS object
* @throws Will throw an error if the plist type is unknown
*/
function parsePlist(data) {
const textPlist = getXmlPlist(data);
if (textPlist) {
return plist_1.default.parse(textPlist);
}
const binaryPlist = getBinaryPlist(data);
if (binaryPlist) {
return parseBinaryPlist(binaryPlist)[0];
}
throw new Error(`Unknown type of plist, data: ${lodash_1.default.truncate(data.toString(), { length: 200 })}`);
}
async function parseXmlPlistFile(plistFilename) {
const xmlContent = await fs_1.fs.readFile(plistFilename, 'utf8');
return plist_1.default.parse(xmlContent);
}
function getXmlPlist(data) {
if (lodash_1.default.isString(data) && data.startsWith(PLIST_IDENTIFIER.TEXT)) {
return data;
}
if (Buffer.isBuffer(data) &&
PLIST_IDENTIFIER.BUFFER.compare(data, 0, PLIST_IDENTIFIER.BUFFER.length) === 0) {
return data.toString();
}
return null;
}
function getBinaryPlist(data) {
if (lodash_1.default.isString(data) && data.startsWith(BPLIST_IDENTIFIER.TEXT)) {
return Buffer.from(data);
}
if (Buffer.isBuffer(data) &&
BPLIST_IDENTIFIER.BUFFER.compare(data, 0, BPLIST_IDENTIFIER.BUFFER.length) === 0) {
return data;
}
return null;
}
//# sourceMappingURL=plist.js.map