Skip to content
Myr

Separate command-related logic into its own file (#329)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by Sindre Sorhus · parent 5c74a9d · Browse files · 4 files+103 added−91 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+103 added−91 removed

  • tests:+64 added−59 removed
  • other:+39 added−32 removed

4 files+103 added−91 removed

added:lib/command.js+38 added
added:test/command.js+64 added
modified:test/test.js-59 removed
153 unmodified lines
154 await t.throwsAsync(execa('non-executable', {input: 'Hey!'}), /EACCES/);155 });156}157158const command = async (t, expected, ...args) => {159 const {command: failCommand} = await t.throwsAsync(execa('fail', args));160 t.is(failCommand, `fail${expected}`);161162 const {command} = await execa('noop', args);163 t.is(command, `noop${expected}`);164};165166command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;167168test(command, ' foo bar', 'foo', 'bar');169test(command, ' baz quz', 'baz', 'quz');170test(command, '');171158if (process.platform !== 'win32') {159 test('write to fast-exit process', async t => {160 // Try-catch here is necessary, because this test is not 100% accurate161 // Sometimes process can manage to accept input before exiting
47 unmodified lines
209 t.true(isRunning(pid));210211 process.kill(pid, 'SIGKILL');212});227228test('allow commands with spaces and no array arguments', async t => {229 const {stdout} = await execa('command with space');230 t.is(stdout, '');231});232233test('allow commands with spaces and array arguments', async t => {234 const {stdout} = await execa('command with space', ['foo', 'bar']);235 t.is(stdout, 'foo\nbar');236});237238test('execa.command()', async t => {239 const {stdout} = await execa.command('node test/fixtures/echo foo bar');240 t.is(stdout, 'foo\nbar');241});242243test('execa.command() ignores consecutive spaces', async t => {244 const {stdout} = await execa.command('node test/fixtures/echo foo bar');245 t.is(stdout, 'foo\nbar');246});247248test('execa.command() allows escaping spaces in commands', async t => {249 const {stdout} = await execa.command('command\\ with\\ space foo bar');250 t.is(stdout, 'foo\nbar');251});252253test('execa.command() allows escaping spaces in arguments', async t => {254 const {stdout} = await execa.command('node test/fixtures/echo foo\\ bar');255 t.is(stdout, 'foo bar');256});257258test('execa.command() escapes other whitespaces', async t => {259 const {stdout} = await execa.command('node test/fixtures/echo foo\tbar');260 t.is(stdout, 'foo\tbar');261});262263test('execa.command() trims', async t => {264 const {stdout} = await execa.command(' node test/fixtures/echo foo bar ');265 t.is(stdout, 'foo\nbar');266});267268test('execa.command.sync()', t => {269 const {stdout} = execa.commandSync('node test/fixtures/echo foo bar');270 t.is(stdout, 'foo\nbar');271});
modified:index.js+1 added-32 removed
8 unmodified lines
9const normalizeStdio = require('./lib/stdio');10const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler, cleanup} = require('./lib/kill');11const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream.js');12const {mergePromise, getSpawnedPromise} = require('./lib/promise.js');13const {joinCommand, parseCommand} = require('./lib/command.js');1415const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;1616const SPACES_REGEXP = / +/g;1717const handleArgs = (file, args, options = {}) => {18 const parsed = crossSpawn._parse(file, args, options);19 file = parsed.command;20 args = parsed.args;
48 unmodified lines
6970 return value;71};7274const joinCommand = (file, args = []) => {75 if (!Array.isArray(args)) {76 return file;77 }7879 return [file, ...args].join(' ');80};8173const execa = (file, args, options) => {74 const parsed = handleArgs(file, args, options);75 const command = joinCommand(file, args);76
138 unmodified lines
215 killed: false216 };217};218228// Allow spaces to be escaped by a backslash if not meant as a delimiter229const handleEscaping = (tokens, token, index) => {230 if (index === 0) {231 return [token];232 }233234 const previousToken = tokens[tokens.length - 1];235236 if (previousToken.endsWith('\\')) {237 return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`];238 }239240 return [...tokens, token];241};242243const parseCommand = command => {244 return command245 .trim()246 .split(SPACES_REGEXP)247 .reduce(handleEscaping, []);248};249219module.exports.command = (command, options) => {220 const [file, ...args] = parseCommand(command);221 return execa(file, args, options);222};
33 unmodified lines