From b8d3dedbe2ec3c50228054f8cf7e371d2e364303 Mon Sep 17 00:00:00 2001 From: ayu <54145886+ayu-exorcist@users.noreply.github.com> Date: Wed, 3 Jul 2024 20:05:09 +0800 Subject: [PATCH] Compatibility: npm, yarn and pnpm run scripts --- lib/index.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index c35c034..f0ea4e0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -112,8 +112,29 @@ function applyArguments (patterns, args) { * @returns {string[]} Parsed patterns. */ function parsePatterns (patternOrPatterns, args) { - const patterns = toArray(patternOrPatterns) - const hasPlaceholder = patterns.some(pattern => ARGS_PATTERN.test(pattern)) + let patterns = toArray(patternOrPatterns) + + const isNPM = process.env.npm_config_user_agent && process.env.npm_config_user_agent.startsWith('npm') + + if (!isNPM) { + // yarn | pnpm + patterns = patterns.map((pattern) => { + const match = ARGS_PATTERN.exec(pattern) + + if (!match) { + return pattern + } + + const patternList = pattern.split(' ') + const doubleDashIndex = patternList.findIndex((item) => item === '--') + patternList.splice(doubleDashIndex, 1) + pattern = patternList.join(' ') + + return pattern + }) + } + + const hasPlaceholder = patterns.some((pattern) => ARGS_PATTERN.test(pattern)) return hasPlaceholder ? applyArguments(patterns, args) : patterns }