fswatchshelljs

Sending response after file changes


I want to execute a commande in matlab using shelljs and after it finish send response to client :

 var cmd = 'matlab -nojvm -nosplash -nodesktop -noFigureWindows  -minimize -r  \"  senario(); exit; \"';
     shell.exec(cmd, function (code, stdout, stderr) {
            console.log('matlab exit');
        });

I didn't know how to get the response so I made matlab change a file with Code (Ok, Fail) and watched it using fswatch, my aim was after a change send the 200 status, but it is throwing an error while testing with Postman.

  fs.watch("done.json", function (event, filename) {
    console.log(filename + ' file Changed ...');
        return res.status(200); // I want to Add the response here
    if (filename) {
        console.log('filename provided: ' + filename);
    } else {
        console.log('filename not provided');
    }
});

I know that seems not the good way to do so, but any help


Solution

  • I've solved the issue by promisifying fs.watch

    var Promise = require('bluebird');
    var cmd = ['matlab -nojvm -nosplash -nodesktop -noFigureWindows  -minimize -r  \"  addpath(genpath(\''+ __dirname+'\\..\\uploads\')); '+ nameSenarioToExe+'(); exit; \"'];///
         shell.exec(cmd, function (code, stdout, stderr) {
                console.log('matlab exit');
            }); 
        var p = Promise.resolve(DistDir)
         .timeout(2000)
         .catch(console.error.bind(console, 'Failed to load config!'))
         .then( fs.watch(filedir, function(event, who) {
            if (event === 'rename' && who === filename) {
              if (fs.existsSync(filedir)) {
               res.send('OK');
              } 
            }
          }));