Skip to content

Commit

Permalink
Correct wildcard naming as described in README (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
paescuj committed Nov 18, 2022
1 parent fa1bd17 commit 7b427d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/command-parser/expand-npm-wildcard.spec.ts
Expand Up @@ -83,6 +83,25 @@ for (const npmCmd of ['npm', 'yarn', 'pnpm']) {
]);
});

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

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

it('uses existing command name as prefix to the wildcard match', () => {
readPkg.mockReturnValue({
scripts: {
Expand Down
10 changes: 6 additions & 4 deletions src/command-parser/expand-npm-wildcard.ts
Expand Up @@ -44,7 +44,9 @@ export class ExpandNpmWildcard implements CommandParser {
const preWildcard = _.escapeRegExp(cmdNameSansOmission.slice(0, wildcardPosition));
const postWildcard = _.escapeRegExp(cmdNameSansOmission.slice(wildcardPosition + 1));
const wildcardRegex = new RegExp(`^${preWildcard}(.*?)${postWildcard}$`);
const currentName = commandInfo.name || '';
// If 'commandInfo.name' doesn't match 'cmdName', this means a custom name
// has been specified and thus becomes the prefix (as described in the README).
const prefix = commandInfo.name !== cmdName ? commandInfo.name : '';

return this.scripts
.map((script) => {
Expand All @@ -62,9 +64,9 @@ export class ExpandNpmWildcard implements CommandParser {
return {
...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],
// Will use an empty command name if no prefix has been specified and
// the wildcard match is empty, e.g. if `npm:watch-*` matches `npm run watch-`.
name: prefix + match[1],
};
}
})
Expand Down

0 comments on commit 7b427d6

Please sign in to comment.