i18n-node-server
Version:
NodeJS http server to store your internationalized phrases
101 lines • 4.33 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FsService = void 0;
const country_codes_1 = require("../country-codes/country-codes");
const file_writer_1 = require("./file-writer");
const fs = __importStar(require("fs"));
const valid_ident_1 = require("./utils/valid-ident");
const errors_1 = require("./errors");
const file_reader_1 = require("./file-reader");
const fs_1 = require("fs");
const path_1 = require("path");
const pick_1 = require("./utils/pick");
class FsService {
constructor(dataDirPath) {
this.dataDirPath = dataDirPath;
if (!fs.existsSync(dataDirPath)) {
fs.mkdirSync(dataDirPath);
}
}
writeRecords(ident, records) {
const identIsValid = (0, valid_ident_1.validateIdent)(ident);
if (!identIsValid) {
return errors_1.IDENT_NOT_VALID;
}
const empty = Object.assign({}, FsService.emptyFileContent);
for (let countryCode in records) {
if (FsService.emptyFileContent.hasOwnProperty(countryCode)) {
empty[countryCode] = records[countryCode];
}
}
const filePath = (0, path_1.join)(this.dataDirPath, ident + ".json");
if ((0, fs_1.existsSync)(filePath)) {
return errors_1.IDENT_EXISTS;
}
file_writer_1.FileWriter.writeToFile(filePath, empty);
}
updateRecords(ident, records) {
const identIsValid = (0, valid_ident_1.validateIdent)(ident);
if (!identIsValid) {
return errors_1.IDENT_NOT_VALID;
}
const filePath = (0, path_1.join)(this.dataDirPath, ident + ".json");
const current = file_reader_1.FileReader.readFile(filePath);
if (current instanceof Error) {
return current;
}
for (let countryCode in records) {
if (FsService.emptyFileContent.hasOwnProperty(countryCode)) {
current[countryCode] = records[countryCode];
}
}
file_writer_1.FileWriter.writeToFile(filePath, current);
}
updateRecord(ident, cc, value) {
return this.updateRecords(ident, { [cc]: value });
}
getAllRecords(filterCC = []) {
const allRecords = (0, fs_1.readdirSync)(this.dataDirPath, { withFileTypes: true })
.filter(dirent => dirent.isFile() && dirent.name.endsWith(".json"))
.reduce((acc, dirent) => {
const ident = dirent.name.replace(/.json$/g, "");
const fileContent = file_reader_1.FileReader.readFile((0, path_1.join)(this.dataDirPath, dirent.name));
acc[ident] = (0, pick_1.pick)(fileContent, ...filterCC);
return acc;
}, { "timestamp-i18n-tool": new Date().toISOString() });
return allRecords;
}
getRecordsByIdent(ident, filterCC = []) {
const result = file_reader_1.FileReader.readFile((0, path_1.join)(this.dataDirPath, ident + ".json"));
return (0, pick_1.pick)(result, ...filterCC);
}
}
exports.FsService = FsService;
FsService.emptyFileContent = country_codes_1.countryCodes.reduce((acc, val) => {
acc[val] = "";
return acc;
}, {});
//# sourceMappingURL=fs-service.js.map