How can I be notified with a sound whenever esbuild fails to build due to errors in source?

96 views Asked by At

I always setup esbuild to watch for changes and rebuild automatically, but I'm not notified when build fails due to source errors that I've missed, other than the console which is minimized under the development windows.

I'm searching for hours and I'm surprised I couldn't find any mention of alerting the user when esbuild fails that doesn't rely in watching the console all the time and taking valuable screen space (impossible with a single monitor).

That has made me numerous times searching for errors on the wrong places only to realize after 10 minutes or so that the source wasn't even built in the first time...

So, I consider it ideal to be notified by just playing an mp3 warning sound. How can this be done?

I tried the following:

  1. I changed the build line in package.json to: "build": "node build.js",

  2. I created a build.js file with the following:


const { exec } = require('child_process');
const soundPlay = require('sound-play');

function executeBuildCommand() {
  return new Promise((resolve, reject) => {
    const buildCommand = 'esbuild src/main.js --watch --bundle --minify --sourcemap --outdir=dist --target=edge118,firefox118,safari17'; // Replace with your actual build command

    exec(buildCommand, (error, stdout, stderr) => {
      if (error || stderr) {
        reject(error || stderr);
      } else {
        resolve(stdout);
      }
    });
  });
}

async function buildWithNotification() {
  try {
    const buildOutput = await executeBuildCommand();

    // Check if there are any warnings or errors
    if (buildOutput.includes('error') || buildOutput.includes('warning')) {
      const errorMessage = buildOutput.includes('error') ? 'Build failed with errors' : 'Build completed with warnings';
      console.error(errorMessage);

      // Play an alert sound
      soundPlay.play('alert.mp3', (err) => {
        if (err) {
          console.error('Error playing sound:', err);
        }
      });
    } else {
      console.log('Build completed successfully');
    }
  } catch (error) {
    console.error('esbuild error:', error);

    // Play an alert sound
    soundPlay.play('alert.mp3', (err) => {
      if (err) {
        console.error('Error playing sound:', err);
      }
    });
  }
}

buildWithNotification().catch((err) => {
  console.error('An unexpected error occurred:', err);
});

  1. and I run esbuild with: npm run build.

But it doesn't work.

0

There are 0 answers