Skip to content

Commit

Permalink
Support Exclude patterns. Resolves open-cli-tools#305
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jan 30, 2022
1 parent 025db2f commit feb74b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
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

0 comments on commit feb74b2

Please sign in to comment.