UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

645 lines (644 loc) 29.4 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); const fileRepository_1 = require("../repository/fileRepository"); const nedbRepository_1 = require("../repository/nedbRepository"); const model_1 = require("@wbg-mde/model"); const resource_path_1 = require("../resource-path/resource-path"); const app_repo_constants_1 = require("../shared/app.repo.constants"); const app_repo_utility_1 = require("../shared/app.repo.utility"); const project_configuration_1 = require("../configuration/project.configuration"); const path = require("path"); const fs = require("fs"); const _ = require("lodash"); const $JsonRefParser = require("json-schema-ref-parser"); const trace_decorator_1 = require("../decorators/trace.decorator"); let Template = class Template extends fileRepository_1.FileRepository { constructor() { super(); this.templDb = new nedbRepository_1.NeDBRepository(); this.resourcePath = new resource_path_1.ResourcePath(); } getAllTemplates(callback) { try { let types = app_repo_utility_1.App_Repository_Utility.getTypes(); this.templDb.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); this.getAllCustomTemplates((response) => { if (response.result == 'ok') { let templates = response.data || []; let default_templates = this.getAllDefaultTemplates() || []; callback({ result: 'ok', data: [ ...templates, ...default_templates ] }); } else { callback({ result: 'ok', data: this.getAllDefaultTemplates() || [] }); } }); } catch (e) { callback({ result: 'error', messages: e }); app_repo_utility_1.App_Repository_Utility.LogText('Repository > Template > getAllTemplates > ' + e, 3); } } getAllCustomTemplates(callback) { try { this.templDb.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); this.templDb.find(app_repo_constants_1.App_Repository_Constants.userDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.userDataPaths.template_db, (response) => { if (response.result == 'ok') { let templates = response.data || []; this.assignMissingLanguageInfo(templates); callback({ result: 'ok', data: templates }); } else { callback({ result: 'ok', data: [] }); } }); } catch (e) { callback({ result: 'error', messages: e }); app_repo_utility_1.App_Repository_Utility.LogText('Repository > Template > getAllCustomTemplates > ' + e, 3); } } isCustomTemplate(templateName, callback) { this.getAllCustomTemplates((result) => { let allTemplates = result.data || []; let hasTemp = _.find(allTemplates, (template) => { return template.fileName === templateName; }); callback(hasTemp ? true : false); }); } readTemplate(dataType, language, fileName, callback) { try { if (!language) { language = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } let filePath; this.isCustomTemplate(fileName, (result) => { if (result === true) { filePath = this.resourcePath.getCustomTemplatePath(language, fileName); } else { filePath = this.resourcePath.getStudyTemplatePath(dataType, language, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default, true); } this.findFile(filePath, (response) => { if (response.result == 'ok') { callback({ result: 'ok', data: response.data }); } else { callback({ result: 'error', messages: response.messages }); } }); }); } catch (e) { callback({ result: 'error', messages: e }); app_repo_utility_1.App_Repository_Utility.LogText('Repository > Template > readTemplate > ' + e, 3); } } getEditorSectionMappingSchema(dataType, language, afterSchemaGenerated) { try { this.basePath = project_configuration_1.configuaration.masterDataPath; if (!language) { language = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } let editorSectionPath = this.resourcePath.getSectionMappingPath(dataType, language, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor, true); this.findFile(editorSectionPath, (response) => { if (response.result == 'ok') { afterSchemaGenerated({ result: 'ok', schema: response.data }); } else { afterSchemaGenerated({ result: 'error', messages: response.messages }); } }); } catch (e) { afterSchemaGenerated({ result: 'error', messages: e.messages }); } } getTemplateSectionMappingSchema(dataType, language, afterSchemaGenerated) { this.basePath = project_configuration_1.configuaration.masterDataPath; if (!language) { language = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } let templateSectionPath = this.resourcePath.getSectionMappingPath(dataType, language, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template, true); this.findFile(templateSectionPath, (response) => { if (response.result == 'ok') { afterSchemaGenerated({ result: 'ok', schema: response.data }); } else { afterSchemaGenerated({ result: 'error', messages: response.messages }); } }); } getTemplatePath(dataType, language, templateFileNames, excludeFilename, callback) { try { let fileName; let tmpPaths = new Array(); let tmplpath; let selectedType = app_repo_utility_1.App_Repository_Utility.getDataTypeByType(dataType); let isDefaultTemplate; if (!language) { language = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } for (let i = 0; i < templateFileNames.length; i++) { fileName = templateFileNames[i]; tmplpath = undefined; isDefaultTemplate = this.isDefaultTemplate(fileName); if (selectedType && selectedType.custom === true || isDefaultTemplate !== true) { tmplpath = this.resourcePath.getCustomTemplatePath(language, false); } else { tmplpath = this.resourcePath.getStudyTemplatePath(dataType, language, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default, false); } tmpPaths.push(tmplpath); } callback({ result: 'ok', paths: tmpPaths }); } catch (e) { callback({ result: 'error', messages: e }); app_repo_utility_1.App_Repository_Utility.LogText('Repository > Template > getTemplatePath > ' + e, 3); } } getTemplatePathAsync(dataType, language, templateFileNames, excludeFilename) { try { let fileName; let tmpPaths = new Array(); let tmplpath; let selectedType = app_repo_utility_1.App_Repository_Utility.getDataTypeByType(dataType); let isDefaultTemplate; if (!language) { language = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } for (let i = 0; i < templateFileNames.length; i++) { fileName = templateFileNames[i]; tmplpath = undefined; isDefaultTemplate = this.isDefaultTemplate(fileName); if (selectedType && selectedType.custom === true || isDefaultTemplate !== true) { tmplpath = this.resourcePath.getCustomTemplatePath(language, false); } else { tmplpath = this.resourcePath.getStudyTemplatePath(dataType, language, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default, false); } tmpPaths.push(tmplpath); } return { result: 'ok', paths: tmpPaths }; } catch (e) { return { result: 'error', messages: e }; } } isDefaultTemplate(templateName) { let defaultTemplates = this.getAllDefaultTemplates(); let temp = _.find(defaultTemplates, (tempalte) => { return tempalte.fileName == templateName; }); return temp ? true : false; } getTemplateSchema(dataType, language, templateName, afterSchemaGenerated) { if (!language) { language = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } this.readTemplate(dataType, language, templateName, (response) => { if (response.result == 'ok') { let templateSchema = response.data; let self = this; if (templateSchema) { const $jsonRefParser = new $JsonRefParser(); $jsonRefParser.dereference(templateSchema.schema, { resolve: { file: { extension: 'json', read: (file, callback) => { let files = file.url.split('/'); let schemaSection = files[files.length - 1]; let fPath = this.getRefResolveFilePath(schemaSection, dataType); let schema = fs.readFileSync(fPath); return JSON.parse(schema.toString()); } }, external: true } }, (err, fullSchema) => { if (err) { afterSchemaGenerated({ result: 'error', messages: err }); } else { templateSchema.schema = fullSchema; afterSchemaGenerated({ result: 'ok', schema: templateSchema }); } }); } } else { afterSchemaGenerated({ result: 'error', messages: response.messages }); } }); } getRefResolveFilePath(key, dataType) { let fPath; if (!String(key).startsWith("schema_")) { fPath = path.join(this.resourcePath.getBaseSchemaPath(dataType), 'nada', key); } else { fPath = path.join(this.resourcePath.getBaseSchemaPath(dataType), key); } return fPath; } getBaseTemplateSchema(dataType, language, afterSchemaGenerated) { if (!language) { language = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } this.basePath = project_configuration_1.configuaration.masterDataPath; let baseTempPath = this.resourcePath.getStudyTemplatePath(dataType, language, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base, true); this.findFile(baseTempPath, (response) => { if (response.result == 'ok') { let self = this; let templateSchema = response.data; if (templateSchema) { const $jsonRefParser = new $JsonRefParser(); $jsonRefParser.dereference(templateSchema.schema, { resolve: { file: { extension: 'json', read: (file, callback) => { let filess = file.url.split('/'); let schemaSection = filess[filess.length - 1]; let fPath = this.getRefResolveFilePath(schemaSection, dataType); let schema = fs.readFileSync(fPath); return JSON.parse(schema.toString()); } }, external: true } }, (err, fullSchema) => { if (err) { afterSchemaGenerated({ result: 'error', messages: err }); } else { templateSchema.schema = fullSchema; afterSchemaGenerated({ result: 'ok', schema: templateSchema }); } }); } } else { afterSchemaGenerated({ result: 'error', messages: response.messages }); } }); } writeTemplate(metadata, template, callback) { let templateMetadata = new model_1.TemplateMetadata(metadata); templateMetadata._id = (templateMetadata._id == undefined || templateMetadata._id == "") ? app_repo_utility_1.App_Repository_Utility.generateGUID() : templateMetadata._id; templateMetadata.fileName = templateMetadata.name + '_template_' + templateMetadata.type + "_" + templateMetadata.language; templateMetadata.created = new Date(); this.templDb.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); if (metadata._id != "" && metadata._id != undefined) { this .templDb .update(app_repo_constants_1.App_Repository_Constants.userDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.userDataPaths.template_db, { _id: templateMetadata._id }, templateMetadata, (response_metadata) => { if (response_metadata.result == 'ok') { this.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); let paths = this .resourcePath .getCustomTemplatePath(templateMetadata.language, templateMetadata.fileName); this.createTemplate(paths, template, (response) => { callback(response); }); } else { callback(response_metadata); } }); } else { this .templDb .create(app_repo_constants_1.App_Repository_Constants.userDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.userDataPaths.template_db, templateMetadata, (response_metadata) => { if (response_metadata.result == 'ok') { this.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); let paths = this .resourcePath .getCustomTemplatePath(templateMetadata.language, templateMetadata.fileName); this.createTemplate(paths, template, (response) => { callback(Object.assign(response, { 'metadata': templateMetadata })); }); } else { callback(response_metadata); } }); } } removeTemplate(id, callback) { this.templDb.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); this .templDb .findById(app_repo_constants_1.App_Repository_Constants.userDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.userDataPaths.template_db, id, (response) => { if (response.result == 'ok') { let fileName = response.data.fileName; this .templDb .deleteById(app_repo_constants_1.App_Repository_Constants.userDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.userDataPaths.template_db, id, (response_delete) => { this.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); let deletePaths = this .resourcePath .getCustomTemplatePath(response.data.language, fileName); this.deleteTemplate(deletePaths, (response) => { callback(response_delete); }); }); } else { callback(response); } }); } deleteTemplateByLanguage(language, callback) { let templatePath = this .resourcePath .getTemplateDBPath(); this .templDb .deleteByLanguage(templatePath, language, (response_delete) => { callback(response_delete); }); } mergeTemplateProps(templateSchema) { let template = templateSchema.form; let schema = templateSchema.schema; let self = this; _.forEach(template, function (tmplNodes) { self.getRequiredProperty(tmplNodes, schema); }); return templateSchema; } getRequiredProperty(node, schema) { if (node.required) { this.setRequiredProperty(node.key, schema); } if (node.items && node.items instanceof Array) { let self = this; _.forEach(node.items, function (items) { self.getRequiredProperty(items, schema); }); } } setRequiredProperty(key, schema) { let props = key.split('.'), current = schema; if (props.length == 1) { if (schema.required && schema.required instanceof Array) { schema .required .push(props[0]); } else { schema.required = new Array(); schema .required .push(props[0]); } } else { let propGroups = _.chunk(props, props.length - 1); let parentProps = propGroups[0]; let currProp = propGroups[1][0]; _.forEach(parentProps, function (prop) { if (current) { prop = prop.replace('[]', ''); if (current.type === "object") { if (current.properties) { current = current.properties[prop]; } else { current = null; } } else if (current.type === "array") { if (current.items) { current = current.items; } else { current = null; } } } }); if (current) { if (current.required && current.required instanceof Array) { if (current.required.indexOf(currProp) == -1) { current .required .push(currProp); } } else { current.required = new Array(); current .required .push(currProp); } } } } filterTemplate(type, usertemplates, filter, callback) { this.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); this .templDb .findByFilter(app_repo_constants_1.App_Repository_Constants.userDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.userDataPaths.template_db, filter, (response) => { if (response.result == 'ok') { callback({ result: 'ok', data: response.data }); } else { callback({ result: 'error', messages: response.messages }); } }); } writeCustomTemplate(templateMetadata, schema, callback) { this.templDb.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); this .templDb .create(app_repo_constants_1.App_Repository_Constants.userDataPaths.customTemplate_path, app_repo_constants_1.App_Repository_Constants.userDataPaths.customTemplate_db, templateMetadata, (response_metadata) => { if (response_metadata.result == 'ok') { if (templateMetadata._id != "" && templateMetadata._id != undefined) this.templDb.update(app_repo_constants_1.App_Repository_Constants.userDataPaths.customTemplate_path, app_repo_constants_1.App_Repository_Constants.userDataPaths.template_db, { _id: templateMetadata._id }, templateMetadata, (response_metadata) => { if (response_metadata.result == 'ok') { this.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); this.create(app_repo_constants_1.App_Repository_Constants.userDataPaths.customTemplate_path, templateMetadata.fileName, schema, (response) => { callback(response); }); } else { callback(response_metadata); } }); else { this.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); this.create(app_repo_constants_1.App_Repository_Constants.userDataPaths.customTemplate_path, templateMetadata.fileName, schema, (response) => { callback(response); }); } } else { callback(response_metadata); } }); } getTemplateUsedProjects(template, callback) { this.templDb.basePath = path.join(project_configuration_1.configuaration.userDataPath, app_repo_constants_1.App_Repository_Constants.userDataPaths.cur_user); this .templDb .findByFilter(app_repo_constants_1.App_Repository_Constants.projectHeader.base_folder, app_repo_constants_1.App_Repository_Constants.projectHeader.db_name, { template: template }, (response) => { callback(response); }); } getAllI8nResources(keysOnly) { let resources = app_repo_utility_1.App_Repository_Utility.getAlli18nResources(); let languages; if (resources && keysOnly === true) { languages = Object.keys(resources); } else { languages = resources; } return languages; } getAllDefaultTemplates() { let languages = this.getAllI8nResources(true); let types = app_repo_utility_1.App_Repository_Utility.getTypes(); let templates = new Array(); let metadata; for (let i = 0; i < languages.length; i++) { for (let j = 0; j < types.length; j++) { metadata = this.newDefTemplateMetadata(); metadata.type = types[j].type; metadata.fileName = app_repo_utility_1.App_Repository_Utility.formatFileNameByLanguage(types[j].template, languages[i], false); metadata.language = languages[i]; templates.push(metadata); } } let customTypes = app_repo_utility_1.App_Repository_Utility.getCustomTypes(); for (let j = 0; j < customTypes.length; j++) { metadata = this.newDefTemplateMetadata(); metadata.type = customTypes[j].type; metadata.fileName = this .resourcePath .getProjectTemplateName(customTypes[j].type, customTypes[j].language, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default, false); metadata.language = customTypes[j].language; templates.push(metadata); } return templates; } newDefTemplateMetadata() { return { name: 'Default', type: undefined, fileName: undefined, systemDefined: true, language: undefined }; } assignMissingLanguageInfo(templates) { for (let i = 0; i < templates.length; i++) { if (!templates[i].language) { templates[i].language = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } } } }; Template = __decorate([ trace_decorator_1.Trace({ exclude: [ 'newDefTemplateMetadata', 'getRequiredProperty', 'setRequiredProperty', 'getRequiredProperty', 'mergeTemplateProps' ], params: { writeTemplate: [0, 1], writeCustomTemplate: [0, 1] } }) ], Template); exports.Template = Template;