From a110d76b18af87f1fa3afdd2b38494ca8164d0ad Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sat, 5 Sep 2020 23:29:05 +0200 Subject: [PATCH] Generic Improvements --- src/parser.ts | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 1d80252..7f92712 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -18,25 +18,14 @@ const readFile = promisify(_readFile); */ export async function getModelContents(model: FactoryOptions["model"]) { - const isModelValid = ( - model && ( - typeof model === "string" || ( - typeof model === "object" && - Object.keys(model).length - ) - ) - ); - - if (!isModelValid) { - throw new Error(formatMessage("MODEL_NOT_VALID")); - } - let modelContents: PartitionedBundle; if (typeof model === "string") { modelContents = await getModelFolderContents(model); - } else { + } else if (typeof model === "object" && Object.keys(model).length) { modelContents = getModelBufferContents(model); + } else { + throw new Error(formatMessage("MODEL_NOT_VALID")); } const modelFiles = Object.keys(modelContents.bundle); @@ -112,14 +101,15 @@ export async function getModelFolderContents(model: string): Promise !entry.includes(".lproj")); + const rawBundleFiles = filteredFiles.filter(entry => !entry.includes(".lproj")); const l10nFolders = filteredFiles.filter(entry => entry.includes(".lproj")); - const bundleBuffers = rawBundle.map(file => readFile(path.resolve(modelPath, file))); - const buffers = await Promise.all(bundleBuffers); + const rawBundleBuffers = await Promise.all( + rawBundleFiles.map(file => readFile(path.resolve(modelPath, file))) + ); const bundle: BundleUnit = Object.assign({}, - ...rawBundle.map((fileName, index) => ({ [fileName]: buffers[index] })) + ...rawBundleFiles.map((fileName, index) => ({ [fileName]: rawBundleBuffers[index] })) ); // Reading concurrently localizations folder