UNPKG

@enonic/mock-xp

Version:

Mock Enonic XP API JavaScript Library

370 lines (369 loc) 15.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LibExport = void 0; var tslib_1 = require("tslib"); var isStringLiteral_1 = require("@enonic/js-utils/value/isStringLiteral"); var adm_zip_1 = tslib_1.__importDefault(require("adm-zip")); var fs_1 = require("fs"); var os_1 = require("os"); var path_1 = require("path"); var sortZipEntries_1 = require("./export/sortZipEntries"); var parseEnonicXml_1 = require("./export/parseEnonicXml"); var constants_1 = require("../constants"); var EXPORT_UUID_NIL = '000-000-000-000'; function isDirectory(path) { try { return (0, fs_1.statSync)(path).isDirectory(); } catch (err) { return false; } } function isFile(path) { try { return (0, fs_1.statSync)(path).isFile(); } catch (err) { return false; } } function listDirSync(dirPath, nodeParentPath) { return (0, fs_1.readdirSync)(dirPath, { withFileTypes: true }).map(function (entry) { var absPath = (0, path_1.join)(dirPath, entry.name); return { absPath: absPath, isDirectory: entry.isDirectory(), name: entry.name, nodeParentPath: nodeParentPath, }; }).sort(function (_a, _b) { var name = _a.name; var nameB = _b.name; return name.localeCompare(nameB); }); } var LibExport = (function () { function LibExport(_a) { var sandboxName = _a.sandboxName, server = _a.server; if (sandboxName) { this.sandboxAbsPath = (0, path_1.resolve)((0, os_1.homedir)(), '.enonic/sandboxes', sandboxName); if (!isDirectory(this.sandboxAbsPath)) { throw new Error("LibExport constructor: Sandbox not found at ".concat(this.sandboxAbsPath, "!")); } } this.server = server; } LibExport.prototype._importRootNode = function (_a) { var _debug = _a._debug, _trace = _a._trace, importNodesResult = _a.importNodesResult, includePermissions = _a.includePermissions, rootXmlString = _a.rootXmlString; var rootXmlNode = (0, parseEnonicXml_1.parseEnonicXml)({ _trace: _trace, log: this.server.log, xmlString: rootXmlString, }); if (_trace) this.server.log.debug('rootXmlNode:%s', rootXmlNode); if (rootXmlNode._id !== EXPORT_UUID_NIL) { throw new Error("MockXP importNodes: Only supports \"root\" exports!"); } rootXmlNode._path = '/'; if (_trace) { var rootNode = this.server.getNode({ branchId: this.server.context.branch, key: constants_1.UUID_NIL, repoId: this.server.context.repository, }); this.server.log.debug('rootNode:%s', rootNode); } this.server.modifyNode({ branchId: this.server.context.branch, key: constants_1.UUID_NIL, repoId: this.server.context.repository, editor: function (node) { node._childOrder = rootXmlNode._childOrder; node._indexConfig = rootXmlNode._indexConfig; if (includePermissions) { node._inheritsPermissions = rootXmlNode._inheritsPermissions; node._permissions = rootXmlNode._permissions; } node._ts = rootXmlNode._ts; return node; } }); importNodesResult.updatedNodes.push(constants_1.UUID_NIL); if (_debug || _trace) { var modifiedRootNode = this.server.getNode({ branchId: this.server.context.branch, key: constants_1.UUID_NIL, repoId: this.server.context.repository, }); if (_trace) { this.server.log.debug('modifiedRootNode:%s', modifiedRootNode); } else if (_debug) { var _path = modifiedRootNode._path; this.server.log.debug('updated node with _path:%s ', _path); } } }; LibExport.prototype._importNode = function (_a) { var _debug = _a._debug, _trace = _a._trace, importNodesResult = _a.importNodesResult, includeNodeIds = _a.includeNodeIds, includePermissions = _a.includePermissions, name = _a.name, parentPath = _a.parentPath, xmlString = _a.xmlString; var xmlNode = (0, parseEnonicXml_1.parseEnonicXml)({ _trace: _trace, log: this.server.log, xmlString: xmlString, }); if (_trace) this.server.log.debug('xmlNode:%s', xmlNode); var createNodeParams = xmlNode; createNodeParams._name = name; createNodeParams._parentPath = parentPath; if (!includeNodeIds) { delete createNodeParams._id; } if (!includePermissions) { delete createNodeParams._inheritsPermissions; delete createNodeParams._permissions; } try { var createdNode = this.server.createNode({ branchId: this.server.context.branch, repoId: this.server.context.repository, node: createNodeParams }); if (_trace) { this.server.log.debug('createdNode:%s', createdNode); } else if (_debug) { var _path = createdNode._path; this.server.log.debug('created node with _path:%s', _path); } importNodesResult.addedNodes.push(createdNode._id); return createdNode; } catch (error) { if (error instanceof Error) { importNodesResult.importErrors.push({ exception: "".concat(error), message: error.message, stacktrace: [], }); } else { importNodesResult.importErrors.push({ exception: "".concat(error), message: 'Unknown error', stacktrace: [], }); } } return undefined; }; LibExport.prototype._importFromExportFolder = function (_a) { var _debug = _a._debug, _trace = _a._trace, includeNodeIds = _a.includeNodeIds, includePermissions = _a.includePermissions, source = _a.source; var exportAbsPath = (0, path_1.resolve)(this.sandboxAbsPath, 'home/data/export', source); if (!isDirectory(exportAbsPath)) { throw new Error("importNodes: Export not found at ".concat(exportAbsPath, "!")); } var entries = listDirSync(exportAbsPath, '/'); if (_trace) this.server.log.debug('entries:%s', entries); var firstDir = entries.shift(); if (firstDir.name !== '_') { throw new Error("MockXP importNodes: Only supports \"root\" exports!"); } var rootXmlString = (0, fs_1.readFileSync)((0, path_1.join)(firstDir.absPath, 'node.xml'), 'utf-8'); var importNodesResult = { addedNodes: [], updatedNodes: [], importedBinaries: [], importErrors: [{ exception: '', message: '', stacktrace: [], }], }; this._importRootNode({ _debug: _debug, _trace: _trace, importNodesResult: importNodesResult, includePermissions: includePermissions, rootXmlString: rootXmlString, }); function handleDirectory(entries) { var e_1, _a, e_2, _b; var subDirs = []; try { for (var entries_1 = tslib_1.__values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) { var _c = entries_1_1.value, absPath = _c.absPath, isDirectory_1 = _c.isDirectory, name_1 = _c.name, nodeParentPath = _c.nodeParentPath; if (isDirectory_1) { var xmlString = (0, fs_1.readFileSync)((0, path_1.join)(absPath, '_/node.xml'), 'utf-8'); var createdNode = this._importNode({ _debug: _debug, _trace: _trace, importNodesResult: importNodesResult, includeNodeIds: includeNodeIds, includePermissions: includePermissions, name: name_1, parentPath: nodeParentPath, xmlString: xmlString, }); if (createdNode) { var subDir = listDirSync((0, path_1.join)(absPath), createdNode._path); if (_trace) this.server.log.debug('subDir:%s', subDir); subDir.shift(); subDirs.push(subDir); } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1); } finally { if (e_1) throw e_1.error; } } try { for (var subDirs_1 = tslib_1.__values(subDirs), subDirs_1_1 = subDirs_1.next(); !subDirs_1_1.done; subDirs_1_1 = subDirs_1.next()) { var subDir = subDirs_1_1.value; handleDirectory.call(this, subDir); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (subDirs_1_1 && !subDirs_1_1.done && (_b = subDirs_1.return)) _b.call(subDirs_1); } finally { if (e_2) throw e_2.error; } } } handleDirectory.call(this, entries); if (_trace) this.server.log.debug('importNodesResult:%s', importNodesResult); return importNodesResult; }; LibExport.prototype._importFromZipFile = function (_a) { var e_3, _b; var _debug = _a._debug, _trace = _a._trace, includeNodeIds = _a.includeNodeIds, includePermissions = _a.includePermissions, zipFilePath = _a.zipFilePath; var zip; var zipEntries; try { zip = new adm_zip_1.default(zipFilePath); zipEntries = (0, sortZipEntries_1.sortZipEntries)(zip.getEntries(), { prioritizeShorter: true }); if (_trace) this.server.log.debug("".concat(zipFilePath, " is a valid ZIP file.")); } catch (error) { throw new Error("".concat(zipFilePath, " is not a valid ZIP file!")); } var rootEntry = zipEntries.shift(); if (_trace) this.server.log.debug('rootEntry.entryName:%s', rootEntry.entryName); var rootXmlString = zip.readAsText(rootEntry); if (_trace) this.server.log.debug('rootXmlString:%s', rootXmlString); var importNodesResult = { addedNodes: [], updatedNodes: [], importedBinaries: [], importErrors: [{ exception: '', message: '', stacktrace: [], }], }; this._importRootNode({ _debug: _debug, _trace: _trace, importNodesResult: importNodesResult, includePermissions: includePermissions, rootXmlString: rootXmlString, }); try { for (var zipEntries_1 = tslib_1.__values(zipEntries), zipEntries_1_1 = zipEntries_1.next(); !zipEntries_1_1.done; zipEntries_1_1 = zipEntries_1.next()) { var zipEntry = zipEntries_1_1.value; var entryName = zipEntry.entryName, isDirectory_2 = zipEntry.isDirectory; var fileName = (0, path_1.basename)(entryName); var nodePath = (0, path_1.dirname)(entryName); var pathComponents = (0, path_1.normalize)(nodePath).split(path_1.sep); if (pathComponents.length < 2) { if (_trace) this.server.log.debug('pathComponents:%s', pathComponents); continue; } nodePath = "/".concat(pathComponents.length > 1 ? pathComponents.slice(1, -1).join(path_1.sep) : nodePath); if (_trace) this.server.log.debug('nodePath:%s', nodePath); var nodeParentPath = (0, path_1.dirname)(nodePath); var nodeName = (0, path_1.basename)(nodePath); if (!isDirectory_2 && fileName === 'node.xml') { var xmlString = zip.readAsText(zipEntry); if (_trace) this.server.log.debug('xmlString:%s', xmlString); this._importNode({ _debug: _debug, _trace: _trace, importNodesResult: importNodesResult, includeNodeIds: includeNodeIds, includePermissions: includePermissions, name: nodeName, parentPath: nodeParentPath, xmlString: xmlString, }); } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (zipEntries_1_1 && !zipEntries_1_1.done && (_b = zipEntries_1.return)) _b.call(zipEntries_1); } finally { if (e_3) throw e_3.error; } } return importNodesResult; }; LibExport.prototype.importNodes = function (_a) { var _b = _a._debug, _debug = _b === void 0 ? false : _b, _c = _a._trace, _trace = _c === void 0 ? false : _c, source = _a.source, targetNodePath = _a.targetNodePath, xslt = _a.xslt, xsltParams = _a.xsltParams, _d = _a.includeNodeIds, includeNodeIds = _d === void 0 ? true : _d, _e = _a.includePermissions, includePermissions = _e === void 0 ? false : _e, nodeImported = _a.nodeImported, nodeResolved = _a.nodeResolved; if (targetNodePath) { this.server.log.warning('MockXP importNodes: targetNodePath not supported yet, using "/".'); } if (xslt) { this.server.log.warning('MockXP importNodes: xslt ignored, not supported.'); } if (xsltParams) { this.server.log.warning('MockXP importNodes: xsltParams ignored, not supported.'); } if (nodeImported) { this.server.log.warning('MockXP importNodes: nodeImported ignored, not supported yet.'); } if (nodeResolved) { this.server.log.warning('MockXP importNodes: nodeResolved ignored, not supported yet.'); } if (!(0, isStringLiteral_1.isStringLiteral)(source)) { throw new Error("importNodes: source must be a string!"); } if (isFile(source)) { return this._importFromZipFile({ _debug: _debug, _trace: _trace, includeNodeIds: includeNodeIds, includePermissions: includePermissions, zipFilePath: source }); } return this._importFromExportFolder({ _debug: _debug, _trace: _trace, includeNodeIds: includeNodeIds, includePermissions: includePermissions, source: source, }); }; return LibExport; }()); exports.LibExport = LibExport;