UNPKG

@theia/process

Version:
146 lines 5.09 kB
"use strict"; // ***************************************************************************** // Copyright (C) 2020 Ericsson and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** Object.defineProperty(exports, "__esModule", { value: true }); exports.PowershellQuotingFunctions = exports.CmdQuotingFunctions = exports.BashQuotingFunctions = exports.escapeForShell = exports.createShellCommandLine = void 0; /** * Converts a list of args into an escaped shell command. * * There are two main use cases when handling command/arguments for a shell: * 1. User already wrote the escaped commandline, then just use that. * 2. User wants a specific process to be invoked with some arguments. * * The `createShellCommandLine` function is useful for the latter. * * @param args Standard list of spawn/exec arguments, first item is the command. * @param quotingFunctions Collection of functions to process arguments. */ function createShellCommandLine(args, quotingFunctions) { return args.map(arg => escapeForShell(arg, quotingFunctions)).join(' '); } exports.createShellCommandLine = createShellCommandLine; /** * Escape (or quote) a given input. * * @param arg Input to escape. * @param quotingFunctions Collection of functions to process the given `arg`. * @param quotingType Override the quoting type specified by the given `arg`. */ function escapeForShell(arg, quotingFunctions, quotingType) { let value; let quoting = quotingType; if (typeof arg === 'string') { if (!quoting) { return arg; } value = arg; } else { if (!quoting) { quoting = arg.quoting; } value = arg.value; } if (quotingFunctions && typeof quotingFunctions[quoting] === 'function') { return quotingFunctions[quoting](value); } return value; } exports.escapeForShell = escapeForShell; exports.BashQuotingFunctions = { characters: { needQuotes: '()', escape: '\\', strong: '\'', weak: '"', }, escape(arg) { return arg .replace(/[\s\\|(){}<>$&;"']/g, '\\$&'); }, strong(arg) { // ('+) becomes ('"'+"') return `'${arg .replace(/'+/g, '\'"$&"\'')}'`; }, weak(arg) { return `"${arg // Escape escape-characters. .replace(/\\"/g, '\\\\"') // Escape user-specified double-quotes. .replace(/"/g, '\\"') // Escape trailing (\), we don't want the user to escape our last quote. .replace(/\\$/g, '\\\\')}"`; }, }; exports.CmdQuotingFunctions = { characters: { weak: '"', }, escape(arg) { return arg // Escape forbidden characters (see: cmd /?). .replace(/[%&<>()@^|]/g, '^$&') // Some characters must be escaped using `\`. .replace(/[\\"]/g, '\\$&') // Double-quote whitespaces, else we cannot escape it. .replace(/\s+/g, '"$&"'); }, strong(arg) { return this.weak(arg) // Try to prevent variable expansion. .replace(/%/g, '"%"'); }, weak(arg) { return `"${arg // Escape double quotes. .replace(/\\"/g, '\\\\"') .replace(/"/g, '\\"') // Escape forbidden characters (see: cmd /?) .replace(/[&<>()@^|]/g, '^$&') // Escape trailing backslash, we don't want the user to escape our last quote. .replace(/\\$/g, '\\\\') // Escape line returns .replace(/\r?\n/g, '^$&')}"`; }, }; exports.PowershellQuotingFunctions = { characters: { needQuotes: '()', escape: '`', strong: '\'', weak: '"', }, escape(arg) { return arg.replace(/[`|{}()<>;"' ]/g, '`$&'); }, strong(arg) { // In powershell, one must write ('') for a single quote to be displayed // within a single quoted string. return `'${arg .replace(/'/g, '\'\'')}'`; }, weak(arg) { return `"${arg // Escape escape-characters. .replace(/`"/g, '``"') // Escape user-specified backticks. .replace(/"/g, '`"') // Escape trailing (`), we don't want the user to escape our last quote. .replace(/`$/g, '``')}"`; }, }; //# sourceMappingURL=shell-quoting.js.map