@zowe/imperative
Version:
framework for building configurable CLIs
137 lines • 5.42 kB
JavaScript
/*
* 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.ProcessUtils = exports.GuiResult = void 0;
const logger_1 = require("../../logger");
const spawn = require("cross-spawn");
/**
* This enum represents the possible results from isGuiAvailable.
*/
var GuiResult;
(function (GuiResult) {
/** A GUI is available */
GuiResult[GuiResult["GUI_AVAILABLE"] = 0] = "GUI_AVAILABLE";
/** No GUI because this is an SSH connection. */
GuiResult[GuiResult["NO_GUI_SSH"] = 1] = "NO_GUI_SSH";
/** No GUI because The $DISPLAY environment variable is not set. */
GuiResult[GuiResult["NO_GUI_NO_DISPLAY"] = 2] = "NO_GUI_NO_DISPLAY";
})(GuiResult || (exports.GuiResult = GuiResult = {}));
/**
* A collection of utilities related to the running process.
* @export
* @class ProcessUtils
*/
class ProcessUtils {
// __________________________________________________________________________
/**
* Process utility to wrap callback process routines into promises
* Turn nextTick into a promise to prevent nesting
* @static
* @param {() => void} callback - called before promise is resolved
* @param {...any[]} args - arguments passed to the callback
* @returns {Promise<void>} - fulfilled whenever callback is invoked
* @memberof ProcessUtils
*/
static nextTick(callback, ...args) {
return new Promise((resolve, _reject) => {
process.nextTick(() => {
callback(...args);
resolve();
});
});
}
// __________________________________________________________________________
/**
* Is a Graphical User Interface available in the environment in which
* the current command is running?
*
* @returns {boolean} - True if GUI. False when no GUI.
*/
static isGuiAvailable() {
/* If any of the SSH environment variables are defined,
* then we are in an ssh session --> no GUI.
*/
if (typeof process.env.SSH_CONNECTION !== "undefined" ||
typeof process.env.SSH_CLIENT !== "undefined" ||
typeof process.env.SSH_TTY !== "undefined") {
return GuiResult.NO_GUI_SSH;
}
/* On linux the DISPLAY environment variable indicates
* that we are in an X-Window environment.
*/
if (process.platform !== "win32" && process.platform !== "darwin") {
if (typeof process.env.DISPLAY === "undefined" ||
process.env.DISPLAY === "") {
return GuiResult.NO_GUI_NO_DISPLAY;
}
}
// otherwise we assume we have a GUI
return GuiResult.GUI_AVAILABLE;
}
/**
* Get some basic information about the system
*/
static getBasicSystemInfo() {
const sysInfo = { arch: undefined, platform: undefined };
sysInfo.arch = process.arch;
sysInfo.platform = process.platform;
return sysInfo;
}
/**
* Open a file or URL in the default application associated with its file
* extension or URL protocol. This method is only supported in graphical
* environments.
* @param pathOrUrl File path or Internet URL to open
*/
static openInDefaultApp(pathOrUrl) {
const openerProc = require("opener")(pathOrUrl);
if (process.platform !== "win32") {
/* On linux, without the following statements, the zowe
* command does not return until the browser is closed.
* Mac is untested, but for now we treat it like linux.
*/
openerProc.unref();
openerProc.stdin.unref();
openerProc.stdout.unref();
openerProc.stderr.unref();
}
}
/**
* Open a file in the best editor that can be found in the current
* environment. In a graphical environment, the default application
* associated with its file extension will be launched. In a command-line
* environment, the file will be opened in vi.
* @param filePath - File path to edit
* @param editor - Program name of editor to override the default (e.g., notepad)
* @param sync - Boolean where true == synchronous and false == asynchronous
*/
static openInEditor(filePath, editor, sync) {
if (ProcessUtils.isGuiAvailable() === GuiResult.GUI_AVAILABLE) {
logger_1.Logger.getImperativeLogger().info(`Opening ${filePath} in graphical editor`);
if (editor != null) {
(sync ? spawn.sync : spawn.spawn)(editor, [filePath], { stdio: "inherit" });
}
else {
this.openInDefaultApp(filePath);
}
}
else {
if (editor == null) {
editor = "vi";
}
logger_1.Logger.getImperativeLogger().info(`Opening ${filePath} in command-line editor ${editor}`);
(sync ? spawn.sync : spawn.spawn)(editor, [filePath], { stdio: "inherit" });
}
}
}
exports.ProcessUtils = ProcessUtils;
//# sourceMappingURL=ProcessUtils.js.map
;