mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 21:25:26 +00:00
Removed errors results as JSON, moved to exception throwing; Changed error messages
This commit is contained in:
144
index.js
144
index.js
@@ -31,23 +31,19 @@ class Pass {
|
|||||||
|
|
||||||
return this._parseSettings(this.options)
|
return this._parseSettings(this.options)
|
||||||
.then(() => readdir(this.model))
|
.then(() => readdir(this.model))
|
||||||
.catch(() => Promise.reject({
|
.catch((err) => {
|
||||||
status: false,
|
if (err.code && err.code === "ENOENT") {
|
||||||
error: {
|
throw new Error(`Model ${this.model ? this.model+" " : ""}not found. Provide a valid one to continue`);
|
||||||
message: `Model ${this.model} not found. Provide a valid one to continue`
|
|
||||||
}
|
}
|
||||||
}))
|
|
||||||
|
throw new Error(err);
|
||||||
|
})
|
||||||
.then(files => {
|
.then(files => {
|
||||||
// list without dynamic components like manifest, signature or pass files (will be added later in the flow) and hidden 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));
|
let noDynList = removeHidden(files).filter(f => !/(manifest|signature|pass)/i.test(f));
|
||||||
|
|
||||||
if (!noDynList.length) {
|
if (!noDynList.length) {
|
||||||
return Promise.reject({
|
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.`);
|
||||||
status: false,
|
|
||||||
error: {
|
|
||||||
message: "Model provided 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)
|
// 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"))
|
return readFile(path.resolve(this.model, "pass.json"))
|
||||||
.then(passStructBuffer => {
|
.then(passStructBuffer => {
|
||||||
if (!this._validateType(passStructBuffer)) {
|
if (!this._validateType(passStructBuffer)) {
|
||||||
return Promise.reject({
|
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.`)
|
||||||
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.`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bundle.push("pass.json");
|
bundle.push("pass.json");
|
||||||
|
|
||||||
return this._patch(this._filterOptions(this.overrides), passStructBuffer);
|
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);
|
archive.pipe(passStream);
|
||||||
|
|
||||||
return archive.finalize().then(() => {
|
return archive.finalize().then(() => passStream);
|
||||||
return {
|
|
||||||
status: true,
|
|
||||||
content: passStream,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,55 +209,53 @@ class Pass {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
_patch(options, passBuffer) {
|
_patch(options, passBuffer) {
|
||||||
return new Promise((resolve, reject) => {
|
if (!options) {
|
||||||
if (!options) {
|
return Promise.resolve(passBuffer);
|
||||||
return 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"));
|
passFile["barcodes"].forEach((b,i) => {
|
||||||
|
if (!schema.isValid(b, schema.constants.barcode) && !!options.barcode && b.message !== "") {
|
||||||
// "barcodes" support got introduced in iOS 9 as array of barcode.
|
passFile["barcodes"].splice(i, 1);
|
||||||
// "barcode" is still used in older iOS versions
|
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");
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
// options.barcode may not be defined
|
// 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) {
|
delete options["barcode"];
|
||||||
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) => {
|
Object.assign(passFile, options);
|
||||||
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"];
|
return Promise.resolve(Buffer.from(JSON.stringify(passFile)));
|
||||||
|
|
||||||
Object.assign(passFile, options);
|
|
||||||
|
|
||||||
return resolve(Buffer.from(JSON.stringify(passFile)));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -313,23 +288,12 @@ class Pass {
|
|||||||
|
|
||||||
_parseSettings(options) {
|
_parseSettings(options) {
|
||||||
if (!schema.isValid(options, schema.constants.instance)) {
|
if (!schema.isValid(options, schema.constants.instance)) {
|
||||||
return Promise.reject({
|
return Promise.reject("The options passed to Pass constructor does not meet the requirements. Refer to the documentation to compile them correctly.");
|
||||||
status: false,
|
|
||||||
error: {
|
|
||||||
message: "The options passed to Pass constructor does not meet the requirements. Refer to the documentation to compile them correctly."
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((success, reject) => {
|
return new Promise((success, reject) => {
|
||||||
if (!options.model || typeof options.model !== "string") {
|
if (!options.model || typeof options.model !== "string") {
|
||||||
return reject({
|
return reject("A string model name must be provided in order to continue.");
|
||||||
status: false,
|
|
||||||
error: {
|
|
||||||
message: "A string model name must be provided in order to continue.",
|
|
||||||
ecode: 418
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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" : "");
|
||||||
@@ -346,13 +310,7 @@ class Pass {
|
|||||||
contents.forEach(file => {
|
contents.forEach(file => {
|
||||||
let pem = this.__parsePEM(file, options.certificates.signerKey.passphrase);
|
let pem = this.__parsePEM(file, options.certificates.signerKey.passphrase);
|
||||||
if (!pem) {
|
if (!pem) {
|
||||||
return reject({
|
return reject("Invalid certificates got loaded. Please provide WWDR certificates and developer signer certificate and key (with passphrase).")
|
||||||
status: false,
|
|
||||||
error: {
|
|
||||||
message: "Invalid certificates got loaded. Please provide WWDR certificates and developer signer certificate and key (with passphrase).",
|
|
||||||
ecode: 418
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.Certificates[pem.key] = pem.value;
|
this.Certificates[pem.key] = pem.value;
|
||||||
|
|||||||
Reference in New Issue
Block a user