Refactored all the schemas;

Splitted them in a folder and renamed them to have the same name of typescript type;
Added more SemanticKeys;
Removed Schema resolution via string;
This commit is contained in:
Alexander Cerutti
2021-06-17 23:34:22 +02:00
parent 87321b8dc9
commit 17de1c7d79
16 changed files with 850 additions and 771 deletions

View File

@@ -1,14 +1,7 @@
import * as path from "path";
import forge from "node-forge";
import formatMessage from "./messages";
import {
FactoryOptions,
PartitionedBundle,
BundleUnit,
Certificates,
FinalCertificates,
isValid,
} from "./schema";
import * as Schemas from "./schemas";
import {
removeHidden,
splitBufferBundle,
@@ -28,8 +21,8 @@ const { readdir: readDir, readFile } = fs.promises;
* @param model
*/
export async function getModelContents(model: FactoryOptions["model"]) {
let modelContents: PartitionedBundle;
export async function getModelContents(model: Schemas.FactoryOptions["model"]) {
let modelContents: Schemas.PartitionedBundle;
if (typeof model === "string") {
modelContents = await getModelFolderContents(model);
@@ -77,9 +70,9 @@ export async function getModelContents(model: FactoryOptions["model"]) {
const parsedPersonalization = JSON.parse(
modelContents.bundle[personalizationJsonFile].toString("utf8"),
);
const isPersonalizationValid = isValid(
const isPersonalizationValid = Schemas.isValid(
parsedPersonalization,
"personalizationDict",
Schemas.Personalization,
);
if (!isPersonalizationValid) {
@@ -105,7 +98,7 @@ export async function getModelContents(model: FactoryOptions["model"]) {
export async function getModelFolderContents(
model: string,
): Promise<PartitionedBundle> {
): Promise<Schemas.PartitionedBundle> {
try {
const modelPath = `${model}${(!path.extname(model) && ".pass") || ""}`;
const modelFilesList = await readDir(modelPath);
@@ -142,7 +135,7 @@ export async function getModelFolderContents(
),
);
const bundle: BundleUnit = Object.assign(
const bundle: Schemas.BundleUnit = Object.assign(
{},
...rawBundleFiles.map((fileName, index) => ({
[fileName]: rawBundleBuffers[index],
@@ -151,7 +144,7 @@ export async function getModelFolderContents(
// Reading concurrently localizations folder
// and their files and their buffers
const L10N_FilesListByFolder: Array<BundleUnit> = await Promise.all(
const L10N_FilesListByFolder: Array<Schemas.BundleUnit> = await Promise.all(
l10nFolders.map(async (folderPath) => {
// Reading current folder
const currentLangPath = path.join(modelPath, folderPath);
@@ -172,23 +165,27 @@ export async function getModelFolderContents(
// Assigning each file path to its buffer
// and discarding the empty ones
return validFiles.reduce<BundleUnit>((acc, file, index) => {
if (!buffers[index].length) {
return acc;
}
return validFiles.reduce<Schemas.BundleUnit>(
(acc, file, index) => {
if (!buffers[index].length) {
return acc;
}
const fileComponents = file.split(path.sep);
const fileName = fileComponents[fileComponents.length - 1];
const fileComponents = file.split(path.sep);
const fileName =
fileComponents[fileComponents.length - 1];
return {
...acc,
[fileName]: buffers[index],
};
}, {});
return {
...acc,
[fileName]: buffers[index],
};
},
{},
);
}),
);
const l10nBundle: PartitionedBundle["l10nBundle"] = Object.assign(
const l10nBundle: Schemas.PartitionedBundle["l10nBundle"] = Object.assign(
{},
...L10N_FilesListByFolder.map((folder, index) => ({
[l10nFolders[index]]: folder,
@@ -226,20 +223,21 @@ export async function getModelFolderContents(
* @param model
*/
export function getModelBufferContents(model: BundleUnit): PartitionedBundle {
const rawBundle = 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
export function getModelBufferContents(
model: Schemas.BundleUnit,
): Schemas.PartitionedBundle {
const rawBundle = removeHidden(
Object.keys(model),
).reduce<Schemas.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) || !model[current]) {
return acc;
}
if (/(manifest|signature)/.test(current) || !model[current]) {
return acc;
}
return { ...acc, [current]: model[current] };
},
{},
);
return { ...acc, [current]: model[current] };
}, {});
const bundleKeys = Object.keys(rawBundle);
@@ -266,18 +264,18 @@ export function getModelBufferContents(model: BundleUnit): PartitionedBundle {
* @param options
*/
type flatCertificates = Omit<Certificates, "signerKey"> & {
type flatCertificates = Omit<Schemas.Certificates, "signerKey"> & {
signerKey: string;
};
export async function readCertificatesFromOptions(
options: Certificates,
): Promise<FinalCertificates> {
options: Schemas.Certificates,
): Promise<Schemas.CertificatesSchema> {
if (
!(
options &&
Object.keys(options).length &&
isValid(options, "certificatesSchema")
Schemas.isValid(options, Schemas.CertificatesSchema)
)
) {
throw new Error(formatMessage("CP_NO_CERTS"));