@ilingo/fs
Version:
This is a lightweight library for translation.
124 lines (118 loc) • 3.79 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var locter = require('locter');
var path = require('node:path');
var smob = require('smob');
var ilingo = require('ilingo');
var pathe = require('pathe');
var process = require('node:process');
function buildConfig(input) {
input = input || {};
let directory;
if (input.directory) {
directory = Array.isArray(input.directory) ? input.directory : [
input.directory
];
for(let i = 0; i < directory.length; i++){
directory[i] = pathe.normalize(directory[i]);
}
} else {
directory = [
process.cwd()
];
}
return {
directory
};
}
class FSStore extends ilingo.MemoryStore {
// ------------------------------------------
async get(context) {
await this.loadGroup(context.group, context.locale);
return super.get(context);
}
async set(context) {
return super.set(context);
// todo: write to file!
}
// ------------------------------------------
async getLocales() {
const locations = await locter.locateMany([
'*'
], {
path: this.directories,
onlyDirectories: true
});
return locations.filter((location)=>ilingo.isBCP47LanguageCode(location.name)).map((location)=>location.name);
}
// ------------------------------------------
isLoaded(group, locale) {
this.loaded[locale] = this.loaded[locale] || [];
return this.loaded[locale].indexOf(group) !== -1;
}
setIsLoaded(group, locale) {
this.loaded[locale] = this.loaded[locale] || [];
this.loaded[locale].push(group);
}
// ------------------------------------------
async loadGroup(group, locale) {
// only load file once
if (this.isLoaded(group, locale)) {
/* istanbul ignore next */ return {};
}
this.initLines(group, locale);
this.setIsLoaded(group, locale);
const locations = await locter.locateMany(this.addExtensionPattern(group), this.buildLocatorOptionsForLocale(locale));
const loadPromises = locations.map((location)=>locter.load(location).then((data)=>data && data.default ? data.default : data));
const files = await Promise.all(loadPromises);
if (files.length === 0) {
return {};
}
this.data[locale][group] = this.mergeFiles(files);
return this.data[locale][group];
}
buildLocatorOptionsForLocale(locale) {
let directory;
if (this.directories.length === 0) {
directory = [
locale || 'en'
];
} else {
directory = this.directories.map((directory)=>path.join(directory, locale || 'en'));
}
return {
path: directory,
ignore: []
};
}
addExtensionPattern(name) {
return `${name}.{js,mjs,cjs,ts,mts,mjs,json,conf}`;
}
mergeFiles(files) {
const lineRecord = {};
for(let i = 0; i < files.length; i++){
const file = files[i];
if (ilingo.isLineRecord(file)) {
this.merger(lineRecord, file);
}
}
return lineRecord;
}
constructor(input){
super({
data: {}
});
const options = buildConfig(input);
this.loaded = {};
this.directories = options.directory;
this.merger = smob.createMerger({
inPlace: true,
array: true,
arrayDistinct: true
});
}
}
exports.FSStore = FSStore;
exports.default = FSStore;
module.exports = Object.assign(exports.default, exports);
//# sourceMappingURL=index.cjs.map