Added functions getModelBufferContents, getModelFolderContents and getModelContents;

This commit is contained in:
Alexander Cerutti
2019-05-28 23:19:32 +02:00
parent 95240a91f7
commit 1cc8ae2975

View File

@@ -57,6 +57,131 @@ async function createPass(options: FactoryOptions) {
return new Pass(); return new Pass();
} }
interface BundleUnit {
[key: string]: Buffer;
}
async function getModelContents(model: FactoryOptions["model"]) {
if (!(model && (typeof model === "string" || (typeof model === "object" && Object.keys(model).length)))) {
throw new Error("Unable to create Pass: invalid model provided");
}
let modelContents: {
bundle: BundleUnit,
l10nBundle: {
[key: string]: BundleUnit
},
};
if (typeof model === "string") {
modelContents = await getModelFolderContents(model);
} else {
modelContents = getModelBufferContents(model);
}
const modelFiles = Object.keys(modelContents);
if (!(modelFiles.includes("pass.json") && modelFiles.some(file => file.includes("icon")))) {
throw new Error("missing icon or pass.json");
}
return modelContents;
}
async function getModelFolderContents(model: string) {
const modelPath = path.resolve(model) + (!!model && !path.extname(model) ? ".pass" : "");
const modelFilesList = await readDir(modelPath);
// No dot-starting files, manifest and signature
const filteredFiles = removeHidden(modelFilesList).filter(f => !/(manifest|signature|pass)/i.test(f));
// Icon is required to proceed
if (!(filteredFiles.length && filteredFiles.some(file => file.toLowerCase().includes("icon")))) {
const eMessage = formatMessage("MODEL_UNINITIALIZED", path.parse(this.model).name);
throw new Error(eMessage);
}
// Splitting files from localization folders
const bundle = filteredFiles.filter(entry => !entry.includes(".lproj"));
const l10nFolders = filteredFiles.filter(entry => entry.includes(".lproj"));
const bundleBuffers = bundle.map(file => readFile(path.resolve(model, file)));
const buffers = await Promise.all(bundleBuffers);
const bundleMap = Object.assign({},
...bundle.map((fileName, index) => ({ [fileName]: buffers[index] }))
) as BundleUnit;
// Reading concurrently localizations folder
// and their files and their buffers
const L10N_FilesListByFolder: Array<BundleUnit> = await Promise.all(
l10nFolders.map(folderPath => {
// Reading current folder
const currentLangPath = path.join(model, folderPath);
return readDir(currentLangPath)
.then(files => {
// Transforming files path to a model-relative path
const validFiles = removeHidden(files)
.map(file => path.join(currentLangPath, file));
// Getting all the buffers from file paths
return Promise.all([
...validFiles.map(file => readFile(file))
]).then(buffers =>
// Assigning each file path to its buffer
Object.assign({}, ...validFiles.map((file, index) =>
({ [file]: buffers[index] })
)) as BundleUnit
);
});
})
);
return {
bundle: bundleMap,
l10nBundle: Object.assign({}, ...L10N_FilesListByFolder
.map((folder, index) => ({ [l10nFolders[index]]: folder }))
) as { [key: string]: BundleUnit }
};
}
function getModelBufferContents(model: BundleUnit) {
const bundle = removeHidden(Object.keys(model)).reduce<BundleUnit>((acc, current) => {
// Checking if current file is one of the autogenerated ones or if its
// content is not available
if (/(manifest|signature)/.test(current) || !bundle[current]) {
return acc;
}
return { ...acc, [current]: model[current] };
}, {});
const bundleKeys = Object.keys(bundle);
if (!bundleKeys.length) {
throw new Error("Cannot proceed with pass creation: bundle initialized")
}
// separing localization folders
const l10nFolders = bundleKeys.filter(file => file.includes(".lproj"));
const l10nBundle: { [key: string]: BundleUnit } = Object.assign({},
...l10nFolders.map(folder =>
({ [folder]: bundle[folder] }) as BundleUnit
)
);
const unLocalizedBundle = Object.assign({},
...bundleKeys
.filter(file => !file.includes(".lproj"))
.map(file => ({ [file]: bundle[file] }))
) as BundleUnit;
return {
bundle: unLocalizedBundle,
l10nBundle
};
}
/** /**
* Reads certificate contents, if the passed content is a path, * Reads certificate contents, if the passed content is a path,
* and parses them as a PEM. * and parses them as a PEM.