@ilingo/fs
Version:
This is a lightweight library for translation.
1 lines • 9.5 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../src/utils.ts","../src/module.ts"],"sourcesContent":["/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { normalize } from 'pathe';\nimport process from 'node:process';\nimport type { Config, ConfigInput } from './types';\n\nexport function buildConfig(input?: ConfigInput) : Config {\n input = input || {};\n\n let directory : string[];\n if (input.directory) {\n directory = Array.isArray(input.directory) ?\n input.directory :\n [input.directory];\n\n for (let i = 0; i < directory.length; i++) {\n directory[i] = normalize(directory[i]);\n }\n } else {\n directory = [process.cwd()];\n }\n\n return {\n directory,\n };\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { LocatorOptionsInput } from 'locter';\nimport {\n load,\n locateMany,\n} from 'locter';\nimport path from 'node:path';\nimport type { Merger } from 'smob';\nimport { createMerger } from 'smob';\nimport type { LinesRecord, StoreGetContext, StoreSetContext } from 'ilingo';\nimport { MemoryStore, isBCP47LanguageCode, isLineRecord } from 'ilingo';\nimport type { ConfigInput } from './types';\nimport { buildConfig } from './utils';\n\nexport class FSStore extends MemoryStore {\n protected loaded : Record<string, string[]>;\n\n protected directories : string[];\n\n protected merger : Merger;\n\n constructor(input?: ConfigInput) {\n super({ data: {} });\n\n const options = buildConfig(input);\n\n this.loaded = {};\n this.directories = options.directory;\n\n this.merger = createMerger({\n inPlace: true,\n array: true,\n arrayDistinct: true,\n });\n }\n\n // ------------------------------------------\n override async get(context: StoreGetContext): Promise<string | undefined> {\n await this.loadGroup(context.group, context.locale);\n\n return super.get(context);\n }\n\n override async set(context: StoreSetContext): Promise<void> {\n return super.set(context);\n\n // todo: write to file!\n }\n\n // ------------------------------------------\n\n override async getLocales() : Promise<string[]> {\n const locations = await locateMany(['*'], {\n path: this.directories,\n onlyDirectories: true,\n });\n\n return locations\n .filter((location) => isBCP47LanguageCode(location.name))\n .map((location) => location.name);\n }\n\n // ------------------------------------------\n\n protected isLoaded(group: string, locale: string) : boolean {\n this.loaded[locale] = this.loaded[locale] || [];\n\n return this.loaded[locale].indexOf(group) !== -1;\n }\n\n protected setIsLoaded(group: string, locale: string) {\n this.loaded[locale] = this.loaded[locale] || [];\n\n this.loaded[locale].push(group);\n }\n\n // ------------------------------------------\n\n async loadGroup(group: string, locale: string) : Promise<Record<string, any>> {\n // only load file once\n if (this.isLoaded(group, locale)) {\n /* istanbul ignore next */\n return {};\n }\n\n this.initLines(group, locale);\n this.setIsLoaded(group, locale);\n\n const locations = await locateMany(\n this.addExtensionPattern(group),\n this.buildLocatorOptionsForLocale(locale),\n );\n\n const loadPromises = locations.map(\n (location) => load(location)\n .then((data) => (data && data.default ? data.default : data)),\n );\n\n const files = await Promise.all(loadPromises);\n if (files.length === 0) {\n return {};\n }\n\n this.data[locale][group] = this.mergeFiles(files);\n\n return this.data[locale][group];\n }\n\n protected buildLocatorOptionsForLocale(locale?: string) : LocatorOptionsInput {\n let directory: string[];\n if (this.directories.length === 0) {\n directory = [locale || 'en'];\n } else {\n directory = this.directories.map(\n (directory) => path.join(directory, locale || 'en'),\n );\n }\n\n return {\n path: directory,\n ignore: [],\n };\n }\n\n protected addExtensionPattern(name: string) {\n return `${name}.{js,mjs,cjs,ts,mts,mjs,json,conf}`;\n }\n\n protected mergeFiles(files: unknown[]) {\n const lineRecord : LinesRecord = {};\n for (let i = 0; i < files.length; i++) {\n const file = files[i];\n\n if (isLineRecord(file)) {\n this.merger(lineRecord, file);\n }\n }\n\n return lineRecord;\n }\n}\n"],"names":["buildConfig","input","directory","Array","isArray","i","length","normalize","process","cwd","FSStore","MemoryStore","loaded","directories","merger","data","options","createMerger","inPlace","array","arrayDistinct","get","context","loadGroup","group","locale","set","getLocales","locations","locateMany","path","onlyDirectories","filter","location","isBCP47LanguageCode","name","map","isLoaded","indexOf","setIsLoaded","push","initLines","addExtensionPattern","buildLocatorOptionsForLocale","loadPromises","load","then","default","files","Promise","all","mergeFiles","join","ignore","lineRecord","file","isLineRecord"],"mappings":";;;;;;;AAWO,SAASA,YAAYC,KAAmB,EAAA;AAC3CA,IAAAA,KAAAA,GAAQA,SAAS,EAAC;IAElB,IAAIC,SAAAA;IACJ,IAAID,KAAAA,CAAMC,SAAS,EAAE;QACjBA,SAAAA,GAAYC,KAAAA,CAAMC,OAAO,CAACH,KAAAA,CAAMC,SAAS,CAAA,GACrCD,KAAAA,CAAMC,SAAS,GACf;AAACD,YAAAA,KAAAA,CAAMC;AAAU,SAAA;AAErB,QAAA,IAAK,IAAIG,CAAAA,GAAI,CAAA,EAAGA,IAAIH,SAAAA,CAAUI,MAAM,EAAED,CAAAA,EAAAA,CAAK;AACvCH,YAAAA,SAAS,CAACG,CAAAA,CAAE,GAAGE,SAAAA,CAAUL,SAAS,CAACG,CAAAA,CAAE,CAAA;AACzC,QAAA;IACJ,CAAA,MAAO;QACHH,SAAAA,GAAY;AAACM,YAAAA,OAAAA,CAAQC,GAAG;AAAG,SAAA;AAC/B,IAAA;IAEA,OAAO;AACHP,QAAAA;AACJ,KAAA;AACJ;;ACVO,MAAMQ,OAAAA,SAAgBC,WAAAA,CAAAA;IACfC,MAAAA;IAEAC,WAAAA;IAEAC,MAAAA;AAEV,IAAA,WAAA,CAAYb,KAAmB,CAAE;AAC7B,QAAA,KAAK,CAAC;AAAEc,YAAAA,IAAAA,EAAM;AAAG,SAAA,CAAA;AAEjB,QAAA,MAAMC,UAAUhB,WAAAA,CAAYC,KAAAA,CAAAA;QAE5B,IAAI,CAACW,MAAM,GAAG,EAAC;AACf,QAAA,IAAI,CAACC,WAAW,GAAGG,OAAAA,CAAQd,SAAS;QAEpC,IAAI,CAACY,MAAM,GAAGG,YAAAA,CAAa;YACvBC,OAAAA,EAAS,IAAA;YACTC,KAAAA,EAAO,IAAA;YACPC,aAAAA,EAAe;AACnB,SA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