@contentstack/datasync-content-store-filesystem
Version:
Datasync content store library - saves data in filesystem
77 lines (76 loc) • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildLocalePath = exports.normalizeBaseDir = exports.removeUnwantedKeys = exports.getPathKeys = exports.filter = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const filterKeys = ["_content_type", "_checkpoint", "_type"];
const filter = (data) => {
const result = {};
for (const key in data) {
if (filterKeys.indexOf(key) === -1) {
result[key] = data[key];
}
}
return result;
};
exports.filter = filter;
const getPathKeys = (patternKeys, json) => {
const pathKeys = [];
for (let i = 0, keyLength = patternKeys.length; i < keyLength; i++) {
if (patternKeys[i].charAt(0) === ":") {
let k = patternKeys[i].substring(1);
const idx = k.indexOf(".json");
if (~idx) {
k = k.slice(0, idx);
}
if (json[k]) {
pathKeys.push(json[k]);
}
else {
throw new TypeError(`The key ${k} did not exist on ${JSON.stringify(json)}`);
}
}
else {
pathKeys.push(patternKeys[i]);
}
}
return pathKeys;
};
exports.getPathKeys = getPathKeys;
const removeUnwantedKeys = (unwanted, json) => {
for (const key in unwanted) {
if (unwanted[key] && json.hasOwnProperty(key)) {
delete json[key];
}
}
return json;
};
exports.removeUnwantedKeys = removeUnwantedKeys;
const normalizeBaseDir = (config) => {
if ((0, path_1.isAbsolute)(config.baseDir)) {
if (!(0, fs_1.existsSync)(config.baseDir)) {
(0, fs_1.mkdirSync)(config.baseDir, { recursive: true });
}
}
else {
const projectDir = (0, path_1.join)(__dirname, "..", "..", "..", "..", "..");
const contentDir = (0, path_1.join)(sanitizePath(projectDir), sanitizePath(config.baseDir));
if (!(0, fs_1.existsSync)(contentDir)) {
(0, fs_1.mkdirSync)(contentDir, { recursive: true });
}
}
return config;
};
exports.normalizeBaseDir = normalizeBaseDir;
const buildLocalePath = (appConfig) => {
const localePath = (0, path_1.join)(sanitizePath(appConfig.baseDir), sanitizePath(appConfig.internal.locale));
const localePathArr = localePath.split(path_1.sep);
localePathArr.splice(localePathArr.length - 1);
const localeFolderPath = path_1.join.apply(this, localePathArr);
if (!(0, fs_1.existsSync)(localeFolderPath)) {
(0, fs_1.mkdirSync)(localeFolderPath, { recursive: true });
}
return localePath;
};
exports.buildLocalePath = buildLocalePath;
const sanitizePath = (str) => str === null || str === void 0 ? void 0 : str.replace(/^([\/\\]){2,}/, "./").replace(/[\/\\]+/g, "/").replace(/(\.\.(\/|\\|$))+/g, "");