Skip to content

Commit

Permalink
Fix wildcard naming to match documented behavior (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit authored Dec 19, 2021
1 parent ecc5fa0 commit dd54b9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/command-parser/expand-npm-wildcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@ module.exports = class ExpandNpmWildcard {
const preWildcard = _.escapeRegExp(cmdName.substr(0, wildcardPosition));
const postWildcard = _.escapeRegExp(cmdName.substr(wildcardPosition + 1));
const wildcardRegex = new RegExp(`^${preWildcard}(.*?)${postWildcard}$`);
const currentName = commandInfo.name || '';

return this.scripts
.filter(script => wildcardRegex.test(script))
.map(script => Object.assign({}, commandInfo, {
command: `${npmCmd} run ${script}${args}`,
name: script
}));
.map(script => {
const match = script.match(wildcardRegex);

if (match) {
return Object.assign({}, commandInfo, {
command: `${npmCmd} run ${script}${args}`,
// Will use an empty command name if command has no name and the wildcard match is empty,
// e.g. if `npm:watch-*` matches `npm run watch-`.
name: currentName + match[1],
});
}
})
.filter(Boolean);
}
};
21 changes: 19 additions & 2 deletions src/command-parser/expand-npm-wildcard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,25 @@ for (const npmCmd of ['npm', 'yarn', 'pnpm']) {
});

expect(parser.parse({ command: `${npmCmd} run foo-*-baz qux` })).toEqual([
{ name: 'foo-bar-baz', command: `${npmCmd} run foo-bar-baz qux` },
{ name: 'foo--baz', command: `${npmCmd} run foo--baz qux` },
{ name: 'bar', command: `${npmCmd} run foo-bar-baz qux` },
{ name: '', command: `${npmCmd} run foo--baz qux` },
]);
});

it('uses existing command name as prefix to the wildcard match', () => {
readPkg.mockReturnValue({
scripts: {
'watch-js': '',
'watch-css': '',
}
});

expect(parser.parse({
name: 'w:',
command: `${npmCmd} run watch-*`,
})).toEqual([
{ name: 'w:js', command: `${npmCmd} run watch-js` },
{ name: 'w:css', command: `${npmCmd} run watch-css` },
]);
});

Expand Down

0 comments on commit dd54b9f

Please sign in to comment.