|
1 | 1 | import fs from 'node:fs'; |
2 | 2 | import path from 'node:path'; |
3 | 3 |
|
4 | | -const filePath = path.resolve('node_modules/@eslint/eslintrc/lib/config-array/override-tester.js'); |
5 | | -const expected = 'import minimatch from "minimatch";'; |
6 | | -const replacement = 'import { minimatch } from "minimatch";'; |
| 4 | +const applyPatch = ({ filePath, expected, replacement, label }) => { |
| 5 | + if (!fs.existsSync(filePath)) { |
| 6 | + throw new Error(`Expected file for ${label} at ${filePath}`); |
| 7 | + } |
7 | 8 |
|
8 | | -if (!fs.existsSync(filePath)) { |
9 | | - throw new Error(`Expected ESLint override tester at ${filePath}`); |
10 | | -} |
| 9 | + const source = fs.readFileSync(filePath, 'utf8'); |
| 10 | + if (source.includes(replacement)) { |
| 11 | + console.log(`${label} patch already applied`); |
| 12 | + return; |
| 13 | + } |
11 | 14 |
|
12 | | -const source = fs.readFileSync(filePath, 'utf8'); |
| 15 | + if (!source.includes(expected)) { |
| 16 | + throw new Error(`Expected pattern not found for ${label}`); |
| 17 | + } |
13 | 18 |
|
14 | | -if (source.includes(replacement)) { |
15 | | - console.log('eslint minimatch patch already applied'); |
16 | | - process.exit(0); |
17 | | -} |
| 19 | + fs.writeFileSync(filePath, source.replace(expected, replacement)); |
| 20 | + console.log(`patched ${label}`); |
| 21 | +}; |
18 | 22 |
|
19 | | -if (!source.includes(expected)) { |
20 | | - throw new Error('Expected ESLint minimatch import pattern not found'); |
21 | | -} |
| 23 | +applyPatch({ |
| 24 | + filePath: path.resolve('node_modules/@eslint/eslintrc/lib/config-array/override-tester.js'), |
| 25 | + expected: 'import minimatch from "minimatch";', |
| 26 | + replacement: 'import { minimatch } from "minimatch";', |
| 27 | + label: '@eslint/eslintrc minimatch import' |
| 28 | +}); |
22 | 29 |
|
23 | | -fs.writeFileSync(filePath, source.replace(expected, replacement)); |
24 | | -console.log('patched @eslint/eslintrc minimatch import for minimatch v10'); |
| 30 | +{ |
| 31 | + const filePath = path.resolve('node_modules/@vscode/vsce/out/package.js'); |
| 32 | + if (!fs.existsSync(filePath)) { |
| 33 | + throw new Error(`Expected file for @vscode/vsce minimatch runtime shim at ${filePath}`); |
| 34 | + } |
| 35 | + |
| 36 | + const source = fs.readFileSync(filePath, 'utf8'); |
| 37 | + const importLine = 'const minimatch_1 = __importDefault(require("minimatch"));'; |
| 38 | + const oldShim = |
| 39 | + 'if (typeof minimatch_1.default !== "function" && typeof minimatch_1.default?.minimatch === "function") {\n' + |
| 40 | + ' minimatch_1.default = minimatch_1.default.minimatch;\n' + |
| 41 | + '}'; |
| 42 | + const newShim = |
| 43 | + 'if (typeof minimatch_1.default !== "function") {\n' + |
| 44 | + ' const minimatchFallback = typeof minimatch_1.minimatch === "function"\n' + |
| 45 | + ' ? minimatch_1.minimatch\n' + |
| 46 | + ' : (typeof minimatch_1.default?.minimatch === "function" ? minimatch_1.default.minimatch : undefined);\n' + |
| 47 | + ' if (minimatchFallback) {\n' + |
| 48 | + ' minimatch_1.default = minimatchFallback;\n' + |
| 49 | + ' }\n' + |
| 50 | + '}'; |
| 51 | + |
| 52 | + if (source.includes(newShim)) { |
| 53 | + console.log('@vscode/vsce minimatch runtime shim patch already applied'); |
| 54 | + } else if (source.includes(oldShim)) { |
| 55 | + fs.writeFileSync(filePath, source.replace(oldShim, newShim)); |
| 56 | + console.log('patched @vscode/vsce minimatch runtime shim'); |
| 57 | + } else if (source.includes(importLine)) { |
| 58 | + fs.writeFileSync(filePath, source.replace(importLine, `${importLine}\n${newShim}`)); |
| 59 | + console.log('patched @vscode/vsce minimatch runtime shim'); |
| 60 | + } else { |
| 61 | + throw new Error('Expected pattern not found for @vscode/vsce minimatch runtime shim'); |
| 62 | + } |
| 63 | +} |
0 commit comments