fix(ci): patch nyc@15 to handle glob@10+ and rimraf@5+

Add postinstall script that patches nyc/index.js to handle modern versions
of glob and rimraf. These packages now export objects/named exports instead
of functions, which breaks nyc@15's promisify() calls.

This fixes the 'original argument must be of type function' error.

# Conflicts:
#	webapp/scripts/patch-nyc.cjs
This commit is contained in:
Benjamin
2025-11-24 10:32:10 +01:00
parent 825705b036
commit 253b26511a

View File

@@ -1,76 +1,42 @@
#!/usr/bin/env node
/**
* Patch nyc@15 to handle Node.js 20+ promisify issues
* Patch nyc@15 to handle glob@10+ and rimraf@5+
* This fixes the "original argument must be of type function" error
*/
const fs = require('fs');
const path = require('path');
const nycPath = path.join(__dirname, '../node_modules/@cypress/code-coverage/node_modules/nyc/lib/fs-promises.js');
const nycIndexPath = path.join(__dirname, '../node_modules/@cypress/code-coverage/node_modules/nyc/index.js');
if (!fs.existsSync(nycPath)) {
console.log('⚠️ nyc fs-promises.js not found, skipping patch');
if (!fs.existsSync(nycIndexPath)) {
console.log('⚠️ nyc/index.js not found, skipping patch');
process.exit(0);
}
const patchedContent = `'use strict'
// Read current content
let content = fs.readFileSync(nycIndexPath, 'utf-8');
const fs = require('fs')
// Check if already patched
if (content.includes('// PATCHED for glob@10+')) {
console.log('✅ nyc@15 already patched');
process.exit(0);
}
const { promisify } = require('util')
// Patch glob and rimraf imports to handle modern versions
content = content.replace(
"const glob = promisify(require('glob'))",
`// PATCHED for glob@10+ compatibility
const globModule = require('glob')
const glob = typeof globModule === 'function' ? promisify(globModule) : globModule.glob`
);
module.exports = { ...fs }
content = content.replace(
"const rimraf = promisify(require('rimraf'))",
`// PATCHED for rimraf@5+ compatibility
const rimrafModule = require('rimraf')
const rimraf = typeof rimrafModule === 'function' ? promisify(rimrafModule) : rimrafModule.rimraf`
);
// Promisify all functions for consistency
const fns = [
'access',
'appendFile',
'chmod',
'chown',
'close',
'copyFile',
'fchmod',
'fchown',
'fdatasync',
'fstat',
'fsync',
'ftruncate',
'futimes',
'lchmod',
'lchown',
'link',
'lstat',
'mkdir',
'mkdtemp',
'open',
'read',
'readdir',
'readFile',
'readlink',
'realpath',
'rename',
'rmdir',
'stat',
'symlink',
'truncate',
'unlink',
'utimes',
'write',
'writeFile'
]
fns.forEach(fn => {
/* istanbul ignore else: all functions exist on OSX */
if (fs[fn]) {
try {
module.exports[fn] = promisify(fs[fn])
} catch (err) {
// Fallback to original function if promisify fails (Node.js 20+ compat)
module.exports[fn] = fs[fn]
}
}
})
`;
fs.writeFileSync(nycPath, patchedContent);
console.log('✅ Patched nyc@15 for Node.js 20+ compatibility');
fs.writeFileSync(nycIndexPath, content);
console.log('✅ Patched nyc@15 for glob@10+ and rimraf@5+ compatibility');