From bb7c815d7f0e557afcf86334652ae895f8104c5d Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Mon, 6 Jan 2020 07:38:53 -0500 Subject: [PATCH 1/2] fix: bugfix for wildcard naming #211 --- src/command-parser/expand-npm-wildcard.js | 18 +++++++++++++----- src/command-parser/expand-npm-wildcard.spec.js | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/command-parser/expand-npm-wildcard.js b/src/command-parser/expand-npm-wildcard.js index 02ff9511..edd635f1 100644 --- a/src/command-parser/expand-npm-wildcard.js +++ b/src/command-parser/expand-npm-wildcard.js @@ -25,10 +25,18 @@ module.exports = class ExpandNpmWildcard { const wildcardRegex = new RegExp(`^${preWildcard}(.*?)${postWildcard}$`); 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}`, + // Use the wildcard portion of the script match unless it's empty, + // in which case use the full name of the script + name: match[1] || script + }); + } + }) + .filter(Boolean); } }; diff --git a/src/command-parser/expand-npm-wildcard.spec.js b/src/command-parser/expand-npm-wildcard.spec.js index 940d475c..51af280d 100644 --- a/src/command-parser/expand-npm-wildcard.spec.js +++ b/src/command-parser/expand-npm-wildcard.spec.js @@ -42,7 +42,7 @@ for (const npmCmd of ['npm', 'yarn']) { }); expect(parser.parse({ command: `${npmCmd} run foo-*-baz qux` })).toEqual([ - { name: 'foo-bar-baz', command: `${npmCmd} run foo-bar-baz qux` }, + { name: 'bar', command: `${npmCmd} run foo-bar-baz qux` }, { name: 'foo--baz', command: `${npmCmd} run foo--baz qux` }, ]); }); From 59c00ad15c713eeb816b5e076eebacd7d4bdf42f Mon Sep 17 00:00:00 2001 From: Gustavo Henke Date: Sun, 19 Dec 2021 16:59:53 +1100 Subject: [PATCH 2/2] Use command name and empty wildcard match --- src/command-parser/expand-npm-wildcard.js | 7 ++++--- .../expand-npm-wildcard.spec.js | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/command-parser/expand-npm-wildcard.js b/src/command-parser/expand-npm-wildcard.js index b091a200..9df1ae4d 100644 --- a/src/command-parser/expand-npm-wildcard.js +++ b/src/command-parser/expand-npm-wildcard.js @@ -32,6 +32,7 @@ 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 .map(script => { @@ -40,9 +41,9 @@ module.exports = class ExpandNpmWildcard { if (match) { return Object.assign({}, commandInfo, { command: `${npmCmd} run ${script}${args}`, - // Use the wildcard portion of the script match unless it's empty, - // in which case use the full name of the script - name: match[1] || script + // 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], }); } }) diff --git a/src/command-parser/expand-npm-wildcard.spec.js b/src/command-parser/expand-npm-wildcard.spec.js index 86ce12b5..bb40ae07 100644 --- a/src/command-parser/expand-npm-wildcard.spec.js +++ b/src/command-parser/expand-npm-wildcard.spec.js @@ -72,7 +72,24 @@ for (const npmCmd of ['npm', 'yarn', 'pnpm']) { expect(parser.parse({ command: `${npmCmd} run foo-*-baz qux` })).toEqual([ { name: 'bar', command: `${npmCmd} run foo-bar-baz qux` }, - { name: 'foo--baz', command: `${npmCmd} run foo--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` }, ]); });