From 92a48eccbec710346ad228afb6198c5dd95b8011 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Thu, 11 Jul 2019 23:29:23 +0200 Subject: [PATCH] Renamed splitBundle to splitBufferBundle and edited it to return splitted l10n/rootBundle object --- src/factory.ts | 4 ++-- src/utils.ts | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/factory.ts b/src/factory.ts index 8dd8378..2043c54 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -2,7 +2,7 @@ import { Pass } from "./pass"; import { FactoryOptions, BundleUnit } from "./schema"; import formatMessage from "./messages"; import { getModelContents, readCertificatesFromOptions } from "./parser"; -import { splitBundle } from "./utils"; +import { splitBufferBundle } from "./utils"; export type Pass = InstanceType @@ -18,7 +18,7 @@ export async function createPass(options: FactoryOptions, additionalBuffers?: Bu ]); if (additionalBuffers) { - const [ additionalL10n, additionalBundle ] = splitBundle(additionalBuffers); + const [ additionalL10n, additionalBundle ] = splitBufferBundle(additionalBuffers); Object.assign(bundle["l10nBundle"], additionalL10n); Object.assign(bundle["bundle"], additionalBundle); } diff --git a/src/utils.ts b/src/utils.ts index 2968bbf..9f3f528 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import moment from "moment"; import { EOL } from "os"; -import { BundleUnit } from "./schema"; +import { PartitionedBundle } from "./schema"; +import { sep } from "path"; /** * Checks if an rgb value is compliant with CSS-like syntax @@ -88,11 +89,19 @@ export function generateStringFile(lang: { [index: string]: string }): Buffer { * @param origin */ -export function splitBundle(origin: Object): [BundleUnit, BundleUnit] { +export function splitBufferBundle(origin: Object): [PartitionedBundle["l10nBundle"], PartitionedBundle["bundle"]] { const keys = Object.keys(origin); - return keys.reduce(([ l10n, bundle ], current) => - current.includes(".lproj") && - [ { ...l10n, [current]: origin[current] }, bundle] || - [ l10n, {...bundle, [current]: origin[current] }] - , [{},{}]); + return keys.reduce(([ l10n, bundle ], current) => { + if (current.includes(".lproj")) { + const pathComponents = current.split(sep); + const lang = pathComponents[0]; + const file = pathComponents.slice(1).join("/"); + + (l10n[lang] || (l10n[lang] = {}))[file] = origin[current]; + + return [ l10n, bundle ]; + } else { + return [ l10n, { ...bundle, [current]: origin[current] }]; + } + }, [{},{}]); }