UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

204 lines (203 loc) 9.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 path = require("path"); const fs = require("fs"); const copydir = require("copy-dir"); const DecompressZip = require("decompress-zip"); const i18n_core_1 = require("./i18n.core"); const app_repo_utility_1 = require("../shared/app.repo.utility"); const app_repo_constants_1 = require("../shared/app.repo.constants"); const model_1 = require("@wbg-mde/model"); const timers_1 = require("timers"); const trace_decorator_1 = require("../decorators/trace.decorator"); let I18nImport = class I18nImport extends i18n_core_1.I18nCore { constructor() { super(); } importResource(filePath, igoneDuplicate) { return new Promise((resolve, reject) => { try { this.importStatus = new model_1.I18nExportImportStatus({}); let importTemp = this.getTempDirectory(); this.unzipFile(filePath, importTemp).then(() => { this.readResourceInfo(); if (igoneDuplicate !== true && this.isDulicateResource() === true) { reject({ result: "duplicate", metadata: this.importedResource, filePath: filePath }); return; } else { this.importResourceFile(); this.importSectionMappingFiles(); this.importTemplateFiles(); this.checkForImport((status) => { resolve({ result: "ok", data: this.importedResource }); }); } }).catch(() => { reject({ result: "error" }); }); } catch (e) { reject({ result: "error" }); } }); } getTempDirectory() { return path.join(this.resourcePath.getTempDirectory(), app_repo_constants_1.App_Repository_Constants.tempFolder.importI18n); } unzipFile(filePath, destinationPath) { return new Promise((resolve, reject) => { try { let unzipper = new DecompressZip(filePath); unzipper.on('error', (err) => { app_repo_utility_1.App_Repository_Utility.LogText("Extract Error > " + err, 3); reject(); }); unzipper.on('extract', (log) => { resolve(); }); unzipper.on('progress', (fileIndex, fileCount) => { }); unzipper.extract({ path: destinationPath }); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Import Resource > unzipFile > ' + e, 3); reject(); } }); } readResourceInfo() { try { let tempPath = this.getTempDirectory(); this.importMetadata = JSON.parse(fs.readFileSync(path.join(tempPath, this.resInfoFile), 'utf8')); this.importedResource = new model_1.I18nResource(this.importMetadata); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Import Resource> readResourceInfo > ' + e, 3); } } isDulicateResource() { try { let resources = app_repo_utility_1.App_Repository_Utility.getAlli18nResources(); return resources[this.importedResource.isoCode] ? true : false; } catch (e) { return false; } } importResourceFile() { let fromPath = this.getTempDirectory(); let isoCode = this.importMetadata.isoCode; let toPath = this.geti18nResourcePath(this.importMetadata.isoCode, false); copydir(fromPath, toPath, (stat, filepath, filename) => { if (stat === 'file' && filename.indexOf(isoCode) !== -1) { return true; } return false; }, (err) => { if (err) { this.importStatus.resourceFile = false; return; } this.importStatus.resourceFile = true; }); } importSectionMappingFiles() { let tempPath = this.getTempDirectory(); let isoCode = this.importMetadata.isoCode; let fromPath1 = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.section_mapping, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor); let toPath1 = this.getSectionMappingPath(isoCode, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor, false); copydir(fromPath1, toPath1, (stat, filepath, filename) => { if (stat === 'file') { return true; } return false; }, (err) => { if (err) { this.importStatus.editorSectionFile = false; return; } this.importStatus.editorSectionFile = true; }); let fromPath2 = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.section_mapping, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template); let toPath2 = this.getSectionMappingPath(isoCode, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template, false); copydir(fromPath2, toPath2, (stat, filepath, filename) => { if (stat === 'file') { return true; } return false; }, (err) => { if (err) { this.importStatus.editorSectionFile = false; return; } this.importStatus.templateSectionFile = true; }); } importTemplateFiles() { let tempPath = this.getTempDirectory(); let isoCode = this.importMetadata.isoCode; let toPath1 = this.getResourceTemplatePath(isoCode, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base); let fromPath1 = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base); copydir(fromPath1, toPath1, (stat, filepath, filename) => { if (stat === 'file') { return true; } return false; }, (err) => { if (err) { this.importStatus.baseTemplateFile = false; return; } this.importStatus.baseTemplateFile = true; }); let toPath2 = this.getResourceTemplatePath(isoCode, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default); let fromPath2 = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default); copydir(fromPath2, toPath2, (stat, filepath, filename) => { if (stat === 'file') { return true; } return false; }, (err) => { if (err) { this.importStatus.baseTemplateFile = false; return; } this.importStatus.defaultTemplateFile = true; }); } checkForImport(callbak) { let path = this.getTempDirectory(); try { let wait = 0; let id = setInterval(() => { if ((this.importStatus.resourceFile !== undefined && this.importStatus.editorSectionFile !== undefined && this.importStatus.templateSectionFile !== undefined && this.importStatus.baseTemplateFile !== undefined && this.importStatus.defaultTemplateFile !== undefined) || wait === 1000) { this.updateConfigFile(this.importedResource, 1); timers_1.clearInterval(id); callbak(true); } wait++; }, 120); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Export Resource> zipTempFolder > ' + e, 3); callbak(false); } } }; I18nImport = __decorate([ trace_decorator_1.Trace() ], I18nImport); exports.I18nImport = I18nImport;