mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 18:25:24 +00:00
Transformed the whole generate() execution to a Promise-driven flow
This commit is contained in:
223
index.js
223
index.js
@@ -27,82 +27,45 @@ class Pass {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
generate() {
|
generate() {
|
||||||
let manifest = {};
|
|
||||||
let archive = archiver("zip");
|
let archive = archiver("zip");
|
||||||
|
|
||||||
return new Promise((success, reject) => {
|
return this._parseSettings(this.options)
|
||||||
let _gen = (() => {
|
.then(() => readdir(this.model))
|
||||||
fs.readdir(this.model, (err, files) => {
|
.catch(() => Promise.reject({
|
||||||
if (err) {
|
status: false,
|
||||||
return reject({
|
error: {
|
||||||
status: false,
|
message: `Model ${this.model} not found. Provide a valid one to continue`
|
||||||
error: {
|
}
|
||||||
message: "Model not found. Provide a valid one to continue."
|
}))
|
||||||
}
|
.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));
|
||||||
|
|
||||||
// list without dynamic components like manifest, signature or pass files (will be added later in the flow) and hidden files.
|
if (!noDynList.length) {
|
||||||
let noDynList = removeHidden(files).filter(f => !/(manifest|signature|pass)/i.test(f));
|
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."
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!noDynList.length) {
|
// list without localization files (they will be added later in the flow)
|
||||||
return reject({
|
let bundle = noDynList.filter(f => !f.includes(".lproj"));
|
||||||
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)
|
// Localization folders only
|
||||||
let bundleList = noDynList.filter(f => !f.includes(".lproj"));
|
const L10N = noDynList.filter(f => f.includes(".lproj"));
|
||||||
|
|
||||||
const L10N = {
|
/*
|
||||||
// localization folders only
|
* Defining pass.json patcher and extractor
|
||||||
list: noDynList.filter(f => f.includes(".lproj"))
|
* Extracting it with the other paths
|
||||||
};
|
*/
|
||||||
|
|
||||||
/*
|
|
||||||
* I may have (and I rathered) used async.concat to achieve this but it returns an
|
|
||||||
* array of filenames ordered by folder, without any kind of folder indication.
|
|
||||||
* So, the problem rises when I have to understand which is the first file of a
|
|
||||||
* folder which is not the first one, as I don't know how many file there are in
|
|
||||||
* a folder.
|
|
||||||
*
|
|
||||||
* Therefore, I generate a function for each localization (L10N) folder inside the
|
|
||||||
* model. Each function will read the content of the folder and return an array of
|
|
||||||
* the filenames inside that L10N folder.
|
|
||||||
*/
|
|
||||||
|
|
||||||
L10N.extractors = L10N.list.map(f => ((callback) => {
|
|
||||||
let l10nPath = path.join(this.model, f);
|
|
||||||
|
|
||||||
fs.readdir(l10nPath, function(err, list) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
let filteredFiles = removeHidden(list);
|
|
||||||
return callback(null, filteredFiles);
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
// === flow definition ===
|
|
||||||
|
|
||||||
let _passExtractor = (passCallback => {
|
|
||||||
fs.readFile(path.resolve(this.model, "pass.json"), {}, (err, passStructBuffer) => {
|
|
||||||
if (err) {
|
|
||||||
// Flow should never enter in there since pass.json existence-check is already done above.
|
|
||||||
return passCallback({
|
|
||||||
status: false,
|
|
||||||
error: {
|
|
||||||
message: `Unable to read pass.json file @ ${this.model}`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
let _passExtractor = (() => {
|
||||||
|
return readFile(path.resolve(this.model, "pass.json"))
|
||||||
|
.then(passStructBuffer => {
|
||||||
if (!this._validateType(passStructBuffer)) {
|
if (!this._validateType(passStructBuffer)) {
|
||||||
return passCallback({
|
return Promise.reject({
|
||||||
status: false,
|
status: false,
|
||||||
error: {
|
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.`
|
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.`
|
||||||
@@ -110,94 +73,66 @@ class Pass {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
bundle.push("pass.json");
|
||||||
let patchedPass = this._patch(this._filterOptions(this.overrides), passStructBuffer);
|
|
||||||
|
|
||||||
manifest["pass.json"] = forge.md.sha1.create().update(patchedPass.toString("binary")).digest().toHex();
|
return this._patch(this._filterOptions(this.overrides), passStructBuffer);
|
||||||
archive.append(patchedPass, { name: "pass.json" });
|
})
|
||||||
|
.catch(err => {
|
||||||
return passCallback();
|
console.log(err);
|
||||||
} catch (e) {
|
return Promise.reject({
|
||||||
return passCallback({
|
|
||||||
status: false,
|
|
||||||
error: {
|
|
||||||
message: `Unable to read pass.json as buffer @ ${this.model}. Unable to continue.\n${err}`,
|
|
||||||
ecode: 418
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
let _addBuffers = ((err, modelBuffers) => {
|
|
||||||
if (err) {
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// I want to get an object containing each buffer associated with its own file name
|
|
||||||
let modelFiles = Object.assign(...modelBuffers.map((buf, index) => ({ [bundleList[index]]: buf })));
|
|
||||||
|
|
||||||
async.eachOf(modelFiles, (fileBuffer, bufferKey, callback) => {
|
|
||||||
let hashFlow = forge.md.sha1.create();
|
|
||||||
hashFlow.update(fileBuffer.toString("binary"));
|
|
||||||
|
|
||||||
manifest[bufferKey] = hashFlow.digest().toHex().trim();
|
|
||||||
archive.file(path.resolve(this.model, bufferKey), { name: bufferKey });
|
|
||||||
|
|
||||||
return callback();
|
|
||||||
}, _finalize);
|
|
||||||
});
|
|
||||||
|
|
||||||
let _finalize = (err => {
|
|
||||||
if (err) {
|
|
||||||
return reject({
|
|
||||||
status: false,
|
status: false,
|
||||||
error: {
|
error: {
|
||||||
message: `Unable to compile manifest. ${err}`,
|
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.`
|
||||||
ecode: 418
|
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
|
||||||
|
|
||||||
archive.append(JSON.stringify(manifest), { name: "manifest.json" });
|
|
||||||
|
|
||||||
let signatureBuffer = this._sign(manifest);
|
|
||||||
archive.append(signatureBuffer, { name: "signature" });
|
|
||||||
|
|
||||||
let passStream = new stream.PassThrough();
|
|
||||||
|
|
||||||
archive.pipe(passStream);
|
|
||||||
archive.finalize().then(function() {
|
|
||||||
return success({
|
|
||||||
status: true,
|
|
||||||
content: passStream,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// === execution ===
|
return Promise.all(L10N.map(f => readdir(path.join(this.model, f)).then(removeHidden)))
|
||||||
|
.then(listByFolder => {
|
||||||
|
listByFolder.forEach((folder, index) => bundle.push(...folder.map(f => path.join(L10N[index], f))));
|
||||||
|
|
||||||
async.parallel([_passExtractor, ...L10N.extractors], (err, listByFolder) => {
|
return Promise.all([...bundle.map(f => readFile(path.resolve(this.model, f))), _passExtractor()]).then(buffers => [buffers, bundle]);
|
||||||
if (err) {
|
})
|
||||||
return reject(err);
|
})
|
||||||
}
|
.then(([buffers, bundle]) => {
|
||||||
|
/*
|
||||||
|
* Parsing the buffers and pushing them into the archive
|
||||||
|
*/
|
||||||
|
|
||||||
// removing result of passExtractor, which is undefined.
|
let manifest = {};
|
||||||
listByFolder.shift();
|
|
||||||
|
|
||||||
listByFolder.forEach((folder, index) => bundleList.push(...folder.map(f => path.join(L10N.list[index], f))));
|
let hashAppendTemplate = ((buffer, key) => {
|
||||||
|
let hashFlow = forge.md.sha1.create();
|
||||||
|
hashFlow.update(buffer.toString("binary"));
|
||||||
|
|
||||||
let pathList = bundleList.map(f => path.resolve(this.model, f));
|
manifest[key] = hashFlow.digest().toHex();
|
||||||
|
|
||||||
async.concat(pathList, fs.readFile, _addBuffers);
|
archive.append(buffer, { name: key });
|
||||||
});
|
return Promise.resolve();
|
||||||
})
|
});
|
||||||
|
|
||||||
|
let passFilesFn = buffers.map((buf, index) => hashAppendTemplate.bind(null, buf, bundle[index])());
|
||||||
|
|
||||||
|
return Promise.all(passFilesFn).then(() => manifest);
|
||||||
|
})
|
||||||
|
.then((manifest) => {
|
||||||
|
archive.append(JSON.stringify(manifest), { name: "manifest.json" });
|
||||||
|
|
||||||
|
let signatureBuffer = this._sign(manifest);
|
||||||
|
archive.append(signatureBuffer, { name: "signature" });
|
||||||
|
|
||||||
|
let passStream = new stream.PassThrough();
|
||||||
|
|
||||||
|
archive.pipe(passStream);
|
||||||
|
|
||||||
|
return archive.finalize().then(() => {
|
||||||
|
return {
|
||||||
|
status: true,
|
||||||
|
content: passStream,
|
||||||
|
};
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
this._parseSettings(this.options)
|
|
||||||
.then(_gen)
|
|
||||||
.catch(reject)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user