Skip to content
Myr

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

7 files+86 added−8 removed

modified:lib/command.js+25 added-5 removed
modified:lib/error.js+2 added
modified:test/command.js+23 added
16 unmodified lines
17test(command, ' foo bar', 'foo', 'bar');18test(command, ' baz quz', 'baz', 'quz');19test(command, '');2021const testEscapedCommand = async (t, expected, args) => {22 const {escapedCommand: failEscapedCommand} = await t.throwsAsync(execa('fail', args));23 t.is(failEscapedCommand, `fail ${expected}`);2425 const {escapedCommand: failEscapedCommandSync} = t.throws(() => {26 execa.sync('fail', args);27 });28 t.is(failEscapedCommandSync, `fail ${expected}`);2930 const {escapedCommand} = await execa('noop', args);31 t.is(escapedCommand, `noop ${expected}`);3233 const {escapedCommand: escapedCommandSync} = execa.sync('noop', args);34 t.is(escapedCommandSync, `noop ${expected}`);35};3637testEscapedCommand.title = (message, expected) => `escapedCommand is: ${JSON.stringify(expected)}`;3839test(testEscapedCommand, 'foo bar', ['foo', 'bar']);40test(testEscapedCommand, '"foo bar"', ['foo bar']);41test(testEscapedCommand, '"\\"foo\\""', ['"foo"']);42test(testEscapedCommand, '"*"', ['*']);4344test('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 lines
modified:index.d.ts+11 added-1 removed
250 unmodified lines
251 }252253 interface ExecaReturnBase<StdoutStderrType> {254 /**255 The file and arguments that were run.255 The file and arguments that were run, for logging purposes.256257 This is not escaped and should not be executed directly as a process, including using `execa()` or `execa.command()`.258 */259 command: string;260261 /**262 Same as `command` but escaped.263264 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;268269 /**270 The numeric exit code of the process that was run.271 */272 exitCode: number;273
291 unmodified lines
modified:index.js+9 added-1 removed
8 unmodified lines
9const normalizeStdio = require('./lib/stdio');10const {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} = require('./lib/kill');11const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream');12const {mergePromise, getSpawnedPromise} = require('./lib/promise');13const {joinCommand, parseCommand} = require('./lib/command');13const {joinCommand, parseCommand, getEscapedCommand} = require('./lib/command');1415const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;1617const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {
55 unmodified lines
7374const execa = (file, args, options) => {75 const parsed = handleArguments(file, args, options);76 const command = joinCommand(file, args);77 const escapedCommand = getEscapedCommand(file, args);7879 validateTimeout(parsed.options);8081 let spawned;
7 unmodified lines
89 stdout: '',90 stderr: '',91 all: '',92 command,93 escapedCommand,94 parsed,95 timedOut: false,96 isCanceled: false,97 killed: false
24 unmodified lines
122 stdout,123 stderr,124 all,125 command,126 escapedCommand,127 parsed,128 timedOut,129 isCanceled: context.isCanceled,130 killed: spawned.killed
7 unmodified lines
138 }139140 return {141 command,142 escapedCommand,143 exitCode: 0,144 stdout,145 stderr,146 all,
17 unmodified lines
164165module.exports.sync = (file, args, options) => {166 const parsed = handleArguments(file, args, options);167 const command = joinCommand(file, args);168 const escapedCommand = getEscapedCommand(file, args);169170 validateInputSync(parsed.options);171172 let result;
5 unmodified lines
178 stdout: '',179 stderr: '',180 all: '',181 command,182 escapedCommand,183 parsed,184 timedOut: false,185 isCanceled: false,186 killed: false
10 unmodified lines
197 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 !== null
7 unmodified lines
213 }214215 return {216 command,217 escapedCommand,218 exitCode: 0,219 stdout,220 stderr,221 failed: false,
47 unmodified lines
modified:index.test-d.ts+2 added
14 unmodified lines
15 expectType<ReadableStream | undefined>(execaPromise.all);1617 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 lines
4748try {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 lines
modified:readme.md+14 added-1 removed
66 unmodified lines
67 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 lines
121 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 lines
235#### command236237Type: `string`238237The file and arguments that were run.239The file and arguments that were run, for logging purposes.240241This is not escaped and should not be executed directly as a process, including using [`execa()`](#execafile-arguments-options) or [`execa.command()`](#execacommandcommand-options).242243#### escapedCommand244245Type: `string`246247Same as [`command`](#command) but escaped.248249This is meant to be copy and pasted into a shell, for debugging purposes.250Since 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).251252#### exitCode253254Type: `number`255
408 unmodified lines