Removed Promise returning template function to be executed for each buffer for a reduce implementation

This commit is contained in:
alexandercerutti
2018-08-22 22:43:48 +02:00
parent 3eac7a4cb5
commit db74f3d407

View File

@@ -92,37 +92,48 @@ class Pass {
listByFolder.forEach((folder, index) => bundle.push(...folder.map(f => path.join(L10N[index], f)))); listByFolder.forEach((folder, index) => bundle.push(...folder.map(f => path.join(L10N[index], f))));
return Promise.all([...bundle.map(f => readFile(path.resolve(this.model, f))), _passExtractor()]).then(buffers => { /*
Object.keys(this.l10n).forEach(l => { * Getting all bundle file buffers, pass.json included and appending
const strings = this._generateStringFile(l); */
if (strings.length) {
buffers.push(strings);
bundle.push(path.join(`${l}.lproj`, `pass.strings`));
}
});
return [buffers, bundle]; let bundleBuffers = bundle.map(f => readFile(path.resolve(this.model, f)));
}); let passBuffer = passExtractor();
})
return Promise.all([...bundleBuffers, passBuffer])
.then(buffers => {
Object.keys(this.l10n).forEach(l => {
const strings = this._generateStringFile(l);
// if .string file buffer is empty, no translations were added
// but still wanted to include the language
if (strings.length) {
buffers.push(strings);
bundle.push(path.join(`${l}.lproj`, `pass.strings`));
}
});
return [buffers, bundle];
});
});
}) })
.then(([buffers, bundle]) => { .then(([buffers, bundle]) => {
/* Parsing the buffers, pushing them into the archive and returning the manifest */ /*
* Parsing the buffers, pushing them into the archive
* and returning the compiled manifest
*/
let manifest = {}; return buffers.reduce((acc, current, index) => {
let filename = bundle[index];
let hashAppendTemplate = ((buffer, key) => {
let hashFlow = forge.md.sha1.create(); let hashFlow = forge.md.sha1.create();
hashFlow.update(buffer.toString("binary"));
manifest[key] = hashFlow.digest().toHex(); hashFlow.update(current.toString("binary"));
archive.append(current, { name: filename });
archive.append(buffer, { name: key }); acc[filename] = hashFlow.digest().toHex();
return Promise.resolve();
});
let passFilesFn = buffers.map((buf, index) => hashAppendTemplate.bind(null, buf, bundle[index])()); return acc;
}, {});
return Promise.all(passFilesFn).then(() => manifest);
}) })
.then((manifest) => { .then((manifest) => {
archive.append(JSON.stringify(manifest), { name: "manifest.json" }); archive.append(JSON.stringify(manifest), { name: "manifest.json" });