From 7b427d67ec6178d0f98f6a7dbdbddb336bacef28 Mon Sep 17 00:00:00 2001 From: Pascal Jufer Date: Fri, 18 Nov 2022 10:36:57 +0100 Subject: [PATCH] Correct wildcard naming as described in README (#385) --- .../expand-npm-wildcard.spec.ts | 19 +++++++++++++++++++ src/command-parser/expand-npm-wildcard.ts | 10 ++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/command-parser/expand-npm-wildcard.spec.ts b/src/command-parser/expand-npm-wildcard.spec.ts index 9c0a7954..7a72826c 100644 --- a/src/command-parser/expand-npm-wildcard.spec.ts +++ b/src/command-parser/expand-npm-wildcard.spec.ts @@ -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: { diff --git a/src/command-parser/expand-npm-wildcard.ts b/src/command-parser/expand-npm-wildcard.ts index 6e674911..72ebe728 100644 --- a/src/command-parser/expand-npm-wildcard.ts +++ b/src/command-parser/expand-npm-wildcard.ts @@ -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) => { @@ -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], }; } })