Running a command with arguments in gulp

2.2k Views Asked by At

I'm rewriting some bash code in gulp that produces several add-ons/extensions for different browsers inspired by the ublockorigin project on GitHub.

For Firefox there's a line that is supposed to run a python script which takes a destination directory as an argument. In gulp, I am having a hard time to run this python script.

I tried gulp-run, gulp-shell, child_process, but none of them are giving me the correct output.

When I run python ./tools/make-firefox-meta.py ../firefox_debug/ from the command line I get my desired result and the firefox_debug directory is created.

Here is my code for gulp-run:

gulp.task("python-bsff", function(){
    return run("python ./tools/make-firefox-meta.py ../firefox_debug/").exec();
});

This is giving me this without actually doing anything:

$ gulp python-bsff
[14:15:53] Using gulpfile ~\dev\gulpfile.js
[14:15:53] Starting 'python-bsff'...
[14:15:54] Finished 'python-bsff' after 629 ms
$ python ./tools/make-firefox-meta.py ../firefox_debug/

Here is my code for gulp-shell:

gulp.task("python-bsff", function(){
   return shell.task(["./tools/make-firefox-meta.py ../firefox_debug/""]);
});

This is giving me this without actual result:

$ gulp python-bsff
[14:18:54] Using gulpfile ~\dev\gulpfile.js
[14:18:54] Starting 'python-bsff'...
[14:18:54] Finished 'python-bsff' after 168 μs

Here is my code for child_process: this was the most promising one, since I saw some output from python on the command line.

gulp.task("python-bsff", function(){
  var spawn = process.spawn;
  console.info('Starting python');
  var PIPE = {stdio: 'inherit'};
  spawn('python', ["./tools/make-firefox-meta.py `../firefox_debug/`"], PIPE);
});

It's giving me this output:

[14:08:59] Using gulpfile ~\dev\gulpfile.js
[14:08:59] Starting 'python-bsff'...
Starting python
[14:08:59] Finished 'python-bsff' after 172 ms
python: can't open file './tools/make-firefox-meta.py     ../firefox_debug/`': [Errno 2] No such file or directory

Can somebody tell me please, what change should I do to make it work?

1

There are 1 best solutions below

3
On BEST ANSWER

The last one using child_process.spawn() is indeed the approach I would recommend, but the way you're passing the arguments to the python executable is wrong.

Each argument has to be passed as a separate element of the array. You can't just pass a single string. spawn() will interpret the string as a single argument and python will look for a file that is literally named ./tools/make-firefox-meta.py `../firefox_debug/`. Which, of course, doesn't exist.

So instead of this:

spawn('python', ["./tools/make-firefox-meta.py `../firefox_debug/`"], PIPE);

You need to do this:

spawn('python', ["./tools/make-firefox-meta.py", "../firefox_debug/"], PIPE);

You also need to properly signal async completion:

gulp.task("python-bsff", function(cb) {
  var spawn = process.spawn;
  console.info('Starting python');
  var PIPE = {stdio: 'inherit'};
  spawn('python', ["./tools/make-firefox-meta.py", "../firefox_debug/"], PIPE).on('close', cb);
});