Skip to content

Commit

Permalink
Add fork method
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Martigny committed Apr 3, 2019
1 parent 7702b8e commit ec492d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,12 @@ module.exports.sync = (command, args, options) => {
};

module.exports.shellSync = (command, options) => handleShell(execa.sync, command, options);

module.exports.fork = (filePath, args, options) =>{
return execa('node', [filePath, ...(args || [])], {
...options,
ipc: 'pipe',
execPath: path.resolve(filePath),
shell: false,
});
};
2 changes: 1 addition & 1 deletion lib/stdio.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const alias = ['stdin', 'stdout', 'stderr'];
const alias = ['stdin', 'stdout', 'stderr', 'ipc'];

const hasAlias = opts => alias.some(x => Boolean(opts[x]));

Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,9 @@ test('calling cancel method on a process which has been killed does not make err
const error = await t.throwsAsync(subprocess);
t.false(error.isCanceled);
});

test.only('fork', async t => {
const result = await execa.fork('./fixtures/simple-log.js');

t.is(result.stdout, 'ok');
});
38 changes: 20 additions & 18 deletions test/stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,26 @@ test(macro, null, undefined);
test(macro, {stdio: 'inherit'}, 'inherit');
test(macro, {stdio: 'pipe'}, 'pipe');
test(macro, {stdio: 'ignore'}, 'ignore');
test(macro, {stdio: [0, 1, 2]}, [0, 1, 2]);

test(macro, {}, [undefined, undefined, undefined]);
test(macro, {stdio: []}, [undefined, undefined, undefined]);
test(macro, {stdin: 'pipe'}, ['pipe', undefined, undefined]);
test(macro, {stdout: 'ignore'}, [undefined, 'ignore', undefined]);
test(macro, {stderr: 'inherit'}, [undefined, undefined, 'inherit']);
test(macro, {stdin: 'pipe', stdout: 'ignore', stderr: 'inherit'}, ['pipe', 'ignore', 'inherit']);
test(macro, {stdin: 'pipe', stdout: 'ignore'}, ['pipe', 'ignore', undefined]);
test(macro, {stdin: 'pipe', stderr: 'inherit'}, ['pipe', undefined, 'inherit']);
test(macro, {stdout: 'ignore', stderr: 'inherit'}, [undefined, 'ignore', 'inherit']);
test(macro, {stdin: 0, stdout: 1, stderr: 2}, [0, 1, 2]);
test(macro, {stdin: 0, stdout: 1}, [0, 1, undefined]);
test(macro, {stdin: 0, stderr: 2}, [0, undefined, 2]);
test(macro, {stdout: 1, stderr: 2}, [undefined, 1, 2]);
test(macro, {stdio: [0, 1, 2, 3]}, [0, 1, 2, 3]);

test(macro, {}, [undefined, undefined, undefined, undefined]);
test(macro, {stdio: []}, [undefined, undefined, undefined, undefined]);
test(macro, {stdin: 'pipe'}, ['pipe', undefined, undefined, undefined]);
test(macro, {stdout: 'ignore'}, [undefined, 'ignore', undefined, undefined]);
test(macro, {stderr: 'inherit'}, [undefined, undefined, 'inherit', undefined]);
test(macro, {ipc: 'pipe'}, [undefined, undefined, undefined, 'pipe']);
test(macro, {stdin: 'pipe', stdout: 'ignore', stderr: 'inherit'}, ['pipe', 'ignore', 'inherit', undefined]);
test(macro, {stdin: 'pipe', stdout: 'ignore'}, ['pipe', 'ignore', undefined, undefined]);
test(macro, {stdin: 'pipe', stderr: 'inherit'}, ['pipe', undefined, 'inherit', undefined]);
test(macro, {stdout: 'ignore', stderr: 'inherit'}, [undefined, 'ignore', 'inherit', undefined]);
test(macro, {stdin: 0, stdout: 1, stderr: 2}, [0, 1, 2, undefined]);
test(macro, {stdin: 0, stdout: 1}, [0, 1, undefined, undefined]);
test(macro, {stdin: 0, stderr: 2}, [0, undefined, 2, undefined]);
test(macro, {stdout: 1, stderr: 2}, [undefined, 1, 2, undefined]);
test(macro, {ipc: 3, stderr: 2}, [undefined, undefined, 2, 3]);

test(macro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));

test(macro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));
test(macro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));
test(macro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));
test(macro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`, `ipc`'));
test(macro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`, `ipc`'));
test(macro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`, `ipc`'));

0 comments on commit ec492d8

Please sign in to comment.