Files
add-to-project/fix-regex.js
T

106 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-08-15 16:20:30 +00:00
#!/usr/bin/env node
/**
* Fix for misleading operator precedence in @octokit/request regex
2025-08-15 17:03:48 +00:00
* Changes /^text\/|charset=utf-8$/ to /^(text\/|charset=utf-8)$/
2025-08-15 16:20:30 +00:00
*/
2025-08-15 16:36:36 +00:00
const fs = require('fs')
2025-08-15 16:20:30 +00:00
const filesToFix = [
'node_modules/@actions/github/node_modules/@octokit/request/dist-src/fetch-wrapper.js',
'node_modules/@actions/github/node_modules/@octokit/request/dist-node/index.js',
'node_modules/@actions/github/node_modules/@octokit/request/dist-web/index.js',
2025-08-15 16:36:36 +00:00
]
2025-08-15 16:20:30 +00:00
/**
* Apply regex fix to content
* @param {string} content - The file content to fix
* @returns {string} - The fixed content
*/
function applyRegexFix(content) {
let fixedContent = content
// Fix the problematic regex pattern - add proper grouping to fix operator precedence
fixedContent = fixedContent.replace(/\/\^text\\?\/\|charset=utf-8\$?\//g, '/^(text\\/|charset=utf-8)$/')
fixedContent = fixedContent.replace(/\/\^text\/\|charset=utf-8\$?\//g, '/^(text/|charset=utf-8)$/')
return fixedContent
}
2025-08-15 16:20:30 +00:00
/**
* Fix a single file
* @param {string} filePath - Path to the file to fix
* @returns {{fixed: boolean, error: string|null}} - Result of the fix operation
*/
function fixFile(filePath) {
2025-08-15 17:03:48 +00:00
try {
const content = fs.readFileSync(filePath, 'utf8')
2025-08-15 17:03:48 +00:00
const originalContent = content
const fixedContent = applyRegexFix(content)
2025-08-15 16:36:36 +00:00
if (fixedContent !== originalContent) {
fs.writeFileSync(filePath, fixedContent, 'utf8')
return { fixed: true, error: null }
2025-08-15 17:03:48 +00:00
} else {
return { fixed: false, error: null }
2025-08-15 17:03:48 +00:00
}
} catch (error) {
return { fixed: false, error: error.message }
2025-08-15 16:20:30 +00:00
}
2025-08-15 16:36:36 +00:00
}
2025-08-15 16:20:30 +00:00
/**
* Main function to fix all files
* @param {string[]} files - Array of file paths to fix
* @returns {{filesFixed: number, results: Array}} - Summary of fix operations
*/
function fixAllFiles(files = filesToFix) {
const results = []
let filesFixed = 0
for (const filePath of files) {
const result = fixFile(filePath)
results.push({ filePath, ...result })
if (result.fixed) {
filesFixed++
}
}
return { filesFixed, results }
}
// Main execution when run as script
if (require.main === module) {
process.stdout.write('🔧 Applying regex fix for @octokit/request...\n')
const { filesFixed, results } = fixAllFiles()
for (const result of results) {
if (result.error) {
if (result.error.includes('ENOENT') || result.error.includes('no such file')) {
process.stdout.write(`⚠️ File not found: ${result.filePath}\n`)
} else {
process.stderr.write(`❌ Error fixing ${result.filePath}: ${result.error}\n`)
}
} else if (result.fixed) {
process.stdout.write(`✅ Fixed: ${result.filePath}\n`)
} else {
process.stdout.write(`️ No changes needed: ${result.filePath}\n`)
}
}
process.stdout.write(`\n🎉 Fix complete! ${filesFixed} files updated.\n`)
if (filesFixed > 0) {
process.stdout.write('Run "npm run build:package" to rebuild with the fix.\n')
}
}
// Export functions for testing
module.exports = {
applyRegexFix,
fixFile,
fixAllFiles
2025-08-15 16:20:30 +00:00
}