Skip to content

Commit

Permalink
Compatibility: npm, yarn and pnpm run scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ayu-exorcist authored Jul 4, 2024
1 parent bac3905 commit 21eaf6b
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = pattern.match(ARGS_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) => pattern.match(ARGS_PATTERN))

return hasPlaceholder ? applyArguments(patterns, args) : patterns
}
Expand Down

0 comments on commit 21eaf6b

Please sign in to comment.