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
Provenance
- Commit
46daa7b- Tree
9bad3da- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
5c74a9d
4 files changed+103 added−91 removed
added:lib/command.js+38 added
1234567891011121314151617181920212223242526272829303132333435363738'use strict';const SPACES_REGEXP = / +/g;
const joinCommand = (file, args = []) => { if (!Array.isArray(args)) { return file; }
return [file, ...args].join(' ');};
// Allow spaces to be escaped by a backslash if not meant as a delimiterconst handleEscaping = (tokens, token, index) => { if (index === 0) { return [token]; }
const previousToken = tokens[tokens.length - 1];
if (previousToken.endsWith('\\')) { return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`]; }
return [...tokens, token];};
// Handle `execa.command()`const parseCommand = command => { return command .trim() .split(SPACES_REGEXP) .reduce(handleEscaping, []);};
module.exports = { joinCommand, parseCommand};added:test/command.js+64 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import path from 'path';import test from 'ava';import execa from '..';
process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
const command = async (t, expected, ...args) => { const {command: failCommand} = await t.throwsAsync(execa('fail', args)); t.is(failCommand, `fail${expected}`);
const {command} = await execa('noop', args); t.is(command, `noop${expected}`);};
command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;
test(command, ' foo bar', 'foo', 'bar');test(command, ' baz quz', 'baz', 'quz');test(command, '');
test('allow commands with spaces and no array arguments', async t => { const {stdout} = await execa('command with space'); t.is(stdout, '');});
test('allow commands with spaces and array arguments', async t => { const {stdout} = await execa('command with space', ['foo', 'bar']); t.is(stdout, 'foo\nbar');});
test('execa.command()', async t => { const {stdout} = await execa.command('node test/fixtures/echo foo bar'); t.is(stdout, 'foo\nbar');});
test('execa.command() ignores consecutive spaces', async t => { const {stdout} = await execa.command('node test/fixtures/echo foo bar'); t.is(stdout, 'foo\nbar');});
test('execa.command() allows escaping spaces in commands', async t => { const {stdout} = await execa.command('command\\ with\\ space foo bar'); t.is(stdout, 'foo\nbar');});
test('execa.command() allows escaping spaces in arguments', async t => { const {stdout} = await execa.command('node test/fixtures/echo foo\\ bar'); t.is(stdout, 'foo bar');});
test('execa.command() escapes other whitespaces', async t => { const {stdout} = await execa.command('node test/fixtures/echo foo\tbar'); t.is(stdout, 'foo\tbar');});
test('execa.command() trims', async t => { const {stdout} = await execa.command(' node test/fixtures/echo foo bar '); t.is(stdout, 'foo\nbar');});
test('execa.command.sync()', t => { const {stdout} = execa.commandSync('node test/fixtures/echo foo bar'); t.is(stdout, 'foo\nbar');});modified:test/test.js-59 removed
153 unmodified lines154 await t.throwsAsync(execa('non-executable', {input: 'Hey!'}), /EACCES/);155 });156 }157 158-const command = async (t, expected, ...args) => {159- const {command: failCommand} = await t.throwsAsync(execa('fail', args));160- t.is(failCommand, `fail${expected}`);161-162- const {command} = await execa('noop', args);163- t.is(command, `noop${expected}`);164-};165-166-command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;167-168-test(command, ' foo bar', 'foo', 'bar');169-test(command, ' baz quz', 'baz', 'quz');170-test(command, '');171-158 if (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 exiting47 unmodified lines209 t.true(isRunning(pid));210 211 process.kill(pid, 'SIGKILL');212 });227-228-test('allow commands with spaces and no array arguments', async t => {229- const {stdout} = await execa('command with space');230- t.is(stdout, '');231-});232-233-test('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-});237-238-test('execa.command()', async t => {239- const {stdout} = await execa.command('node test/fixtures/echo foo bar');240- t.is(stdout, 'foo\nbar');241-});242-243-test('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-});247-248-test('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-});252-253-test('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-});257-258-test('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-});262-263-test('execa.command() trims', async t => {264- const {stdout} = await execa.command(' node test/fixtures/echo foo bar ');265- t.is(stdout, 'foo\nbar');266-});267-268-test('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 lines9 const normalizeStdio = require('./lib/stdio');10 const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler, cleanup} = require('./lib/kill');11 const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream.js');12 const {mergePromise, getSpawnedPromise} = require('./lib/promise.js');13+const {joinCommand, parseCommand} = require('./lib/command.js');14 15 const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;16 16-const SPACES_REGEXP = / +/g;17-17 const handleArgs = (file, args, options = {}) => {18 const parsed = crossSpawn._parse(file, args, options);19 file = parsed.command;20 args = parsed.args;48 unmodified lines69 70 return value;71 };72 74-const joinCommand = (file, args = []) => {75- if (!Array.isArray(args)) {76- return file;77- }78-79- return [file, ...args].join(' ');80-};81-73 const execa = (file, args, options) => {74 const parsed = handleArgs(file, args, options);75 const command = joinCommand(file, args);76 138 unmodified lines215 killed: false216 };217 };218 228-// Allow spaces to be escaped by a backslash if not meant as a delimiter229-const handleEscaping = (tokens, token, index) => {230- if (index === 0) {231- return [token];232- }233-234- const previousToken = tokens[tokens.length - 1];235-236- if (previousToken.endsWith('\\')) {237- return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`];238- }239-240- return [...tokens, token];241-};242-243-const parseCommand = command => {244- return command245- .trim()246- .split(SPACES_REGEXP)247- .reduce(handleEscaping, []);248-};249-219 module.exports.command = (command, options) => {220 const [file, ...args] = parseCommand(command);221 return execa(file, args, options);222 };33 unmodified lines