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;
This commit is contained in:
alexandercerutti
2018-08-19 14:23:11 +02:00
parent f065ab98cd
commit fcb29b3d68

View File

@@ -15,10 +15,10 @@ const readFile = util.promisify(fs.readFile);
class Pass { class Pass {
constructor(options) { constructor(options) {
this.options = options; this.options = options;
this.overrides = this.options.overrides || {};
this.Certificates = {}; this.Certificates = {};
this.model = ""; this.model = "";
this.l10n = {}; this.l10n = {};
this.props = {};
fields.areas.forEach(a => this[a] = new fields.FieldsArea()); fields.areas.forEach(a => this[a] = new fields.FieldsArea());
} }
@@ -66,7 +66,7 @@ class Pass {
bundle.push("pass.json"); bundle.push("pass.json");
return this._patch(this._filterOptions(this.overrides), passStructBuffer); return this._patch(passStructBuffer);
}); });
}); });
@@ -257,8 +257,8 @@ class Pass {
* @returns {Promise<Buffer>} Edited pass.json buffer or Object containing error. * @returns {Promise<Buffer>} Edited pass.json buffer or Object containing error.
*/ */
_patch(options, passBuffer) { _patch(passBuffer) {
if (!options) { if (!Object.keys(this.props).length) {
return Promise.resolve(passBuffer); return Promise.resolve(passBuffer);
} }
@@ -270,12 +270,12 @@ class Pass {
if (passFile["barcode"]) { if (passFile["barcode"]) {
let barcode = 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"); 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"]; delete passFile["barcode"];
} else { } else {
// options.barcode may not be defined // options.barcode may not be defined
passFile["barcode"].message = options.barcode || passFile["barcode"].message; passFile["barcode"].message = this.props.barcode || passFile["barcode"].message;
} }
} else { } 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"); 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) => { 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); 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"); 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 { } else {
// options.barcode may not be defined // options.barcode may not be defined
b.message = options.barcode || b.message; b.message = this.props.barcode || b.message;
} }
}); });
} else { } 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"); 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 => { fields.areas.forEach(area => {
if (this[area].fields.length) { 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.model = path.resolve(options.model) + (!!options.model && !path.extname(options.model) ? ".pass" : "");
this.props = this._filterOptions(options.overrides);
let certPaths = Object.keys(options.certificates) let certPaths = Object.keys(options.certificates)
.filter(v => v !== "dir") .filter(v => v !== "dir")