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

Add new --matrix option to multiply commands #526

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cdrini
Copy link
Contributor

@cdrini cdrini commented Jan 16, 2025

Closes #517

Sample runs:

Single dimension:

$ dist/bin/concurrently.js --group --matrix 'A B C' 'echo {1}'
[0] A
[0] echo A exited with code 0
[1] B
[1] echo B exited with code 0
[2] C
[2] echo C exited with code 0

Multiple dimensions:

$ dist/bin/concurrently.js --group --matrix 'A B' --matrix '1 2' 'echo {1}{2}'
[0] A1
[0] echo A1 exited with code 0
[1] A2
[1] echo A2 exited with code 0
[2] B1
[2] echo B1 exited with code 0
[3] B2
[3] echo B2 exited with code 0

With environment variables:

$ SERVERS="$(echo ol-web{0..2})"
$ IMAGES="python:3.12-slim python:3.13-slim"
$ dist/bin/concurrently.js --group --matrix "$SERVERS" --matrix "$IMAGES" 'ssh {1} docker image pull {2}'
[0] 3.12-slim: Pulling from library/python
[0] af302e5c37e9: Pulling fs layer
[0] 499051ac08b3: Pulling fs layer
...
[0] bc54bb88b02b: Pull complete
[0] Digest: sha256:123be5684f39d8476e64f47a5fddf38f5e9d839baff5c023c815ae5bdfae0df7
[0] Status: Downloaded newer image for python:3.12-slim
[0] docker.io/library/python:3.12-slim
[0] ssh ol-web0 docker image pull python\:3.12-slim exited with code 0
[1] 3.13-slim: Pulling from library/python
[1] af302e5c37e9: Pulling fs layer
[1] 31fc40b7245b: Pulling fs layer
...
[1] e81d26d6c5a7: Pull complete
[1] Digest: sha256:23a81be7b258c8f516f7a60e80943cace4350deb8204cf107c7993e343610d47
[1] Status: Downloaded newer image for python:3.13-slim
[1] docker.io/library/python:3.13-slim
[1] ssh ol-web0 docker image pull python\:3.13-slim exited with code 0
[2] 3.12-slim: Pulling from library/python
[2] af302e5c37e9: Pulling fs layer
...

@cdrini cdrini force-pushed the 517/feature/matrix-option branch from ecdae50 to 5af7c99 Compare January 16, 2025 01:56
@coveralls
Copy link

Coverage Status

coverage: 98.032% (-0.1%) from 98.18%
when pulling ecdae50 on cdrini:517/feature/matrix-option
into 7f3efb2 on open-cli-tools:main.

@cdrini cdrini force-pushed the 517/feature/matrix-option branch from 5af7c99 to dfc3c5f Compare January 16, 2025 01:57
@cdrini cdrini force-pushed the 517/feature/matrix-option branch from dfc3c5f to f656f5d Compare January 16, 2025 02:16
@cdrini cdrini marked this pull request as ready for review January 16, 2025 02:38
@gustavohenke gustavohenke self-requested a review January 27, 2025 21:53
Copy link
Member

@gustavohenke gustavohenke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this up!
I made several suggestions and asked a few questions in this pass.

I'll update the docs to mention matrices as well in a follow-up, unless you feel like doing it while keeping it on brand?

* Replace placeholders with new commands for each combination of matrices.
*/
export class ExpandMatrices implements CommandParser {
private _bindings: string[][];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the _ is to signal that the field is private - if there isn't another field with the same name (such as a getter), let's not add that prefix.
It can also be made readonly:

Suggested change
private _bindings: string[][];
private readonly bindings: string[][];

private _bindings: string[][];

constructor(private readonly matrices: readonly string[][]) {
this.matrices = matrices;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line isn't necessary, as the private in the constructor param list does the same job.

Suggested change
this.matrices = matrices;

export class ExpandMatrices implements CommandParser {
private _bindings: string[][];

constructor(private readonly matrices: readonly string[][]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem like it's read outside of the constructor, so for now let's not make it a field

Suggested change
constructor(private readonly matrices: readonly string[][]) {
constructor(matrices: readonly string[][]) {

Comment on lines +30 to +31
let index = 0;
if (placeholderTarget && !isNaN(placeholderTarget)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should an error be thrown if the placeholder isn't a number?
Might convert a "wtf is concurrently doing" to a "oh, my bad, I got it wrong" 😄

@@ -103,6 +103,11 @@ export type ConcurrentlyOptions = Omit<BaseConcurrentlyOptions, 'abortSignal' |
* If not defined, no argument replacing will happen.
*/
additionalArguments?: string[];

/**
* This command should be run multiple times, for each of the provided matrices.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update this comment? It looks like it's assuming some context from elsewhere.

@@ -179,6 +185,10 @@ export function concurrently(
new ExpandWildcard(),
];

if (options.matrices?.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like matrices have preference over passthrough arguments using the same {number} syntax. Was it intended?

It needs to be called out in the help and in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CLI support for command templates/matrix commands
3 participants