Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow different env vars for each command #184

Merged
merged 5 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ concurrently can be used programmatically by using the API documented below:

### `concurrently(commands[, options])`
- `commands`: an array of either strings (containing the commands to run) or objects
with the shape `{ command, name, prefixColor }`.
with the shape `{ command, name, prefixColor, env }`.
- `options` (optional): an object containing any of the below:
- `defaultInputTarget`: the default input target when reading from `inputStream`.
Default: `0`.
Expand Down Expand Up @@ -259,7 +259,8 @@ Example:
const concurrently = require('concurrently');
concurrently([
'npm:watch-*',
{ command: 'nodemon', name: 'server' }
{ command: 'nodemon', name: 'server' },
{ command: 'deploy', name: 'deploy', env: {PUBLIC_KEY: '...'} }
gustavohenke marked this conversation as resolved.
Show resolved Hide resolved
], {
prefix: 'name',
killOthers: ['failure', 'success'],
Expand Down
17 changes: 9 additions & 8 deletions src/concurrently.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ module.exports = (commands, options) => {
new ExpandNpmWildcard()
];

const spawnOpts = getSpawnOpts({ raw: options.raw });

commands = _(commands)
.map(mapToCommandInfo)
.flatMap(command => parseCommand(command, commandParsers))
.map((command, index) => new Command(Object.assign({
index,
spawnOpts,
killProcess: options.kill,
spawn: options.spawn,
}, command)))
.map((command, index) => new Command(
Object.assign({
index,
spawnOpts: getSpawnOpts({ raw: options.raw, env: command.env }),
killProcess: options.kill,
spawn: options.spawn,
}, command)
))
.value();

commands = options.controllers.reduce(
Expand All @@ -63,6 +63,7 @@ function mapToCommandInfo(command) {
command: command.command || command,
name: command.name || '',
prefixColor: command.prefixColor || '',
env: command.env || {},
};
}

Expand Down
19 changes: 19 additions & 0 deletions src/concurrently.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,22 @@ it('passes commands wrapped from a controller to the next one', () => {

expect(fakeCommand.start).toHaveBeenCalledTimes(1);
});

it('merges extra env vars into each command', () => {
create([
{ command: 'echo', env: { foo: 'bar' } },
{ command: 'echo', env: { foo: 'baz' } },
'kill'
]);

expect(spawn).toHaveBeenCalledTimes(3);
expect(spawn).toHaveBeenCalledWith('echo', expect.objectContaining({
env: expect.objectContaining({ foo: 'bar' })
}));
expect(spawn).toHaveBeenCalledWith('echo', expect.objectContaining({
env: expect.objectContaining({ foo: 'baz' })
}));
expect(spawn).toHaveBeenCalledWith('kill', expect.objectContaining({
env: expect.not.objectContaining({ foo: expect.anything() })
}));
});
5 changes: 3 additions & 2 deletions src/get-spawn-opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const supportsColor = require('supports-color');
module.exports = ({
colorSupport = supportsColor.stdout,
process = global.process,
raw = false
raw = false,
env = {}
}) => Object.assign(
{},
raw && { stdio: 'inherit' },
/^win/.test(process.platform) && { detached: false },
colorSupport && { env: Object.assign({ FORCE_COLOR: colorSupport.level }, process.env) }
colorSupport && { env: Object.assign({ FORCE_COLOR: colorSupport.level }, process.env, env) }
gustavohenke marked this conversation as resolved.
Show resolved Hide resolved
);