UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

220 lines (219 loc) 9.78 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 i18n_core_1 = require("./i18n.core"); const path = require("path"); const fs = require("fs"); const copydir = require("copy-dir"); const zipdir = require("zip-dir"); const mkdirp = require("mkdirp"); 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 trace_decorator_1 = require("../decorators/trace.decorator"); let I18nExport = class I18nExport extends i18n_core_1.I18nCore { constructor() { super(); } exportResource(langMetadata, filePath) { return new Promise((resolve, reject) => { try { this.exportMetadata = new model_1.I18nExportMetadata({}); this.exportStatus = new model_1.I18nExportImportStatus({}); this.exportMetadata.isoCode = langMetadata.isoCode; this.exportMetadata.name = langMetadata.name; this.createResourceTempDirectory(langMetadata.isoCode); this.exportResourceFile(langMetadata.isoCode); this.exportSectionMappingFiles(langMetadata.isoCode); this.exportTemplateFiles(langMetadata.isoCode); this.checkForExport(filePath, langMetadata.isoCode, (result) => { if (result === false) { reject(); } else { this.zipExportFolder(filePath, langMetadata.isoCode).then(() => { resolve(); }).catch(() => { reject(); }); } }); } catch (e) { reject(e); } }); } getTempDirectory(isoCode) { return path.join(this.resourcePath.getTempDirectory(), app_repo_constants_1.App_Repository_Constants.tempFolder.exportI18n, isoCode); } createResourceTempDirectory(isoCode) { try { let tmpPath = this.getTempDirectory(isoCode); if (fs.existsSync(tmpPath)) { this.removeDirectory(tmpPath); } mkdirp.sync(tmpPath); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Export Resource> createResourceTempDirectory > ' + e, 3); return true; } } createDirerctory(dirPath) { if (!fs.existsSync(dirPath)) { mkdirp.sync(dirPath); } } exportResourceFile(isoCode) { let fromPath = this.geti18nResourcePath(isoCode, false); let toPath = this.getTempDirectory(isoCode); copydir(fromPath, toPath, (stat, filepath, filename) => { if (stat === 'file') { return true; } return false; }, (err) => { if (err) { this.exportStatus.resourceFile = false; return; } this.exportStatus.resourceFile = true; }); } exportSectionMappingFiles(isoCode) { let tempPath = this.getTempDirectory(isoCode); let toPath1 = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.section_mapping, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor); let fromPath1 = this.getSectionMappingPath(isoCode, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.editor, false); this.createDirectorySync(toPath1); copydir.sync(fromPath1, toPath1, (stat, filepath, filename) => { if (stat === 'file') { this.exportMetadata.editorSection = toPath1; return true; } return false; }, (err) => { if (err) { this.exportStatus.editorSectionFile = false; return; } }); this.exportStatus.editorSectionFile = true; let toPath2 = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.section_mapping, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template); let fromPath2 = this.getSectionMappingPath(isoCode, app_repo_constants_1.App_Repository_Constants.appSectionMappingTypes.template, false); this.createDirectorySync(toPath2); copydir.sync(fromPath2, toPath2, (stat, filepath, filename) => { if (stat === 'file') { this.exportMetadata.templateSection = toPath2; return true; } return false; }, (err) => { if (err) { this.exportStatus.editorSectionFile = false; return; } }); this.exportStatus.templateSectionFile = true; } exportTemplateFiles(isoCode) { let tempPath = this.getTempDirectory(isoCode); let fromPath1 = this.getResourceTemplatePath(isoCode, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base); let toPath1 = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.base); this.createDirectorySync(toPath1); copydir.sync(fromPath1, toPath1, (stat, filepath, filename) => { if (stat === 'file') { this.exportMetadata.baseTemplate = toPath1; return true; } return false; }, (err) => { if (err) { this.exportStatus.baseTemplateFile = false; return; } }); this.exportStatus.baseTemplateFile = true; let fromPath2 = this.getResourceTemplatePath(isoCode, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default); let toPath2 = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.masterDataPaths.template_path, app_repo_constants_1.App_Repository_Constants.appTemplateTypes.default); this.createDirectorySync(toPath2); copydir.sync(fromPath2, toPath2, (stat, filepath, filename) => { if (stat === 'file') { this.exportMetadata.defaultTemplate = toPath2; return true; } return false; }, (err) => { if (err) { this.exportStatus.defaultTemplateFile = false; return; } }); this.exportStatus.defaultTemplateFile = true; } checkForExport(filePath, isoCode, callbak) { let path = this.getTempDirectory(isoCode); try { let wait = 0; setTimeout(() => { if ((this.exportStatus.resourceFile !== undefined && this.exportStatus.editorSectionFile !== undefined && this.exportStatus.templateSectionFile !== undefined && this.exportStatus.baseTemplateFile !== undefined && this.exportStatus.defaultTemplateFile !== undefined) || wait === 1000) { this.writeResourceInfoFile(filePath, isoCode).then(() => { callbak(true); }).catch(() => { callbak(false); }); } wait++; }, 120); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Export Resource> zipTempFolder > ' + e, 3); callbak(false); } } writeResourceInfoFile(filePath, isoCode) { try { let toPath = this.getTempDirectory(isoCode); toPath = path.join(toPath, this.resInfoFile); return this.writeFile(toPath, this.exportMetadata); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Export Resource> createResourceInfoFile > ' + e, 3); return new Promise((resolve, reject) => { reject(); }); } } zipExportFolder(filePath, isoCode) { let path = this.getTempDirectory(isoCode); return new Promise((resolve, reject) => { try { filePath = (filePath.indexOf('.zip') === -1 ? (filePath + '.zip') : filePath); zipdir(path, { saveTo: filePath }, (err, buffer) => { if (err) { reject(err); return; } resolve(); }); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Export Resource> zipTempFolder > ' + e, 3); reject(e); } }); } }; I18nExport = __decorate([ trace_decorator_1.Trace() ], I18nExport); exports.I18nExport = I18nExport;