Skip to content

Commit 7b427d6

Browse files
authored
Correct wildcard naming as described in README (#385)
1 parent fa1bd17 commit 7b427d6

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/command-parser/expand-npm-wildcard.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ for (const npmCmd of ['npm', 'yarn', 'pnpm']) {
8383
]);
8484
});
8585

86+
it('uses wildcard match of script as command name', () => {
87+
readPkg.mockReturnValue({
88+
scripts: {
89+
'watch-js': '',
90+
'watch-css': '',
91+
},
92+
});
93+
94+
expect(
95+
parser.parse({
96+
name: '',
97+
command: `${npmCmd} run watch-*`,
98+
})
99+
).toEqual([
100+
{ name: 'js', command: `${npmCmd} run watch-js` },
101+
{ name: 'css', command: `${npmCmd} run watch-css` },
102+
]);
103+
});
104+
86105
it('uses existing command name as prefix to the wildcard match', () => {
87106
readPkg.mockReturnValue({
88107
scripts: {

src/command-parser/expand-npm-wildcard.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export class ExpandNpmWildcard implements CommandParser {
4444
const preWildcard = _.escapeRegExp(cmdNameSansOmission.slice(0, wildcardPosition));
4545
const postWildcard = _.escapeRegExp(cmdNameSansOmission.slice(wildcardPosition + 1));
4646
const wildcardRegex = new RegExp(`^${preWildcard}(.*?)${postWildcard}$`);
47-
const currentName = commandInfo.name || '';
47+
// If 'commandInfo.name' doesn't match 'cmdName', this means a custom name
48+
// has been specified and thus becomes the prefix (as described in the README).
49+
const prefix = commandInfo.name !== cmdName ? commandInfo.name : '';
4850

4951
return this.scripts
5052
.map((script) => {
@@ -62,9 +64,9 @@ export class ExpandNpmWildcard implements CommandParser {
6264
return {
6365
...commandInfo,
6466
command: `${npmCmd} run ${script}${args}`,
65-
// Will use an empty command name if command has no name and the wildcard match is empty,
66-
// e.g. if `npm:watch-*` matches `npm run watch-`.
67-
name: currentName + match[1],
67+
// Will use an empty command name if no prefix has been specified and
68+
// the wildcard match is empty, e.g. if `npm:watch-*` matches `npm run watch-`.
69+
name: prefix + match[1],
6870
};
6971
}
7072
})

0 commit comments

Comments
 (0)