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 1 commit
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
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