Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
import { util, net } from 'appium/support';
import { net } from 'appium/support';
import { promisify } from 'node:util';
import { exec } from 'node:child_process';
import B from 'bluebird';
Expand All @@ -21,7 +21,7 @@ export async function shellExec(cmd, args = [], opts = {}) {
const {
timeoutMs = 60 * 1000 * 5
} = opts;
const fullCmd = util.quote([cmd, ...args]);
const fullCmd = [cmd, ...args].map(escapeWindowsArg).join(' ');
const { stdout, stderr } = await B.resolve(execAsync(fullCmd, opts))
.timeout(timeoutMs, `The command '${fullCmd}' timed out after ${timeoutMs}ms`);
return {
Expand All @@ -30,6 +30,27 @@ export async function shellExec(cmd, args = [], opts = {}) {
};
}

/**
* Escapes a string to be used as a Windows command line argument
*
* @param {string} arg
* @returns {string}
*/
function escapeWindowsArg(arg) {
if (!arg) {
return '""';
}

const needsQuotes = /[\s"]/g.test(arg);
if (!needsQuotes) {
return arg;
}

// Escape double quotes and backslashes before quotes
const escaped = arg.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, '$1$1');
return `"${escaped}"`;
}

/**
*
* @param {string} srcUrl
Expand Down