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

Support Exclude patterns. #306

Merged
merged 4 commits into from Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions README.md
@@ -1,6 +1,6 @@
# Concurrently

[![Build Status](https://github.com/open-cli-tools/concurrently/workflows/Tests/badge.svg)](https://github.com/open-cli-tools/concurrently/actions?workflow=Tests)
[![Build Status](https://github.com/open-cli-tools/concurrently/workflows/Tests/badge.svg)](https://github.com/open-cli-tools/concurrently/actions?workflow=Tests)
[![Coverage Status](https://coveralls.io/repos/github/open-cli-tools/concurrently/badge.svg?branch=master)](https://coveralls.io/github/open-cli-tools/concurrently?branch=master)

[![NPM Badge](https://nodei.co/npm/concurrently.png?downloads=true)](https://www.npmjs.com/package/concurrently)
Expand Down Expand Up @@ -109,6 +109,26 @@ concurrently -n w: npm:watch-*
concurrently -n w:js,w:css,w:node "npm run watch-js" "npm run watch-css" "npm run watch-node"
```

Exclusion is also supported. Given the following scripts in package.json:
```javascript
{
// ...
"scripts": {
"lint:js": "...",
"lint:ts": "...",
"lint:fix:js": "...",
"lint:fix:ts": "...",
// ...
}
// ...
}
```
```bash
# Running only lint:js and lint:ts
# with lint:fix:js and lint:fix:ts excluded
concurrently "npm:lint:*(!fix)"
```

Good frontend one-liner example [here](https://github.com/kimmobrunfeldt/dont-copy-paste-this-frontend-template/blob/5cd2bde719654941bdfc0a42c6f1b8e69ae79980/package.json#L9).

Help:
Expand Down Expand Up @@ -304,7 +324,7 @@ result.then(success, failure);
```

### `Command`
An object that contains all information about a spawned command, and ways to interact with it.
An object that contains all information about a spawned command, and ways to interact with it.<br>
It has the following properties:

- `index`: the index of the command among all commands spawned.
Expand All @@ -322,11 +342,11 @@ It has the following properties:
- `timer`: an RxJS observable to the command's timing events (e.g. starting, stopping).
- `close`: an RxJS observable to the command's close events.
See [`CloseEvent`](#CloseEvent) for more information.
- `start()`: starts the command, setting up all
- `start()`: starts the command, setting up all
- `kill([signal])`: kills the command, optionally specifying a signal (e.g. `SIGTERM`, `SIGKILL`, etc).

### `CloseEvent`
An object with information about a command's closing event.
An object with information about a command's closing event.<br>
It contains the following properties:

- `command`: a stripped down version of [`Command`](#command), including only `name`, `command`, `env` and `cwd` properties.
Expand Down
4 changes: 4 additions & 0 deletions bin/epilogue.ts
Expand Up @@ -49,6 +49,10 @@ const examples = [
{
description: 'Shortened NPM run command with wildcard (make sure to wrap it in quotes!)',
example: '$ $0 "npm:watch-*"',
},
{
description: 'Exclude patterns so that between "lint:js" and "lint:fix:js", only "lint:js" is ran',
example: '$ $0 "npm:*(!fix)"'
}
];

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/command-parser/expand-npm-wildcard.spec.ts
Expand Up @@ -100,6 +100,22 @@ for (const npmCmd of ['npm', 'yarn', 'pnpm']) {
]);
});

it('allows negation', () => {
readPkg.mockReturnValue({
scripts: {
'lint:js': '',
'lint:ts': '',
'lint:fix:js': '',
'lint:fix:ts': '',
}
});

expect(parser.parse(createCommandInfo(`${npmCmd} run lint:*(!fix)`))).toEqual([
{ name: 'js', command: `${npmCmd} run lint:js` },
{ name: 'ts', command: `${npmCmd} run lint:ts` },
]);
});

it('caches scripts upon calls', () => {
readPkg.mockReturnValue({});
parser.parse(createCommandInfo(`${npmCmd} run foo-*-baz qux`));
Expand Down
17 changes: 15 additions & 2 deletions src/command-parser/expand-npm-wildcard.ts
Expand Up @@ -3,6 +3,9 @@ import * as _ from 'lodash';
import { CommandInfo } from '../command';
import { CommandParser } from './command-parser';


const OMISSION = /\(!([^\)]+)\)/;

/**
* Finds wildcards in npm/yarn/pnpm run commands and replaces them with all matching scripts in the
* `package.json` file of the current directory.
Expand Down Expand Up @@ -35,15 +38,25 @@ export class ExpandNpmWildcard implements CommandParser {
this.scripts = Object.keys(this.readPackage().scripts || {});
}

const preWildcard = _.escapeRegExp(cmdName.substr(0, wildcardPosition));
const postWildcard = _.escapeRegExp(cmdName.substr(wildcardPosition + 1));
const omissionRegex = cmdName.match(OMISSION);
const cmdNameSansOmission = cmdName.replace(OMISSION, '');
const preWildcard = _.escapeRegExp(cmdNameSansOmission.substr(0, wildcardPosition));
const postWildcard = _.escapeRegExp(cmdNameSansOmission.substr(wildcardPosition + 1));
const wildcardRegex = new RegExp(`^${preWildcard}(.*?)${postWildcard}$`);
const currentName = commandInfo.name || '';

return this.scripts
.map(script => {
const match = script.match(wildcardRegex);

if (omissionRegex) {
const toOmit = script.match(new RegExp(omissionRegex[1]));

if (toOmit) {
return;
}
}

if (match) {
return Object.assign({}, commandInfo, {
command: `${npmCmd} run ${script}${args}`,
Expand Down