Run shell script from Meteor, including compatibility with mup

I need to run a shell script to post-process a downloaded file. The library I am trying to call in my shell script is gltf-transform, which has a CLI that can be installed by npm and also be executed by npx. What is the best approach in this case to make this work both locally (dev machine) as in the EC2 instance created by mup?

Is there no viable option to use npm pre/post scripts? scripts | npm Docs

Thanks for the feedback. I actually meant to trigger this script by the server at runtime, not on npm install.

I resolved this now by using the Node script instead, but I would still be curious how a shell script could be triggered by Meteor.

1 Like

you can use child_process that allow you to capture the output

The exec function runs the script asynchronously and collects both stdout and stderr in a callback .

const { exec } = require('child_process');
const util = require('util');
const execPromise = util.promisify(exec);

async function runScript(scriptPath) {
  try {
    const { stdout, stderr } = await execPromise(`bash ${scriptPath}`);
    console.log('Output:', stdout);
    if (stderr) console.error('Error:', stderr);
    return { stdout, stderr };
  } catch (error) {
    console.error('Execution failed:', error);
    throw error;
  }
}

// Usage
runScript('./my-script.sh');

For real-time output (useful for long-running scripts), use spawn instead:

const { spawn } = require('child_process');

function runScriptStream(scriptPath) {
  return new Promise((resolve, reject) => {
    const process = spawn('bash', [scriptPath]);
    
    let output = '';
    process.stdout.on('data', (data) => {
      output += data.toString();
      process.stdout.pipe(process.stdout); // Real-time display
    });
    
    process.stderr.on('data', (data) => {
      console.error(`Error: ${data}`);
    });
    
    process.on('close', (code) => {
      if (code === 0) resolve(output);
      else reject(new Error(`Exit code: ${code}`));
    });
  });
}
1 Like