UNPKG

@zowe/zos-files-for-zowe-sdk

Version:

Zowe SDK to interact with files and data sets on z/OS

150 lines 6.12 kB
"use strict"; /* * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * * Copyright Contributors to the Zowe Project. * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ZosFilesAttributes = exports.TransferMode = void 0; const fs = require("fs"); const minimatch_1 = require("minimatch"); const imperative_1 = require("@zowe/imperative"); const ZosFiles_messages_1 = require("../constants/ZosFiles.messages"); const pathUtils = require("path"); const utilities_1 = require("../methods/utilities"); var TransferMode; (function (TransferMode) { TransferMode[TransferMode["BINARY"] = 0] = "BINARY"; TransferMode[TransferMode["TEXT"] = 1] = "TEXT"; })(TransferMode || (exports.TransferMode = TransferMode = {})); /** * Attributes for a set of files relating to how they will be uploaded to USS */ class ZosFilesAttributes { constructor(attributesFileContents, basePath) { this.attributes = new Map(); this.parse(attributesFileContents); this.basePath = basePath; } static loadFromFile(filePath, defaultDir) { let attributesFile; if (filePath != null) { if (!fs.existsSync(filePath)) { throw new imperative_1.ImperativeError({ msg: imperative_1.TextUtils.formatMessage(ZosFiles_messages_1.ZosFilesMessages.attributesFileNotFound.message, { file: filePath }) }); } attributesFile = filePath; } else { const localAttributesFile = pathUtils.join(defaultDir !== null && defaultDir !== void 0 ? defaultDir : process.cwd(), ".zosattributes"); if (fs.existsSync(localAttributesFile)) { attributesFile = localAttributesFile; } else { return; } } let attributesFileContents; try { attributesFileContents = fs.readFileSync(attributesFile).toString(); } catch (err) { throw new imperative_1.ImperativeError({ msg: imperative_1.TextUtils.formatMessage(ZosFiles_messages_1.ZosFilesMessages.errorReadingAttributesFile.message, { file: attributesFile, message: err.message }) }); } let attributes; try { attributes = new ZosFilesAttributes(attributesFileContents, defaultDir); } catch (err) { throw new imperative_1.ImperativeError({ msg: imperative_1.TextUtils.formatMessage(ZosFiles_messages_1.ZosFilesMessages.errorParsingAttributesFile.message, { file: attributesFile, message: err.message }) }); } return attributes; } fileShouldBeUploaded(path) { let result = false; const attributes = this.findLastMatchingAttributes(path); if (attributes === null) { result = true; } else { result = !attributes.ignore; } imperative_1.Logger.getAppLogger().debug("path: " + path + " : " + attributes + " result = " + result); return result; } fileShouldBeIgnored(path) { return !this.fileShouldBeUploaded(path); } getFileTransferMode(path, preferBinary) { const attributes = this.findLastMatchingAttributes(path); if (attributes === null) { return preferBinary ? TransferMode.BINARY : TransferMode.TEXT; } if (attributes.localEncoding === utilities_1.Tag.BINARY || attributes.localEncoding === attributes.remoteEncoding) { return TransferMode.BINARY; } else { return TransferMode.TEXT; } } getRemoteEncoding(path) { var _a; const attributes = this.findLastMatchingAttributes(path); if (attributes === null || ((_a = attributes.remoteEncoding) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === "EBCDIC") { return; // Fall back to default system code page } return attributes.remoteEncoding; } getLocalEncoding(path) { const attributes = this.findLastMatchingAttributes(path); if (attributes === null) { return "ISO8859-1"; } return attributes.localEncoding; } parse(attributesFileContents) { const lines = attributesFileContents.split("\n"); let lineNumber = 0; lines.forEach((line) => { lineNumber++; line = line.trim(); if (line === "" || line.startsWith("#")) { return; } const parts = line.trim().split(/\s+/); const pattern = parts[0]; const localEncoding = parts[1]; const remoteEncoding = parts[2]; if (parts.length > ZosFilesAttributes.MAX_EXPECTED_FIELDS || parts.length < ZosFilesAttributes.MIN_EXPECTED_FIELDS) { throw new Error(imperative_1.TextUtils.formatMessage(ZosFiles_messages_1.ZosFilesMessages.invalidAttributesSyntax.message, { lineNumber })); } if (localEncoding === "-") { this.attributes.set(pattern, { ignore: true }); } else { this.attributes.set(pattern, { ignore: false, localEncoding, remoteEncoding }); } }); } findLastMatchingAttributes(path) { let relativePath = path; if (this.basePath) { relativePath = pathUtils.relative(this.basePath, path); } let result = null; this.attributes.forEach((attributes, pattern) => { if ((0, minimatch_1.minimatch)(relativePath, pattern, { matchBase: true, dot: true })) { result = attributes; } }); return result; } } exports.ZosFilesAttributes = ZosFilesAttributes; ZosFilesAttributes.MAX_EXPECTED_FIELDS = 3; // eslint-disable-line @typescript-eslint/no-magic-numbers ZosFilesAttributes.MIN_EXPECTED_FIELDS = 2; //# sourceMappingURL=ZosFilesAttributes.js.map