diff --git a/index.js b/index.js index 55a6cbd..643de32 100644 --- a/index.js +++ b/index.js @@ -106,23 +106,22 @@ class Pass { }); } - this._patch(this._filterOptions(this.overrides), passStructBuffer) - .then(function _afterJSONParse(passFileBuffer) { - manifest["pass.json"] = forge.md.sha1.create().update(passFileBuffer.toString("binary")).digest().toHex(); - archive.append(passFileBuffer, { name: "pass.json" }); + try { + let patchedPass = this._patch(this._filterOptions(this.overrides), passStructBuffer); - // no errors happened - return passCallback(null); - }) - .catch(err => { - return passCallback({ - status: false, - error: { - message: `Unable to read pass.json as buffer @ ${this.model}. Unable to continue.\n${err}`, - ecode: 418 - } - }); + manifest["pass.json"] = forge.md.sha1.create().update(patchedPass.toString("binary")).digest().toHex(); + archive.append(patchedPass, { name: "pass.json" }); + + return passCallback(); + } catch (e) { + return passCallback({ + status: false, + error: { + message: `Unable to read pass.json as buffer @ ${this.model}. Unable to continue.\n${err}`, + ecode: 418 + } }); + } }); }); @@ -292,59 +291,54 @@ class Pass { */ _patch(options, passBuffer) { + if (!options) { - return Promise.resolve(passBuffer); + return passBuffer; } - return new Promise((success, reject) => { - try { - let passFile = JSON.parse(passBuffer.toString("utf8")); + let passFile = JSON.parse(passBuffer.toString("utf8")); - // "barcodes" support got introduced in iOS 9 as array of barcode. - // "barcode" is still used in older iOS versions + // "barcodes" support got introduced in iOS 9 as array of barcode. + // "barcode" is still used in older iOS versions - if (passFile["barcode"]) { - let barcode = passFile["barcode"]; + if (passFile["barcode"]) { + let barcode = passFile["barcode"]; - if (!(barcode instanceof Object) || !schema.isValid(barcode, schema.constants.barcode) || !options.barcode && barcode.message === "") { - console.log("\x1b[41m", `Barcode syntax of the chosen model (${path.parse(this.model).base}) is not correct or the override content was not provided got removed. 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; - } - } 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"); - } - - if (passFile["barcodes"] && passFile["barcodes"] instanceof Array) { - if (!passFile["barcodes"].length) { - console.log("\x1b[33m", `No barcodes support specified. The element got removed.`, "\x1b[0m"); - delete passFile["barcodes"]; - } - - passFile["barcodes"].forEach((b,i) => { - if (!schema.isValid(b, schema.constants.barcode) && !!options.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; - } - }); - } 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"]; - - Object.assign(passFile, options); - - return success(Buffer.from(JSON.stringify(passFile))); - } catch(e) { - return reject(e); + if (!(barcode instanceof Object) || !schema.isValid(barcode, schema.constants.barcode) || !options.barcode && barcode.message === "") { + console.log("\x1b[41m", `Barcode syntax of the chosen model (${path.parse(this.model).base}) is not correct or the override content was not provided got removed. 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; } - }); + } 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"); + } + + if (passFile["barcodes"] && passFile["barcodes"] instanceof Array) { + if (!passFile["barcodes"].length) { + console.log("\x1b[33m", `No barcodes support specified. The element got removed.`, "\x1b[0m"); + delete passFile["barcodes"]; + } + + passFile["barcodes"].forEach((b,i) => { + if (!schema.isValid(b, schema.constants.barcode) && !!options.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; + } + }); + } 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"]; + + Object.assign(passFile, options); + + return Buffer.from(JSON.stringify(passFile)); } /** @@ -386,7 +380,6 @@ class Pass { } return new Promise((success, reject) => { - if (!options.model || typeof options.model !== "string") { return reject({ status: false,