mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 19:25:23 +00:00
Improved PKPass.from
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import FieldsArray from "./fieldsArray";
|
import FieldsArray from "./fieldsArray";
|
||||||
import { Certificates } from "./schemas";
|
|
||||||
import { default as Bundle, filesSymbol } from "./Bundle";
|
import { default as Bundle, filesSymbol } from "./Bundle";
|
||||||
import { getModelFolderContents } from "./parser";
|
import { getModelFolderContents } from "./parser";
|
||||||
import * as Schemas from "./schemas";
|
import * as Schemas from "./schemas";
|
||||||
@@ -16,6 +15,14 @@ interface NamedBuffers {
|
|||||||
[key: string]: Buffer;
|
[key: string]: Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace PKPass {
|
||||||
|
export interface Template {
|
||||||
|
model: string;
|
||||||
|
certificates: Schemas.Certificates;
|
||||||
|
overrides?: Schemas.OverridesSupportedOptions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type TransitTypes = `PKTransitType${
|
type TransitTypes = `PKTransitType${
|
||||||
| "Air"
|
| "Air"
|
||||||
| "Boat"
|
| "Boat"
|
||||||
@@ -26,7 +33,7 @@ type TransitTypes = `PKTransitType${
|
|||||||
const LOCALIZED_FILE_REGEX_BASE = "(?<lang>[a-zA-Z-]{2,}).lproj/";
|
const LOCALIZED_FILE_REGEX_BASE = "(?<lang>[a-zA-Z-]{2,}).lproj/";
|
||||||
|
|
||||||
export default class PKPass extends Bundle {
|
export default class PKPass extends Bundle {
|
||||||
private certificates: Certificates;
|
private certificates: Schemas.Certificates;
|
||||||
private [fieldKeysPoolSymbol] = new Set<string>();
|
private [fieldKeysPoolSymbol] = new Set<string>();
|
||||||
private [propsSymbol]: Schemas.ValidPass = {};
|
private [propsSymbol]: Schemas.ValidPass = {};
|
||||||
private [localizationSymbol]: {
|
private [localizationSymbol]: {
|
||||||
@@ -44,10 +51,16 @@ export default class PKPass extends Bundle {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static async from(source: PKPass | string): Promise<PKPass> {
|
static async from(source: PKPass | PKPass.Template): Promise<PKPass> {
|
||||||
let certificates: Certificates = undefined;
|
let certificates: Schemas.Certificates = undefined;
|
||||||
let buffers: NamedBuffers = undefined;
|
let buffers: NamedBuffers = undefined;
|
||||||
|
|
||||||
|
if (!source) {
|
||||||
|
throw new TypeError(
|
||||||
|
`Cannot create PKPass from source: source is '${source}'`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (source instanceof PKPass) {
|
if (source instanceof PKPass) {
|
||||||
/** Cloning is happening here */
|
/** Cloning is happening here */
|
||||||
certificates = source.certificates;
|
certificates = source.certificates;
|
||||||
@@ -63,6 +76,12 @@ export default class PKPass extends Bundle {
|
|||||||
contentBuffer.copy(buffers[fileName]);
|
contentBuffer.copy(buffers[fileName]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (!source.model || typeof source.model !== "string") {
|
||||||
|
throw new TypeError(
|
||||||
|
"Cannot create PKPass from source: unknown model but expected a string.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/** Disk model reading is happening here */
|
/** Disk model reading is happening here */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,12 +89,10 @@ export default class PKPass extends Bundle {
|
|||||||
* @TODO determine how to use localized files
|
* @TODO determine how to use localized files
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const { bundle, l10nBundle } = await getModelFolderContents(source);
|
buffers = await getModelFolderContents(source.model);
|
||||||
|
|
||||||
buffers = bundle;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PKPass(buffers, certificates);
|
return new PKPass(buffers, certificates, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user