UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

96 lines (91 loc) 3.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fs = require("fs"); var _snooplogg = _interopRequireDefault(require("snooplogg")); var _tmp = _interopRequireDefault(require("tmp")); var _resultsRenderers = require("../common/resultsRenderers"); var _utils = require("../common/utils"); var _bashCommands = require("./bashCommands"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } const { log } = (0, _snooplogg.default)('central:class.TmpFile'); class TmpFile { /** * Init temporary file if "data" is provided - write data to file (as YAML at the moment) * @param {object} data optional data to write while creating file */ constructor(data) { _defineProperty(this, "file", void 0); _defineProperty(this, "path", void 0); log('crating a new file'); // discardDescriptor = true is required for windows (fixes "file is open by another process" error). this.file = _tmp.default.fileSync({ discardDescriptor: true, prefix: 'axway-central-edit-', postfix: '.yaml' }); this.path = this.file.name; log(`file created at: ${this.path}`); // if data is provided write it to the file as YAML data && this.write((0, _resultsRenderers.parseAsYaml)(data)); } /** * Delete tmp file */ delete() { log(`removing: ${this.path}`); this.file.removeCallback(); } /** * Write to file * @param {string} data data to write */ write(data) { log(`writing to: ${this.path}`); (0, _fs.writeFileSync)(this.path, data); } /** * Read file as string (as is) * @return {string} data from file */ read() { log(`reading from: ${this.path}`); return (0, _fs.readFileSync)(this.path, 'utf8'); } /** * Open file in editor and return promise with flags indicating if edit was successful or not * (process killed, vim q! happened etc. ), and if file content has been changed or not. * Using vim or "EDITOR" env on linux and only notepad on windows. * @returns {object} represent result of editing: * isComplete: editor process completed successfully * isUpdated: content of the file changed */ async edit() { log(`editing: ${this.path}`); const editorToUse = _utils.isWindows ? 'notepad' : process.env.EDITOR || 'vi'; const contentBeforeEdit = Buffer.from(this.read()); const editorExitCode = await (0, _bashCommands.editor)(editorToUse, this.path); const isUpdated = !contentBeforeEdit.equals(Buffer.from(this.read())); if (editorExitCode === 0) { log('file edit has been successful'); return { isComplete: true, isUpdated }; } else { log(`file edit error, code: ${editorExitCode}`); return { isComplete: false, isUpdated }; } } } exports.default = TmpFile;