Skip to content

Commit 2aa562c

Browse files
committed
fix(npm): properly replace entire IF EXIST block in Windows cmd wrappers using regex
1 parent 8f4f5ba commit 2aa562c

1 file changed

Lines changed: 11 additions & 20 deletions

File tree

internal/provider/npm.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"regexp"
1213
"runtime"
1314
"strings"
1415

@@ -172,32 +173,22 @@ func (p *NpmProvider) rewriteCmdNodePath(cmdPath, nodePath string) error {
172173
content := string(data)
173174

174175
// Bail out quickly if neither known pattern is present.
175-
if !strings.Contains(content, `%dp0%\node.exe`) &&
176-
!strings.Contains(content, `%~dp0\node.exe`) {
176+
if !strings.Contains(content, "node.exe") {
177177
return nil
178178
}
179179

180180
replacement := fmt.Sprintf(`SET "_prog=%s"`, nodePath)
181181

182182
// Pattern 1 — npm 7+ conditional block (CRLF or LF tolerant).
183-
// Find "IF EXIST "%dp0%\node.exe" (" and replace through the closing ")".
184-
const ifExistMarker = `IF EXIST "%dp0%\node.exe" (`
185-
if idx := strings.Index(content, ifExistMarker); idx != -1 {
186-
// The block ends at the next bare ")" on its own line.
187-
// npm formats it as "\r\n)" (Windows) or "\n)" (Unix editors).
188-
after := content[idx:]
189-
// Look for the closing ")" — it follows a newline and is the first non-space char.
190-
closingIdx := -1
191-
for _, nl := range []string{"\r\n)", "\n)"} {
192-
if ci := strings.Index(after, nl); ci != -1 {
193-
if closingIdx == -1 || ci < closingIdx {
194-
closingIdx = ci + len(nl)
195-
}
196-
}
197-
}
198-
if closingIdx != -1 {
199-
content = content[:idx] + replacement + content[idx+closingIdx:]
200-
}
183+
// Match:
184+
// IF EXIST "%dp0%\node.exe" (
185+
// ...
186+
// ) ELSE (
187+
// ...
188+
// )
189+
reNpm7 := regexp.MustCompile(`(?i)IF EXIST "%~?dp0%?\\node\.exe" \([\s\S]*?\) ELSE \([\s\S]*?\)`)
190+
if reNpm7.MatchString(content) {
191+
content = reNpm7.ReplaceAllString(content, replacement)
201192
}
202193

203194
// Pattern 2 — older npm one-liner: "%~dp0\node.exe" …

0 commit comments

Comments
 (0)