shell commands in production Vercel

39 Views Asked by At

I am developing in node.js the implementation of a thermal printer. To perform the printing I use commands, which work perfectly in local, but when I deploy with vercel it doesn't print. How can I solve this? I am deploying the backend in a graphql interface.

const { Printer, InMemory, Model, Align } = require('escpos-buffer');
const { spawn } = require('child_process');
const os = require('os');

async function openTerminal(bufferData) {
  const platform = os.platform();
  let printCommand;

  if (platform === 'win32') {
    const computerName = os.hostname();
    console.log('Computer Name:', computerName);
    printCommand = `print /d:\\\\%${computerName}%\\MHT-W5801`;
  } else {
    printCommand = 'lp -d MHT-W5801';
  }

  const terminalProcess = spawn(printCommand, [], {
    stdio: ['pipe', 'inherit', 'inherit'],
    shell: true,
  });

  terminalProcess.stdin.write(bufferData);

  terminalProcess.stdin.end();

  terminalProcess.on('open', (code) => {
    console.log(`La terminal se abrió con el código de salida ${code}`);
  });
  terminalProcess.on('exit', (code) => {
    console.log(`La terminal se cerró con el código de salida ${code}`);
  });

  terminalProcess.on('error', (err) => {
    console.error(`Error al abrir la terminal: ${err.message}`);
  });
}

async function printerCommand(data) {
  const model = new Model('POS-80');
  const connection = new InMemory();
  const printer = await Printer.CONNECT(model, connection);

  await printer.writeln('');
  await printer.withStyle({
    align: Align.Right,
  }, async () => {
    await printer.writeln(`Fecha: ${data.date}`);
    await printer.writeln(`Horario: ${data.hours}`);
  });

  await printer.writeln('--------------------------------');
  await printer.withStyle({
    align: Align.Left,
  }, async () => {
    await printer.writeln(data.nameclient);
    await printer.writeln(data.cellphonenumber);
    await printer.writeln(data.address);
  });
  await printer.writeln('--------------------------------');
  await printer.writeln(`${data.quantity}. ${data.menu}`);
  await printer.writeln('--------------------------------');
  await printer.withStyle({
    align: Align.Left,
  }, async () => {
    await printer.writeln(`Oservación cliente: ${data.observationClient}`);
    await printer.writeln(`Oservación pedido: ${data.observation}`);
    await printer.writeln(`Extra: ${data.extra}`);
  });
  await printer.writeln('');
  await printer.writeln('--------------------------------');
  await printer.writeln('');
  await printer.writeln('');
  await printer.writeln('');


  const bufferData = connection.buffer();

  await openTerminal(bufferData);
}

module.exports = printerCommand;

I have also tried to open the device with the 'usb' package but the open() function gives undefinded.

  const device = await findBySerialNumber('00000000011A');
  const webDevice = await WebUSBDevice.createInstance(device);
  await webDevice.open(); 

My idea is to have the terminal open in the background in deploy so that it executes the command but it is not working as I expect.

0

There are 0 best solutions below