Files

92 lines
2.9 KiB
JavaScript

/**
* Emergency patch for lodash.template@4.18.x (broken assignWith).
* When package.json "overrides" pins lodash.template@4.5.0, do NOT run this — it is unnecessary and may corrupt the file.
*
* Usage: node fix-lodash-manual.js
*/
const fs = require('fs');
const path = require('path');
const lodashTemplatePkg = path.join(__dirname, 'node_modules', 'lodash.template', 'package.json');
if (fs.existsSync(lodashTemplatePkg)) {
try {
const ver = JSON.parse(fs.readFileSync(lodashTemplatePkg, 'utf8')).version || '';
if (ver.startsWith('4.5.')) {
console.log('✓ lodash.template is already 4.5.x (overrides); skip manual patch');
process.exit(0);
}
} catch (e) {
/* continue to patch path */
}
}
const lodashTemplatePath = path.join(__dirname, 'node_modules', 'lodash.template', 'index.js');
if (!fs.existsSync(lodashTemplatePath)) {
console.error('❌ lodash.template not found at:', lodashTemplatePath);
console.error('Please run npm install or yarn install first');
process.exit(1);
}
let content = fs.readFileSync(lodashTemplatePath, 'utf8');
// Check if already patched with new version
if (content.includes('/* LODASH_TEMPLATE_PATCHED_V2 */')) {
console.log('✓ lodash.template already patched with v2');
process.exit(0);
}
// Remove old patch if exists
if (content.includes('/* LODASH_TEMPLATE_PATCHED */')) {
console.log('🔄 Removing old patch...');
content = content.replace(/\/\* LODASH_TEMPLATE_PATCHED \*\/\n.*assignWith[\s\S]*?^\}/m, '');
}
console.log('🔧 Patching lodash.template with v2...');
// Correct assignWith implementation that handles undefined customizer
const assignWithImpl = `/* LODASH_TEMPLATE_PATCHED_V2 */
// assignWith polyfill for lodash.template - Fixed version
function assignWith(object, source, customizer) {
if (object == null) return object;
if (typeof customizer !== 'function') {
customizer = function(objValue, srcValue) {
return srcValue;
};
}
var props = Object.keys(Object(source));
for (var i = 0; i < props.length; i++) {
var key = props[i];
var value = source[key];
var assignedValue = customizer(object[key], value, key, object, source);
if (assignedValue !== undefined) {
object[key] = assignedValue;
}
}
return object;
}
`;
// Find first line break after comments
let insertPos = 0;
const lines = content.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].trim() === ' */' || lines[i].trim() === '*/') {
insertPos = content.indexOf('\n', content.indexOf(lines[i])) + 1;
break;
}
}
if (insertPos === 0) {
insertPos = content.indexOf('\n') + 1;
}
const before = content.substring(0, insertPos);
const after = content.substring(insertPos);
content = before + '\n' + assignWithImpl + after;
fs.writeFileSync(lodashTemplatePath, content);
console.log('✅ Successfully patched lodash.template with assignWith function (v2)');
console.log(' File:', lodashTemplatePath);