mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 16:25:21 +00:00
Changed generate method: removed
This commit is contained in:
127
src/pass.ts
127
src/pass.ts
@@ -88,89 +88,49 @@ export class Pass implements PassIndexSignature {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
async generate(): Promise<Stream> {
|
async generate(): Promise<Stream> {
|
||||||
try {
|
// Editing Pass.json
|
||||||
// Reading the model
|
|
||||||
const modelFilesList = await readdir(this.model);
|
|
||||||
|
|
||||||
// list without dynamic components like manifest, signature or pass files (will be added later in the flow) and hidden files.
|
this.bundle["pass.json"] = this._patch(this.bundle["pass.json"]);
|
||||||
const noDynList = removeHidden(modelFilesList).filter(f => !/(manifest|signature|pass)/i.test(f));
|
|
||||||
|
|
||||||
if (!noDynList.length || !noDynList.some(f => f.toLowerCase().includes("icon"))) {
|
const finalBundle = { ...this.bundle } as schema.BundleUnit;
|
||||||
let eMessage = formatMessage("MODEL_UNINITIALIZED", path.parse(this.model).name);
|
|
||||||
throw new Error(eMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
// list without localization files (they will be added later in the flow)
|
Object.keys(this.l10nTranslations).forEach(lang => {
|
||||||
let bundle = noDynList.filter(f => !f.includes(".lproj"));
|
const strings = generateStringFile(this.l10nTranslations[lang]);
|
||||||
|
|
||||||
// Localization folders only
|
|
||||||
const L10N = noDynList.filter(f => f.includes(".lproj") && Object.keys(this.l10n).includes(path.parse(f).name));
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Reading all the localization selected folders and removing hidden files (the ones that starts with ".")
|
|
||||||
* from the list.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const L10N_FilesListByFolder = await Promise.all(
|
|
||||||
L10N.map(async folderPath => {
|
|
||||||
const list = await readdir(path.join(this.model, folderPath))
|
|
||||||
return removeHidden(list);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
// Pushing into the bundle the composed paths for localization files
|
|
||||||
|
|
||||||
L10N_FilesListByFolder.forEach((filesList, index) =>
|
|
||||||
bundle.push(
|
|
||||||
...filesList.map(file => path.join(L10N[index], file))
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
/* Getting all bundle file buffers, pass.json included, and appending path */
|
|
||||||
|
|
||||||
// Reading bundle files to buffers without pass.json - it gets read below
|
|
||||||
// to use a different parsing process
|
|
||||||
|
|
||||||
const bundleBuffers = bundle.map(file => readFile(path.resolve(this.model, file)));
|
|
||||||
const passBuffer = this._extractPassDefinition();
|
|
||||||
bundle.push("pass.json");
|
|
||||||
|
|
||||||
const buffers = await Promise.all([...bundleBuffers, passBuffer]);
|
|
||||||
|
|
||||||
Object.keys(this.l10n).forEach(lang => {
|
|
||||||
const strings = generateStringFile(this.l10n[lang]);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* if .string file buffer is empty, no translations were added
|
|
||||||
* but still wanted to include the language
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (!strings.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (strings.length) {
|
||||||
/**
|
/**
|
||||||
* if there's already a buffer of the same folder and called
|
* if there's already a buffer of the same folder and called
|
||||||
* `pass.strings`, we'll merge the two buffers. We'll create
|
* `pass.strings`, we'll merge the two buffers. We'll create
|
||||||
* it otherwise.
|
* it otherwise.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!this.l10nBundles[lang]) {
|
||||||
|
this.l10nBundles[lang] = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
this.l10nBundles[lang]["pass.strings"] = Buffer.concat([
|
||||||
|
this.l10nBundles[lang]["pass.strings"] || Buffer.from("", "utf8"),
|
||||||
|
strings
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(this.l10nBundles[lang] && Object.keys(this.l10nBundles[lang]).length)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assigning all the localization files to the final bundle
|
||||||
|
* by mapping the buffer to the pass-relative file path;
|
||||||
*
|
*
|
||||||
* We are replacing the slashes to avoid Windows slashes
|
* We are replacing the slashes to avoid Windows slashes
|
||||||
* composition.
|
* composition.
|
||||||
*/
|
*/
|
||||||
|
Object.assign(finalBundle, ...Object.keys(this.l10nBundles[lang])
|
||||||
const stringFilePath = path.join(`${lang}.lproj`, "pass.strings").replace(/\\/, "/");
|
.map(fileName => {
|
||||||
|
const fullPath = path.join(`${lang}.lproj`, fileName).replace(/\\/, "/");
|
||||||
const stringFileIndex = bundle.findIndex(file => file === stringFilePath);
|
return { [fullPath]: this.l10nBundles[lang][fileName] };
|
||||||
|
})
|
||||||
if (stringFileIndex > -1) {
|
);
|
||||||
buffers[stringFileIndex] = Buffer.concat([
|
|
||||||
buffers[stringFileIndex],
|
|
||||||
strings
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
buffers.push(strings);
|
|
||||||
bundle.push(stringFilePath);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -178,24 +138,17 @@ export class Pass implements PassIndexSignature {
|
|||||||
* and returning the compiled manifest
|
* and returning the compiled manifest
|
||||||
*/
|
*/
|
||||||
const archive = archiver("zip");
|
const archive = archiver("zip");
|
||||||
const manifest = buffers.reduce((acc, current, index) => {
|
const manifest = Object.keys(finalBundle).reduce((acc, current) => {
|
||||||
let filename = bundle[index];
|
|
||||||
let hashFlow = forge.md.sha1.create();
|
let hashFlow = forge.md.sha1.create();
|
||||||
|
|
||||||
hashFlow.update(current.toString("binary"));
|
hashFlow.update(finalBundle[current].toString("binary"));
|
||||||
archive.append(current, { name: filename });
|
archive.append(current, { name: current });
|
||||||
|
|
||||||
acc[filename] = hashFlow.digest().toHex();
|
acc[current] = hashFlow.digest().toHex();
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
// Reading the certificates,
|
|
||||||
// signing the manifest, appending signature an manifest to the archive
|
|
||||||
// and returning the generated pass stream.
|
|
||||||
|
|
||||||
Object.assign(this.Certificates, await readCertificates(this.Certificates));
|
|
||||||
|
|
||||||
const signatureBuffer = this._sign(manifest);
|
const signatureBuffer = this._sign(manifest);
|
||||||
|
|
||||||
archive.append(signatureBuffer, { name: "signature" });
|
archive.append(signatureBuffer, { name: "signature" });
|
||||||
@@ -206,14 +159,6 @@ export class Pass implements PassIndexSignature {
|
|||||||
archive.pipe(passStream);
|
archive.pipe(passStream);
|
||||||
|
|
||||||
return archive.finalize().then(() => passStream);
|
return archive.finalize().then(() => passStream);
|
||||||
} catch (err) {
|
|
||||||
if (err.code && err.code === "ENOENT") {
|
|
||||||
// No model available at this path - renaming the error
|
|
||||||
throw new Error(formatMessage("MODEL_NOT_FOUND", this.model));
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -230,7 +175,7 @@ export class Pass implements PassIndexSignature {
|
|||||||
|
|
||||||
localize(lang: string, translations?: { [key: string]: string }) {
|
localize(lang: string, translations?: { [key: string]: string }) {
|
||||||
if (lang && typeof lang === "string" && (typeof translations === "object" || translations === undefined)) {
|
if (lang && typeof lang === "string" && (typeof translations === "object" || translations === undefined)) {
|
||||||
this.l10n[lang] = translations || {};
|
this.l10nTranslations[lang] = translations || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
Reference in New Issue
Block a user