From fcb29b3d687982dea320e8a9cc1026eb20778901 Mon Sep 17 00:00:00 2001 From: alexandercerutti Date: Sun, 19 Aug 2018 14:23:11 +0200 Subject: [PATCH] Replaces this.overrides with this.props; all the properties inserted into overrides now get filtered through _parseSettings and pushed in there; _patch method now accepts only the pass buffer and not anymore the options; --- index.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 381c5d3..c76a875 100644 --- a/index.js +++ b/index.js @@ -15,10 +15,10 @@ const readFile = util.promisify(fs.readFile); class Pass { constructor(options) { this.options = options; - this.overrides = this.options.overrides || {}; this.Certificates = {}; this.model = ""; this.l10n = {}; + this.props = {}; fields.areas.forEach(a => this[a] = new fields.FieldsArea()); } @@ -66,7 +66,7 @@ class Pass { bundle.push("pass.json"); - return this._patch(this._filterOptions(this.overrides), passStructBuffer); + return this._patch(passStructBuffer); }); }); @@ -257,8 +257,8 @@ class Pass { * @returns {Promise} Edited pass.json buffer or Object containing error. */ - _patch(options, passBuffer) { - if (!options) { + _patch(passBuffer) { + if (!Object.keys(this.props).length) { return Promise.resolve(passBuffer); } @@ -270,12 +270,12 @@ class Pass { if (passFile["barcode"]) { let barcode = passFile["barcode"]; - if (!(barcode instanceof Object) || !schema.isValid(barcode, schema.constants.barcode) || !options.barcode && barcode.message === "") { + if (!(barcode instanceof Object) || !schema.isValid(barcode, schema.constants.barcode) || !this.props.barcode && barcode.message === "") { console.log("\x1b[41m", `Barcode syntax of the chosen model (${path.parse(this.model).base}) is not correct and got removed or the override content was not provided. Please refer to https://apple.co/2myAbst.`, "\x1b[0m"); delete passFile["barcode"]; } else { // options.barcode may not be defined - passFile["barcode"].message = options.barcode || passFile["barcode"].message; + passFile["barcode"].message = this.props.barcode || passFile["barcode"].message; } } else { console.log("\x1b[33m", `Your pass model (${path.parse(this.model).base}) is not compatible with iOS versions lower than iOS 9. Please refer to https://apple.co/2O5K65k to make it backward-compatible.`, "\x1b[0m"); @@ -288,21 +288,22 @@ class Pass { } passFile["barcodes"].forEach((b,i) => { - if (!schema.isValid(b, schema.constants.barcode) && !!options.barcode && b.message !== "") { + if (!schema.isValid(b, schema.constants.barcode) && !!this.props.barcode && b.message !== "") { passFile["barcodes"].splice(i, 1); console.log("\x1b[41m", `Barcode @ index ${i} of the chosen model (${path.parse(this.model).base}) is not well-formed or have syntax errors and got removed. Please refer to https://apple.co/2myAbst.`, "\x1b[0m"); } else { // options.barcode may not be defined - b.message = options.barcode || b.message; + b.message = this.props.barcode || b.message; } }); } else { console.log("\x1b[33m", `Your pass model (${path.parse(this.model).base}) is not compatible with iOS versions greater than iOS 8. Refer to https://apple.co/2O5K65k to make it forward-compatible.`, "\x1b[0m"); } - delete options["barcode"]; + delete this.props["barcode"]; - Object.assign(passFile, options); + // Merging the original and the new properties + Object.assign(passFile, this.props); fields.areas.forEach(area => { if (this[area].fields.length) { @@ -352,6 +353,7 @@ class Pass { } this.model = path.resolve(options.model) + (!!options.model && !path.extname(options.model) ? ".pass" : ""); + this.props = this._filterOptions(options.overrides); let certPaths = Object.keys(options.certificates) .filter(v => v !== "dir")