Added first support to localization L10N, now localized folders get recognized, indexed, and their files sha1-calculated

This commit is contained in:
Alexander Cerutti
2018-06-11 23:55:08 +02:00
parent 006a0270d3
commit a7590b09dd

158
index.js
View File

@@ -251,7 +251,9 @@ function generatePass(options) {
}); });
} }
fs.readdir(path.resolve(Configuration.passModelsDir, `${options.modelName}.pass`), function(err, files) { let modelPath = path.resolve(Configuration.passModelsDir, `${options.modelName}.pass`);
fs.readdir(modelPath, function(err, files) {
if (err) { if (err) {
return reject({ return reject({
status: false, status: false,
@@ -264,8 +266,6 @@ function generatePass(options) {
// Removing hidden files and folders // Removing hidden files and folders
let list = removeHiddenFiles(files).filter(f => !f.includes(".lproj")); let list = removeHiddenFiles(files).filter(f => !f.includes(".lproj"));
// Getting only folders
let folderList = files.filter(f => f.includes(".lproj"));
if (!list.length) { if (!list.length) {
return reject({ return reject({
@@ -287,82 +287,106 @@ function generatePass(options) {
}); });
} }
let manifest = {}; // Getting only folders
let archive = archiver("zip"); let folderList = files.filter(f => f.includes(".lproj"));
// Using async.parallel since the final part must be executed only when both are completed. // I may have (and I rathered) used async.concat to achieve this but it returns a list of filenames ordered by folder.
// Otherwise would had to put everything in editPassStructure's Promise .then(). // The problem rise when I have to understand which is the first file of a folder which is not the first one.
async.parallel([ // By doing this way, I get an Array containing an array of filenames for each folder.
function _managePass(passCallback) {
fs.readFile(path.resolve(Configuration.passModelsDir, `${options.modelName}.pass`, "pass.json"), {}, function _parsePassJSONBuffer(err, passStructBuffer) {
editPassStructure(filterPassOptions(options.overrides), passStructBuffer)
.then(function _afterJSONParse(passFileBuffer) {
manifest["pass.json"] = forge.md.sha1.create().update(passFileBuffer.toString("binary")).digest().toHex();
archive.append(passFileBuffer, { name: "pass.json" });
return passCallback(null) let folderExtractors = folderList.map(f => function(callback) {
}) let l10nPath = path.join(modelPath, f);
.catch(function(err) {
return reject({ fs.readdir(l10nPath, function(err, list) {
status: false, if (err) {
error: { return callback(err, null);
message: `pass.json Buffer is not a valid buffer. Unable to continue.\n${err}`, }
ecode: 418
} let filteredFiles = removeHiddenFiles(list);
return callback(null, filteredFiles);
});
});
async.parallel(folderExtractors, function(err, listByFolder) {
listByFolder.forEach((folder, index) => folder.forEach(f => list.push(path.join(folderList[index], f)) ) )
let manifest = {};
let archive = archiver("zip");
// Using async.parallel since the final part must be executed only when both are completed.
// Otherwise would had to put everything in editPassStructure's Promise .then().
async.parallel([
function _managePass(passCallback) {
fs.readFile(path.resolve(Configuration.passModelsDir, `${options.modelName}.pass`, "pass.json"), {}, function _parsePassJSONBuffer(err, passStructBuffer) {
editPassStructure(filterPassOptions(options.overrides), passStructBuffer)
.then(function _afterJSONParse(passFileBuffer) {
manifest["pass.json"] = forge.md.sha1.create().update(passFileBuffer.toString("binary")).digest().toHex();
archive.append(passFileBuffer, { name: "pass.json" });
return passCallback(null)
})
.catch(function(err) {
return reject({
status: false,
error: {
message: `pass.json Buffer is not a valid buffer. Unable to continue.\n${err}`,
ecode: 418
}
});
}); });
}); });
}); },
},
function _manageBundle(bundleCallback) { function _manageBundle(bundleCallback) {
async.each(list, function getHashAndArchive(file, callback) { async.each(list, function getHashAndArchive(file, callback) {
if (/(manifest|signature|pass)/ig.test(file)) { if (/(manifest|signature|pass)/ig.test(file)) {
// skipping files // skipping files
return callback(); return callback();
} }
// adding the files to the zip - i'm not using .directory method because it adds also hidden files like .DS_Store on macOS // adding the files to the zip - i'm not using .directory method because it adds also hidden files like .DS_Store on macOS
archive.file(path.resolve(Configuration.passModelsDir, `${options.modelName}.pass`, file), { name: file }); archive.file(path.resolve(Configuration.passModelsDir, `${options.modelName}.pass`, file), { name: file });
let hashFlow = forge.md.sha1.create(); let hashFlow = forge.md.sha1.create();
fs.createReadStream(path.resolve(Configuration.passModelsDir, `${options.modelName}.pass`, file)) fs.createReadStream(path.resolve(Configuration.passModelsDir, `${options.modelName}.pass`, file))
.on("data", function(data) { .on("data", function(data) {
hashFlow.update(data.toString("binary")); hashFlow.update(data.toString("binary"));
}) })
.on("error", function(e) { .on("error", function(e) {
return callback(e); return callback(e);
}) })
.on("end", function() { .on("end", function() {
manifest[file] = hashFlow.digest().toHex().trim(); manifest[file] = hashFlow.digest().toHex().trim();
return callback(); return callback();
});
}, function end(error) {
if (error) {
return reject({
status: false,
error: {
message: `Unable to compile manifest. ${error}`,
ecode: 418
}
}); });
} }, function end(error) {
if (error) {
return reject({
status: false,
error: {
message: `Unable to compile manifest. ${error}`,
ecode: 418
}
});
}
return bundleCallback(null); return bundleCallback(null);
}); });
} }
], function _composeStream() { ], function _composeStream() {
archive.append(JSON.stringify(manifest), { name: "manifest.json" }); archive.append(JSON.stringify(manifest), { name: "manifest.json" });
let signatureBuffer = createSignature(manifest); let signatureBuffer = createSignature(manifest);
archive.append(signatureBuffer, { name: "signature" }); archive.append(signatureBuffer, { name: "signature" });
let passStream = new stream.PassThrough(); let passStream = new stream.PassThrough();
archive.pipe(passStream); archive.pipe(passStream);
archive.finalize().then(function() { archive.finalize().then(function() {
return success({ return success({
status: true, status: true,
content: passStream, content: passStream,
});
}); });
}); });
}); });