Add `.escapedCommand` property (#466)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 6bc7a1c · Browse files · 7 files·+86 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Diff stats
7 files+86 added−8 removed
- tests:+23 added−0 removed
- other:+63 added−8 removed
Provenance
- Commit
712bafc- Tree
62b7031- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
6bc7a1c
7 files changed+86 added−8 removed
modified:lib/command.js+25 added-5 removed
123423645697891011121314151617181920212223242526272829303132333411 unmodified lines46474849505152'use strict';const SPACES_REGEXP = / +/g;
const joinCommand = (file, args = []) => {const normalizeArgs = (file, args = []) => { if (!Array.isArray(args)) { return file; return [file]; }
return [file, ...args].join(' '); return [file, ...args];};
const NO_ESCAPE_REGEXP = /^[\w.-]+$/;const DOUBLE_QUOTES_REGEXP = /"/g;
const escapeArg = arg => { if (NO_ESCAPE_REGEXP.test(arg)) { return arg; }
return `"${arg.replace(DOUBLE_QUOTES_REGEXP, '\\"')}"`;};
const joinCommand = (file, args) => { return normalizeArgs(file, args).join(' ');};
const getEscapedCommand = (file, args) => { return normalizeArgs(file, args).map(arg => escapeArg(arg)).join(' ');};
const SPACES_REGEXP = / +/g;
// Handle `execa.command()`const parseCommand = command => { const tokens = []; for (const token of command.trim().split(SPACES_REGEXP)) {11 unmodified lines};
module.exports = { joinCommand, getEscapedCommand, parseCommand};modified:lib/error.js+2 added
31 unmodified lines32333435363738394020 unmodified lines61626364656667686919 unmodified lines31 unmodified lines error, signal, exitCode, command, escapedCommand, timedOut, isCanceled, killed, parsed: {options: {timeout}}20 unmodified lines }
error.shortMessage = shortMessage; error.command = command; error.escapedCommand = escapedCommand; error.exitCode = exitCode; error.signal = signal; error.signalDescription = signalDescription; error.stdout = stdout;19 unmodified linesmodified:test/command.js+23 added
16 unmodified lines17 test(command, ' foo bar', 'foo', 'bar');18 test(command, ' baz quz', 'baz', 'quz');19 test(command, '');20 21+const testEscapedCommand = async (t, expected, args) => {22+ const {escapedCommand: failEscapedCommand} = await t.throwsAsync(execa('fail', args));23+ t.is(failEscapedCommand, `fail ${expected}`);24+25+ const {escapedCommand: failEscapedCommandSync} = t.throws(() => {26+ execa.sync('fail', args);27+ });28+ t.is(failEscapedCommandSync, `fail ${expected}`);29+30+ const {escapedCommand} = await execa('noop', args);31+ t.is(escapedCommand, `noop ${expected}`);32+33+ const {escapedCommand: escapedCommandSync} = execa.sync('noop', args);34+ t.is(escapedCommandSync, `noop ${expected}`);35+};36+37+testEscapedCommand.title = (message, expected) => `escapedCommand is: ${JSON.stringify(expected)}`;38+39+test(testEscapedCommand, 'foo bar', ['foo', 'bar']);40+test(testEscapedCommand, '"foo bar"', ['foo bar']);41+test(testEscapedCommand, '"\\"foo\\""', ['"foo"']);42+test(testEscapedCommand, '"*"', ['*']);43+44 test('allow commands with spaces and no array arguments', async t => {45 const {stdout} = await execa('command with space');46 t.is(stdout, '');47 });40 unmodified linesmodified:index.d.ts+11 added-1 removed
250 unmodified lines251 }252 253 interface ExecaReturnBase<StdoutStderrType> {254 /**255- The file and arguments that were run.255+ The file and arguments that were run, for logging purposes.256+257+ This is not escaped and should not be executed directly as a process, including using `execa()` or `execa.command()`.258 */259 command: string;260 261 /**262+ Same as `command` but escaped.263+264+ This is meant to be copy and pasted into a shell, for debugging purposes.265+ Since the escaping is fairly basic, this should not be executed directly as a process, including using `execa()` or `execa.command()`.266+ */267+ escapedCommand: string;268+269+ /**270 The numeric exit code of the process that was run.271 */272 exitCode: number;273 291 unmodified linesmodified:index.js+9 added-1 removed
8 unmodified lines9 const normalizeStdio = require('./lib/stdio');10 const {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} = require('./lib/kill');11 const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream');12 const {mergePromise, getSpawnedPromise} = require('./lib/promise');13-const {joinCommand, parseCommand} = require('./lib/command');13+const {joinCommand, parseCommand, getEscapedCommand} = require('./lib/command');14 15 const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;16 17 const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {55 unmodified lines73 74 const execa = (file, args, options) => {75 const parsed = handleArguments(file, args, options);76 const command = joinCommand(file, args);77+ const escapedCommand = getEscapedCommand(file, args);78 79 validateTimeout(parsed.options);80 81 let spawned;7 unmodified lines89 stdout: '',90 stderr: '',91 all: '',92 command,93+ escapedCommand,94 parsed,95 timedOut: false,96 isCanceled: false,97 killed: false24 unmodified lines122 stdout,123 stderr,124 all,125 command,126+ escapedCommand,127 parsed,128 timedOut,129 isCanceled: context.isCanceled,130 killed: spawned.killed7 unmodified lines138 }139 140 return {141 command,142+ escapedCommand,143 exitCode: 0,144 stdout,145 stderr,146 all,17 unmodified lines164 165 module.exports.sync = (file, args, options) => {166 const parsed = handleArguments(file, args, options);167 const command = joinCommand(file, args);168+ const escapedCommand = getEscapedCommand(file, args);169 170 validateInputSync(parsed.options);171 172 let result;5 unmodified lines178 stdout: '',179 stderr: '',180 all: '',181 command,182+ escapedCommand,183 parsed,184 timedOut: false,185 isCanceled: false,186 killed: false10 unmodified lines197 error: result.error,198 signal: result.signal,199 exitCode: result.status,200 command,201+ escapedCommand,202 parsed,203 timedOut: result.error && result.error.code === 'ETIMEDOUT',204 isCanceled: false,205 killed: result.signal !== null7 unmodified lines213 }214 215 return {216 command,217+ escapedCommand,218 exitCode: 0,219 stdout,220 stderr,221 failed: false,47 unmodified linesmodified:index.test-d.ts+2 added
14 unmodified lines15 expectType<ReadableStream | undefined>(execaPromise.all);16 17 const unicornsResult = await execaPromise;18 expectType<string>(unicornsResult.command);19+ expectType<string>(unicornsResult.escapedCommand);20 expectType<number>(unicornsResult.exitCode);21 expectType<string>(unicornsResult.stdout);22 expectType<string>(unicornsResult.stderr);23 expectType<string | undefined>(unicornsResult.all);23 unmodified lines47 48 try {49 const unicornsResult = execa.sync('unicorns');50 expectType<string>(unicornsResult.command);51+ expectType<string>(unicornsResult.escapedCommand);52 expectType<number>(unicornsResult.exitCode);53 expectType<string>(unicornsResult.stdout);54 expectType<string>(unicornsResult.stderr);55 expectError(unicornsResult.all);138 unmodified linesmodified:readme.md+14 added-1 removed
66 unmodified lines67 spawnargs: ['command'],68 originalMessage: 'spawn unknown ENOENT',69 shortMessage: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',70 command: 'unknown command',71+ escapedCommand: 'unknown command',72 stdout: '',73 stderr: '',74 all: '',75 failed: true,45 unmodified lines121 spawnargs: ['command'],122 originalMessage: 'spawnSync unknown ENOENT',123 shortMessage: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',124 command: 'unknown command',125+ escapedCommand: 'unknown command',126 stdout: '',127 stderr: '',128 all: '',129 failed: true,105 unmodified lines235 #### command236 237 Type: `string`238 237-The file and arguments that were run.239+The file and arguments that were run, for logging purposes.240 241+This is not escaped and should not be executed directly as a process, including using [`execa()`](#execafile-arguments-options) or [`execa.command()`](#execacommandcommand-options).242+243+#### escapedCommand244+245+Type: `string`246+247+Same as [`command`](#command) but escaped.248+249+This is meant to be copy and pasted into a shell, for debugging purposes.250+Since the escaping is fairly basic, this should not be executed directly as a process, including using [`execa()`](#execafile-arguments-options) or [`execa.command()`](#execacommandcommand-options).251+252 #### exitCode253 254 Type: `number`255 408 unmodified lines