diff --git a/src/command-parser/expand-npm-wildcard.js b/src/command-parser/expand-npm-wildcard.js index 4b35fd42..9df1ae4d 100644 --- a/src/command-parser/expand-npm-wildcard.js +++ b/src/command-parser/expand-npm-wildcard.js @@ -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); } }; diff --git a/src/command-parser/expand-npm-wildcard.spec.js b/src/command-parser/expand-npm-wildcard.spec.js index 27d1aebf..bb40ae07 100644 --- a/src/command-parser/expand-npm-wildcard.spec.js +++ b/src/command-parser/expand-npm-wildcard.spec.js @@ -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` }, ]); });