@wbg-mde/repository
Version:
Managing all common method for file system CRUD operations.
225 lines (224 loc) • 10.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const app_repo_utility_1 = require("../shared/app.repo.utility");
const app_repo_constants_1 = require("../shared/app.repo.constants");
const project_configuration_1 = require("../configuration/project.configuration");
const path = require("path");
const fs = require("fs");
const mkdirp = require("mkdirp");
const os = require("os");
class ResourcePath {
constructor() {
this.defaultExtention = ".json";
this.dbExtention = '.db';
this.assetsDirectory = project_configuration_1.configuaration.masterDataPath;
this.userDirectory = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user);
}
getI18nResourcePath(isoCode, includeFileName) {
if (!isoCode) {
isoCode = app_repo_constants_1.App_Repository_Constants.defaultLanguage;
}
let resorcePath;
if (isoCode === app_repo_constants_1.App_Repository_Constants.defaultLanguage) {
resorcePath = path.join(project_configuration_1.configuaration.masterDataPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.i18n, isoCode);
}
else {
resorcePath = path.join(this.userDirectory, app_repo_constants_1.App_Repository_Constants.masterDataPaths.i18n, isoCode);
}
if (includeFileName === true) {
resorcePath = path.join(resorcePath, (isoCode.concat(this.defaultExtention)));
}
return resorcePath;
}
getSectionMappingPath(dataType, isoCode, sectionType, setExtension) {
let paths;
let secType = sectionType;
let selectedDataType = app_repo_utility_1.App_Repository_Utility.getDataTypeByType(dataType);
if (!selectedDataType || (selectedDataType && selectedDataType.custom === true) || isoCode !== app_repo_constants_1.App_Repository_Constants.defaultLanguage) {
paths = [
this.userDirectory,
app_repo_constants_1.App_Repository_Constants.masterDataPaths.section_mapping,
secType,
isoCode
];
}
else {
paths = [
project_configuration_1.configuaration.masterDataPath,
app_repo_constants_1.App_Repository_Constants.masterDataPaths.section_mapping,
secType,
isoCode
];
}
if (setExtension === true) {
if (selectedDataType && selectedDataType.custom !== true) {
dataType = undefined;
}
paths.push(this.getSectionMappingFileName(dataType, isoCode, sectionType, setExtension));
}
return path.join(...paths);
}
getStudyTemplatePath(dataType, isoCode, templateType, setExtension) {
let paths;
let tempType = templateType;
let selectedDataType = app_repo_utility_1.App_Repository_Utility.getDataTypeByType(dataType);
if (!selectedDataType || (selectedDataType && selectedDataType.custom === true) || isoCode !== app_repo_constants_1.App_Repository_Constants.defaultLanguage) {
paths = [
this.userDirectory,
app_repo_constants_1.App_Repository_Constants.masterDataPaths.template_path,
tempType,
isoCode
];
}
else {
paths = [
project_configuration_1.configuaration.masterDataPath,
app_repo_constants_1.App_Repository_Constants.masterDataPaths.template_path,
tempType,
isoCode
];
}
if (setExtension === true) {
paths.push(this.getProjectTemplateName(dataType, isoCode, tempType, setExtension));
}
return path.join(...paths);
}
getCustomTemplatePath(isoCode, fileName) {
let paths = [
this.userDirectory,
app_repo_constants_1.App_Repository_Constants.userDataPaths.template_path,
app_repo_constants_1.App_Repository_Constants.userDataPaths.user_template,
isoCode
];
if (fileName) {
paths.push(fileName.concat(this.defaultExtention));
}
return path.join(...paths);
}
getTemplateDBPath() {
let paths = [
this.userDirectory,
app_repo_constants_1.App_Repository_Constants.userDataPaths.template_path,
app_repo_constants_1.App_Repository_Constants.userDataPaths.template_db
];
return path.join(...paths).concat(this.dbExtention);
}
getCustomSchemaPath(dataType, isoCode, appendFileName, extention) {
let paths = [
this.userDirectory,
app_repo_constants_1.App_Repository_Constants.userDataPaths.schema_path
];
if (appendFileName === true) {
paths.push(this.getCustomSchemaName(dataType, isoCode, extention));
}
return path.join(...paths);
}
getCustomSchemaName(dataType, isoCode, extention) {
return app_repo_constants_1.App_Repository_Constants.schemaTypes.custom_schema_prefix.concat(dataType, "_", isoCode, (extention !== false ? this.defaultExtention : ""));
}
getProjectTemplateName(dataType, isoCode, templateType, extention) {
let prefix = app_repo_constants_1.App_Repository_Constants.templateType.custom_default_prefix;
if (templateType === app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base) {
prefix = app_repo_constants_1.App_Repository_Constants.templateType.custom_base_prefix;
}
return prefix.concat("_", dataType, "_", isoCode, (extention === true ? this.defaultExtention : ""));
}
getSectionMappingFileName(dataType, isoCode, sectionType, extention) {
let prefix = app_repo_constants_1.App_Repository_Constants.schemaTypes.editor_mapping;
if (sectionType === app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template) {
prefix = app_repo_constants_1.App_Repository_Constants.schemaTypes.template_mapping;
}
if (dataType) {
prefix = prefix.concat("_", dataType);
}
return prefix.concat("_", isoCode, (extention === true ? this.defaultExtention : ""));
}
getBaseSchemaPath(dataType) {
let selectedDataType = app_repo_utility_1.App_Repository_Utility.getDataTypeByType(dataType);
let paths = new Array();
if (selectedDataType && selectedDataType.custom === true) {
paths.push(this.userDirectory, app_repo_constants_1.App_Repository_Constants.masterDataPaths.schema_path);
}
else {
paths.push(project_configuration_1.configuaration.masterDataPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.schema_path);
}
return path.join(...paths);
}
getIconPath(fileName) {
return path.join(this.userDirectory, app_repo_constants_1.App_Repository_Constants.userDataPaths.custom_icons, (fileName ? fileName : ""));
}
getDefaultIconPath(category, fileName) {
let paths = [project_configuration_1.configuaration.masterDataPath, app_repo_constants_1.App_Repository_Constants.assetDirectories.rootIconDir];
if (category === 1) {
paths.push(app_repo_constants_1.App_Repository_Constants.assetDirectories.dataTypeIcons);
}
if (fileName) {
paths.push(fileName);
}
return path.join(...paths);
}
getDataTypeIcon(dataType, base64) {
let dataTypeMeta = app_repo_utility_1.App_Repository_Utility.getDataTypeByType(dataType);
let filePath;
let prefixType = "";
if (dataTypeMeta) {
if (dataTypeMeta.custom === true) {
filePath = this.getIconPath(dataTypeMeta.icon);
}
else {
filePath = this.getDefaultIconPath(1, dataTypeMeta.icon);
}
}
else {
filePath = this.getDefaultIconPath(1, app_repo_constants_1.App_Repository_Constants.generalDataTypeIcon);
}
if (!fs.existsSync(filePath)) {
filePath = this.getDefaultIconPath(1, app_repo_constants_1.App_Repository_Constants.generalDataTypeIcon);
}
if (path.extname(filePath) === "jpg") {
prefixType = "data:image/jpg;base64,";
}
else {
prefixType = "data:image/png;base64,";
}
if (base64 === true) {
let bitmap = fs.readFileSync(filePath);
return prefixType.concat(Buffer.from(bitmap).toString('base64'));
}
else {
return filePath;
}
}
getProjectDirectory(projectId) {
return path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user, app_repo_constants_1.App_Repository_Constants.projectHeader.base_folder, projectId);
}
getImportedProjectSaveDirectory() {
let directory = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user, app_repo_constants_1.App_Repository_Constants.userDataPaths.wbm_repository);
if (!fs.existsSync(directory)) {
mkdirp.sync(directory);
}
return directory;
}
getTempDirectory() {
return path.join(os.tmpdir(), app_repo_constants_1.App_Repository_Constants.tempFolder.root);
}
getRScriptPath(RScript) {
return require.resolve("@wbg-mde/r-factory").replace("index.js", path.join(app_repo_constants_1.App_Repository_Constants.masterDataPaths.r_scripts, RScript));
}
getLogFilePath() {
let temp = this.getTempDirectory();
let paths = [];
paths[0] = path.join(temp, app_repo_constants_1.App_Repository_Constants.tempFolder.appLogRoot);
paths[1] = path.join(paths[0], "{0}" + app_repo_constants_1.App_Repository_Constants.fileExtention.log);
return paths;
}
getTodaysLog() {
let today = app_repo_utility_1.App_Repository_Utility.getFormattedDate();
let filePaths = this.getLogFilePath();
return app_repo_utility_1.App_Repository_Utility.formatString(filePaths[1], [today]);
}
getDefaultLocalRepository() {
return path.join(this.userDirectory, app_repo_constants_1.App_Repository_Constants.userDataPaths.local_variable_repositoy, app_repo_constants_1.App_Repository_Constants.variableRepositoryFiles.local);
}
}
exports.ResourcePath = ResourcePath;