diff --git a/index.js b/index.js index 7ae4c3c..47187dc 100644 --- a/index.js +++ b/index.js @@ -31,23 +31,19 @@ class Pass { return this._parseSettings(this.options) .then(() => readdir(this.model)) - .catch(() => Promise.reject({ - status: false, - error: { - message: `Model ${this.model} not found. Provide a valid one to continue` + .catch((err) => { + if (err.code && err.code === "ENOENT") { + throw new Error(`Model ${this.model ? this.model+" " : ""}not found. Provide a valid one to continue`); } - })) + + throw new Error(err); + }) .then(files => { // list without dynamic components like manifest, signature or pass files (will be added later in the flow) and hidden files. let noDynList = removeHidden(files).filter(f => !/(manifest|signature|pass)/i.test(f)); if (!noDynList.length) { - return Promise.reject({ - status: false, - error: { - message: "Model provided matched but unitialized. Refer to https://apple.co/2IhJr0Q and documentation to fill the model correctly." - } - }); + throw new Error(`Provided model (${path.parse(this.model).name}) matched but unitialized. Refer to https://apple.co/2IhJr0Q and documentation to fill the model correctly.`); } // list without localization files (they will be added later in the flow) @@ -65,26 +61,12 @@ class Pass { return readFile(path.resolve(this.model, "pass.json")) .then(passStructBuffer => { if (!this._validateType(passStructBuffer)) { - return Promise.reject({ - status: false, - error: { - message: `Unable to validate pass type or pass file is not a valid buffer. Check the syntax of your pass.json file or refer to https://apple.co/2Nvshvn to use a valid type.` - } - }); + throw new Error(`Unable to validate pass type or pass file is not a valid buffer. Check the syntax of your pass.json file or refer to https://apple.co/2Nvshvn to use a valid type.`) } bundle.push("pass.json"); return this._patch(this._filterOptions(this.overrides), passStructBuffer); - }) - .catch(err => { - console.log(err); - return Promise.reject({ - status: false, - error: { - message: `Unable to validate pass type or pass file is not a valid buffer. Check the syntax of your pass.json file or refer to https://apple.co/2Nvshvn to use a valid type.` - } - }) }); }); @@ -126,12 +108,7 @@ class Pass { archive.pipe(passStream); - return archive.finalize().then(() => { - return { - status: true, - content: passStream, - }; - }); + return archive.finalize().then(() => passStream); }); } @@ -232,55 +209,53 @@ class Pass { */ _patch(options, passBuffer) { - return new Promise((resolve, reject) => { - if (!options) { - return resolve(passBuffer); + if (!options) { + return Promise.resolve(passBuffer); + } + + 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 + + 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 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; + } + } 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"]; } - 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 - - 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 and got removed or the override content was not provided. Please refer to https://apple.co/2myAbst.`, "\x1b[0m"); - delete passFile["barcode"]; + 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 - passFile["barcode"].message = options.barcode || passFile["barcode"].message; + 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 lower than iOS 9. Please refer to https://apple.co/2O5K65k to make it backward-compatible.`, "\x1b[0m"); - } + }); + } 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"); + } - 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"]; - } + delete options["barcode"]; - 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"); - } + Object.assign(passFile, options); - delete options["barcode"]; - - Object.assign(passFile, options); - - return resolve(Buffer.from(JSON.stringify(passFile))); - }); + return Promise.resolve(Buffer.from(JSON.stringify(passFile))); } /** @@ -313,23 +288,12 @@ class Pass { _parseSettings(options) { if (!schema.isValid(options, schema.constants.instance)) { - return Promise.reject({ - status: false, - error: { - message: "The options passed to Pass constructor does not meet the requirements. Refer to the documentation to compile them correctly." - } - }); + return Promise.reject("The options passed to Pass constructor does not meet the requirements. Refer to the documentation to compile them correctly."); } return new Promise((success, reject) => { if (!options.model || typeof options.model !== "string") { - return reject({ - status: false, - error: { - message: "A string model name must be provided in order to continue.", - ecode: 418 - } - }); + return reject("A string model name must be provided in order to continue."); } this.model = path.resolve(options.model) + (!!options.model && !path.extname(options.model) ? ".pass" : ""); @@ -346,13 +310,7 @@ class Pass { contents.forEach(file => { let pem = this.__parsePEM(file, options.certificates.signerKey.passphrase); if (!pem) { - return reject({ - status: false, - error: { - message: "Invalid certificates got loaded. Please provide WWDR certificates and developer signer certificate and key (with passphrase).", - ecode: 418 - } - }) + return reject("Invalid certificates got loaded. Please provide WWDR certificates and developer signer certificate and key (with passphrase).") } this.Certificates[pem.key] = pem.value;