#!/usr/bin/env node /* Reintento: extracción por marcador de siguiente función (no por llaves). */ const fs = require('fs'); const { execSync } = require('child_process'); let cur = fs.readFileSync('pos/static/js/inventory.js', 'utf8'); // 1. Quitar el bloque restaurado mal insertado const startMark = '\n // =====================================================================\n // RESTAURADAS (borradas por error en el patch de transferencias)'; const endMark = '\n // =====================================================================\n // EXPOSE GLOBALS (for onclick handlers in HTML)'; const i1 = cur.indexOf(startMark); const i2 = cur.indexOf(endMark); if (i1 !== -1 && i2 !== -1 && i2 > i1) { cur = cur.slice(0, i1) + cur.slice(i2); console.log('bloque malo removido'); } else { console.log('bloque restaurado no encontrado (¿ya limpio?)', i1, i2); } // 2. Extraer funciones de HEAD por marcadores const old = execSync('git show HEAD:pos/static/js/inventory.js', { maxBuffer: 20 * 1024 * 1024 }).toString(); const MISSING = ['viewHistory', 'closeHistoryModal', 'recordTransfer', 'approvePhysicalCount', 'cancelDraft', 'loadAlerts', 'printBarcode', 'deleteItem', 'publishToMeli']; function extractFn(name) { const start = '\n function ' + name + '('; const i = old.indexOf(start); if (i === -1) return null; // fin: el próximo " function " o " // ====" a nivel 4 después del inicio let end = old.length; const nextFn = old.indexOf('\n function ', i + start.length); const nextMark = old.indexOf('\n // =====================================================================', i + start.length); if (nextFn !== -1) end = Math.min(end, nextFn); if (nextMark !== -1) end = Math.min(end, nextMark); return old.slice(i + 1, end); } const blocks = []; for (const name of MISSING) { const b = extractFn(name); if (b) blocks.push(b.trimEnd()); else console.log('⚠ no encontrado en HEAD:', name); } const anchor = endMark; const restore = '\n // =====================================================================\n' + ' // RESTAURADAS (recuperadas de git HEAD)\n' + ' // =====================================================================\n\n' + blocks.join('\n\n') + '\n' + anchor; cur = cur.replace(anchor, restore); // printBarcode versión Code128 const iStart = cur.indexOf(' function printBarcode('); if (iStart !== -1) { let end = cur.length; const nextFn = cur.indexOf('\n function ', iStart + 30); const nextMark = cur.indexOf('\n // =====================================================================', iStart + 30); if (nextFn !== -1) end = Math.min(end, nextFn); if (nextMark !== -1) end = Math.min(end, nextMark); const code128 = ` function code128Svg(data, width, height) { data = String(data || ''); if (!data.length) return ''; var codes = [104]; var sum = 104; for (var i = 0; i < data.length; i++) { var c = data.charCodeAt(i); if (c < 32 || c > 126) c = 32; var v = c - 32; codes.push(v); sum += v * (i + 1); } codes.push(sum % 103); codes.push(106); var quiet = 10; var modules = quiet * 2; codes.forEach(function (cd) { CODE128_BARS[cd].split('').forEach(function (w) { modules += parseInt(w, 10); }); }); var mw = width / modules; var bars = ''; var x = quiet * mw; codes.forEach(function (cd) { var pat = CODE128_BARS[cd]; for (var k = 0; k < pat.length; k++) { var w = parseInt(pat[k], 10) * mw; if (k % 2 === 0) bars += ''; x += w; } }); return '' + bars + ''; } function printBarcode(barcode, partNumber, name) { var w = window.open('', '_blank', 'width=420,height=300'); w.document.write('Etiqueta'); w.document.write(code128Svg(barcode || partNumber, 320, 70)); w.document.write('

' + (barcode || partNumber) + '

'); w.document.write('

' + partNumber + '

'); w.document.write('

' + name + '

'); w.document.write(''); w.document.close(); w.print(); } `; cur = cur.slice(0, iStart) + code128 + cur.slice(end); console.log('printBarcode Code128 OK'); } fs.writeFileSync('pos/static/js/inventory.js', cur); console.log('restauración v2 completa');