UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

264 lines (263 loc) 13.2 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 app_repo_constants_1 = require("../shared/app.repo.constants"); const app_repo_utility_1 = require("../shared/app.repo.utility"); const resource_path_1 = require("../resource-path/resource-path"); const project_configuration_1 = require("../configuration/project.configuration"); const fs = require("fs"); const mkdirp = require("mkdirp"); const _ = require("lodash"); const trace_decorator_1 = require("../decorators/trace.decorator"); let SchemaImport = class SchemaImport { constructor() { this.resourcePath = new resource_path_1.ResourcePath(); } importCustomSchema(filePath, options) { return new Promise((resolve, reject) => { try { this.fileData = JSON.parse(this.readFile(filePath)); this.schemaLanguage = this.fileData.language; let allTypes = app_repo_utility_1.App_Repository_Utility.getTypes(true); let isExist = _.find(allTypes, (dataType) => { return dataType.type === this.fileData.schemaType; }); if (isExist) { reject({ message: 'datatype_duplicate', appenders: [this.fileData.schemaType] }); return; } if (!this.schemaLanguage) { this.schemaLanguage = app_repo_constants_1.App_Repository_Constants.defaultLanguage; } if (!this.hasI18nResource(this.schemaLanguage)) { reject({ message: 'schema_imp_lang_mis' }); return; } this.newDataType = this.fileData.schemaType; this.schemaData = this.fileData.schemaDefinitions.schema; this.formData = this.fileData.schemaDefinitions.form; if (!this.newDataType) { reject({ message: 'schema_imp_type_mis' }); return; } if (!this.schemaData) { reject({ message: 'schema_imp_schema_mis' }); return; } if (!this.formData) { reject({ message: 'schema_imp_form_mis' }); return; } this.writeDataTypeSchema(); this.writeDataTypeTemplates(); this.writeDataTypeSectionMappings(); this.updateConfig(1); this.createIconsDirectory(); resolve(); } catch (ex) { reject(ex.message); } }); } writeDataTypeSchema() { this.createDirectory(this.resourcePath.getCustomSchemaPath()); let schema = JSON.stringify(this.schemaData, null, 2); let filePath = this.resourcePath.getCustomSchemaPath(this.newDataType, this.schemaLanguage, true); this.writeFile(filePath, schema); } writeDataTypeTemplates() { let templateData = JSON.stringify({ schema: this.schemaData, form: this.formData }, null, 2); let defaultTempPath = this.resourcePath.getStudyTemplatePath(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default); this.createDirectory(defaultTempPath); this.writeFile(this.resourcePath.getStudyTemplatePath(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default, true), templateData); let baseTempPath = this.resourcePath.getStudyTemplatePath(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base); this.createDirectory(baseTempPath); this.writeFile(this.resourcePath.getStudyTemplatePath(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base, true), templateData); } writeDataTypeSectionMappings() { let dnlncorMapping; if (this.fileData.includeDublinCore === true) { dnlncorMapping = this.getDublincoreMapping(); } let editorSecData = this.createEditorSectionMapping(dnlncorMapping); let editorSecPath = this.resourcePath.getSectionMappingPath(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor); this.createDirectory(editorSecPath); this.writeFile(this.resourcePath.getSectionMappingPath(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor, true), editorSecData); let templateSecData = this.createTemplateSectionMapping(); let tempSecPath = this.resourcePath.getSectionMappingPath(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template); this.createDirectory(tempSecPath); this.writeFile(this.resourcePath.getSectionMappingPath(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template, true), templateSecData); } getDublincoreMapping() { let mappings = this.readFile(this.resourcePath.getSectionMappingPath(app_repo_constants_1.App_Repository_Constants.schemaTypes.dbln_core, app_repo_constants_1.App_Repository_Constants.defaultLanguage, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor, true)); mappings = JSON.parse(mappings); return _.find(mappings.mappings, (item) => { return item.type === app_repo_constants_1.App_Repository_Constants.schemaTypes.dbln_core; }); } createTemplateSectionMapping() { let dataType = this.toTitleCase(this.newDataType); let items = []; let order = 1; _.each(this.formData, (res) => { items.push({ "key": res.key, "type": "fieldset", "expandable": false, "expanded": true, "title": res.title, "order": order }); order++; }); return JSON.stringify({ version: "1.0", mappings: [{ "type": dataType, "items": items }] }, null, 2); } createEditorSectionMapping(dblngCoreMapping) { let dataType = this.toTitleCase(this.newDataType); var items = []; var order = 1; _.each(this.formData, (res) => { items.push({ "key": res.key, "title": res.title, "order": order, "useTemplate": true }); order++; }); if (dblngCoreMapping) { items.push({ "key": "extResources", "title": "External Resources", "useTemplate": false, "isVirtual": true, "menu": true, "treeView": { "cssClasses": { "opened": "ast-ddi-extrsrc-opened ast-node-menucontext", "default": "ast-ddi-extrsrc-default ast-node-menucontext" }, "type": "resource" } }); } let mappings = [{ "type": dataType, "items": items }]; if (dblngCoreMapping) { mappings.push(dblngCoreMapping); } return JSON.stringify({ version: "1.0", mappings: mappings }, null, 2); } createIconDirectory(language) { this.createDirectory(this.resourcePath.getIconPath()); } updateConfig(action, dataType) { if (action === 1) { let item = { type: this.newDataType, custom: true, fontIcon: this.fileData.iconPath + ' buttonImg', language: this.schemaLanguage, title: this.newDataType.toUpperCase(), template: this.resourcePath.getProjectTemplateName(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default, false), base: this.resourcePath.getProjectTemplateName(this.newDataType, this.schemaLanguage, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base, false), schema: this.resourcePath.getCustomSchemaName(this.newDataType, this.schemaLanguage, false), dblngcore: this.fileData.includeDublinCore }; if (!project_configuration_1.configuaration.customDataTypes) { project_configuration_1.configuaration.customDataTypes = []; } project_configuration_1.configuaration.customDataTypes.push(item); project_configuration_1.configuaration.customDataTypes = project_configuration_1.configuaration.customDataTypes; project_configuration_1.configuaration.refresh(); } else if (action === 2) { let types = project_configuration_1.configuaration.customDataTypes; _.remove(types, (dType) => { return dataType == dType.type; }); project_configuration_1.configuaration.customDataTypes = types; project_configuration_1.configuaration.refresh(); } } createIconsDirectory() { let path = this.resourcePath.getIconPath(); this.createDirectory(path); } hasI18nResource(language) { let resources = app_repo_utility_1.App_Repository_Utility.getAlli18nResources(); return resources[language] ? true : false; } readFile(filePath) { return fs.readFileSync(filePath, 'utf8'); } writeFile(filePath, content) { return new Promise((resolve, reject) => { fs.writeFile(filePath, content, function (err) { if (err) { reject(err); } resolve(); }); }); } deleteFile(filePath) { return new Promise((resolve, reject) => { fs.unlink(filePath, (err) => { if (err) { reject(err); } resolve(); }); }); } createDirectory(directoryPath) { if (!fs.existsSync(directoryPath)) { mkdirp.sync(directoryPath); } } toTitleCase(str) { return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); } deleteCustomSchema(dataTypeName) { return new Promise((resolve, reject) => { try { let customTypes = project_configuration_1.configuaration.customDataTypes; let dataTypeMeta = _.find(customTypes, (dType) => { return dType.type === dataTypeName; }); this.updateConfig(2, dataTypeName); this.deleteMainSchemaFile(dataTypeMeta); this.deleteTemplateFiles(dataTypeMeta); this.deleteSectionMappingFiles(dataTypeMeta); resolve(); } catch (e) { reject(e); } }); } deleteMainSchemaFile(dataType) { let filePath = this.resourcePath.getCustomSchemaPath(dataType.type, dataType.language, true, true); this.deleteFile(filePath); } deleteTemplateFiles(dataType) { this.deleteFile(this.resourcePath.getStudyTemplatePath(dataType.type, dataType.language, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default, true)); this.deleteFile(this.resourcePath.getStudyTemplatePath(dataType.type, dataType.language, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base, true)); } deleteSectionMappingFiles(dataType) { this.deleteFile(this.resourcePath.getSectionMappingPath(dataType.type, dataType.language, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor, true)); this.deleteFile(this.resourcePath.getSectionMappingPath(dataType.type, dataType.language, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template, true)); } }; SchemaImport = __decorate([ trace_decorator_1.Trace({ exclude: ['createDirectory', 'writeFile'] }) ], SchemaImport); exports.SchemaImport = SchemaImport;