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

Correct wildcard naming as described in README #385

Merged
merged 1 commit into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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