|
9 | 9 | "os" |
10 | 10 | "os/exec" |
11 | 11 | "path/filepath" |
| 12 | + "regexp" |
12 | 13 | "runtime" |
13 | 14 | "strings" |
14 | 15 |
|
@@ -172,32 +173,22 @@ func (p *NpmProvider) rewriteCmdNodePath(cmdPath, nodePath string) error { |
172 | 173 | content := string(data) |
173 | 174 |
|
174 | 175 | // 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") { |
177 | 177 | return nil |
178 | 178 | } |
179 | 179 |
|
180 | 180 | replacement := fmt.Sprintf(`SET "_prog=%s"`, nodePath) |
181 | 181 |
|
182 | 182 | // 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) |
201 | 192 | } |
202 | 193 |
|
203 | 194 | // Pattern 2 — older npm one-liner: "%~dp0\node.exe" … |
|
0 commit comments