UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

250 lines (249 loc) 11.7 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 project_header_1 = require("../project-data/project.header"); const template_1 = require("../master-data/template"); const i18n_core_1 = require("../i18n/i18n.core"); 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 appstatus_decorator_1 = require("../decorators/appstatus.decorator"); const project_configuration_1 = require("../configuration/project.configuration"); const path = require("path"); const fs = require("fs"); const util_1 = require("util"); const copydir = require("copy-dir"); const zipdir = require("zip-dir"); const mkdirp = require("mkdirp"); const trace_decorator_1 = require("../decorators/trace.decorator"); const readFileAsync = util_1.promisify(fs.readFile); const writeFileAsync = util_1.promisify(fs.writeFile); const existsAsync = util_1.promisify(fs.exists); const mkdir = util_1.promisify(fs.mkdir); const copydirAsync = util_1.promisify(copydir); const zipdirAsync = util_1.promisify(zipdir); const mkdirpAsync = util_1.promisify(mkdirp); let StudyExport = class StudyExport { constructor() { this.projectHeader = new project_header_1.ProjectHeader(); this.template = new template_1.Template(); this.i18nCore = new i18n_core_1.I18nCore(); this.zipProcess = new ZipProcess(); this.resourcePath = new resource_path_1.ResourcePath(); } exportStudy(projectId, savePath, callBack) { try { this.outPath = savePath; this.updateProgress = callBack; appstatus_decorator_1.StatusTracker.update({ stepIndex: 1 }); this.projectHeader.openStudyById(projectId, (response) => { if (response.result === "ok") { appstatus_decorator_1.StatusTracker.update({ stepIndex: 2 }); this.updateCallBack(0, { stepIndex: 2 }); this.createProjectTempDirectory(response).then((project) => { this.zipProcess.tempFolder = true; Promise.all([ this.createProjectInfoFile(project), this.copyProjectTemplates(project), this.copyI18nResources(project), this.copyProjectDirectory(project) ]).then(() => { this.initZipProcess(project); }); }); } }); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Export Study> exportStudy > ' + e, 3); return true; } } getProjectTempDirectory(project) { return path.join(this.resourcePath.getTempDirectory(), app_repo_constants_1.App_Repository_Constants.tempFolder.exportPackage, project.header.name); } updateCallBack(status, error) { if (this.updateProgress) { this.updateProgress(status, error); } } createProjectTempDirectory(project) { let tmpPath = this.getProjectTempDirectory(project); return existsAsync(tmpPath).then((exists) => { if (exists) { this.removeTempDirectory(tmpPath); } }).then(() => { return Promise.all([ mkdirpAsync(tmpPath), mkdirpAsync(path.join(tmpPath, app_repo_constants_1.App_Repository_Constants.tempFolder.exportPackageTemplate)), mkdirpAsync(path.join(tmpPath, app_repo_constants_1.App_Repository_Constants.tempFolder.exportPackageI18n)) ]); }).then(() => { return project; }); } createProjectInfoFile(project) { let tempPath = this.getProjectTempDirectory(project); const tempInfoPath = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.tempFolder.exportPackageInfoFile); const tempMetadatapath = path.join(tempPath, app_repo_constants_1.App_Repository_Constants.tempFolder.exportPackageMetadataFile); let allLanguages = this.i18nCore.getAllI8nResources(false); project.header.languageName = allLanguages[project.header.language]; return writeFileAsync(tempInfoPath, JSON.stringify(project.header)).then(() => { return writeFileAsync(tempMetadatapath, JSON.stringify(project.metadata)); }).then((data) => { this.zipProcess.jsonMetadata = true; }); } copyProjectTemplates(project) { try { let tempPath = this.getProjectTempDirectory(project); let language = project.header.language || app_repo_constants_1.App_Repository_Constants.defaultLanguage; const response = this.template.getTemplatePathAsync(project.header.type, language, [project.header.template, project.header.dblngcoretemplate], true); if (response.result === "ok") { var filePaths = response.paths; let fns = []; for (let i = 0; i < filePaths.length; i++) { existsAsync(filePaths[i]).then((exists) => { if (exists) { fns.push(copydirAsync(filePaths[i], path.join(tempPath, app_repo_constants_1.App_Repository_Constants.tempFolder.exportPackageTemplate), (stat, filepath, filename) => { if (stat === 'file' && (path.parse(filename).name == project.header.template || path.parse(filename).name == project.header.dblngcoretemplate)) { return true; } return false; })); } }); } return Promise.all(fns).then(() => { this.zipProcess.copyTemplate = true; }); } else { return response.message; } } catch (e) { this.updateCallBack(2, e); app_repo_utility_1.App_Repository_Utility.LogText('Export Study> copyProjectTemplates > ' + e, 3); return true; } } copyI18nResources(project) { try { let tempPath = this.getProjectTempDirectory(project); let rPath = this.i18nCore.geti18nResourcePath(project.header.language); return copydirAsync(rPath, path.join(tempPath, app_repo_constants_1.App_Repository_Constants.tempFolder.exportPackageI18n), (stat, filepath, filename) => { return true; }).then(() => { this.zipProcess.copyI18nResource = true; }); } catch (e) { this.updateCallBack(2, e); app_repo_utility_1.App_Repository_Utility.LogText('Export Study> copyI18nResources > ' + e, 3); return true; } } copyProjectDirectory(project) { try { var to = this.getProjectTempDirectory(project); let from = 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, project.header._id); appstatus_decorator_1.StatusTracker.update({ stepIndex: 3 }); this.updateCallBack(0, { stepIndex: 3 }); return existsAsync(from).then((exists) => { if (exists) { return copydirAsync(from, to, (stat, filepath, filename) => { if (stat === 'file' && path.extname(filepath) === '.db') { return false; } return true; }).then(() => { this.zipProcess.projectFolder = true; }); } }); } catch (e) { this.updateCallBack(2, e); app_repo_utility_1.App_Repository_Utility.LogText('Export Study> copyProjectDirectory > ' + e, 3); return true; } } zipTempFolder(project) { let tempPath = this.getProjectTempDirectory(project); try { this.outPath = (this.outPath.indexOf('.zip') === -1 ? (this.outPath + '.zip') : this.outPath); return zipdirAsync(tempPath, { saveTo: this.outPath }).then((err, buffer) => { appstatus_decorator_1.StatusTracker.update({ stepIndex: 5 }); this.updateCallBack(0, { stepIndex: 5 }); this.removeTempDirectory(tempPath); }); } catch (e) { this.removeTempDirectory(tempPath); this.updateCallBack(2, e); app_repo_utility_1.App_Repository_Utility.LogText('Export Study> zipTempFolder > ' + e, 3); return true; } } removeTempDirectory(filePath) { try { let deleteFolderRecursive = function (fPath) { if (fs.existsSync(fPath)) { fs.readdirSync(fPath).forEach(function (file, index) { let curPath = path.join(fPath, file); if (fs.lstatSync(curPath).isDirectory()) { deleteFolderRecursive(curPath); } else { fs.unlinkSync(curPath); } }); fs.rmdirSync(fPath); } }; deleteFolderRecursive(filePath); } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Export Study> removeTempDirectory > ' + e, 3); return true; } } initZipProcess(project) { appstatus_decorator_1.StatusTracker.update({ stepIndex: 4 }); this.updateCallBack(0, { stepIndex: 4 }); this.zipTempFolder(project); } }; StudyExport = __decorate([ trace_decorator_1.Trace({ exclude: [ 'updateCallBack', 'getProjectTempDirectory' ], params: { createProjectTempDirectory: ['*'], createProjectInfoFile: ['*'], copyProjectTemplates: ['*'], copyI18nResources: ['*'], copyProjectDirectory: ['*'], zipTempFolder: ['*'], initZipProcess: ['*'] } }) ], StudyExport); exports.StudyExport = StudyExport; class ZipProcess { constructor() { this.tempFolder = false; this.jsonMetadata = false; this.copyTemplate = false; this.copyI18nResource = true; this.projectFolder = false; } }