【问题标题】:Running a command with arguments in gulp在 gulp 中运行带有参数的命令
【发布时间】:2016-12-21 19:21:14
【问题描述】:

我在 gulp 中重写了一些 bash 代码,这些代码为不同的浏览器生成了几个附加组件/扩展,灵感来自 GitHub 上的 ublockorigin 项目。

对于 Firefox,有一行代码应该运行一个以目标目录作为参数的 python 脚本。在 gulp 中,我很难运行这个 python 脚本。

我尝试了gulp-rungulp-shellchild_process,但它们都没有给我正确的输出。

当我从命令行运行python ./tools/make-firefox-meta.py ../firefox_debug/ 时,我得到了我想要的结果并创建了firefox_debug 目录。

这是我的gulp-run 代码:

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

这是给了我这个而实际上没有做任何事情:

$ 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/

这是我的gulp-shell 代码:

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

这给了我这个没有实际结果:

$ 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

这是我为child_process 编写的代码:这是最有前途的代码,因为我在命令行上看到了 python 的一些输出。

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);
});

它给了我这个输出:

[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

谁能告诉我,我应该做些什么改变才能让它工作?

【问题讨论】:

    标签: python gulp firefox-addon child-process gulp-shell


    【解决方案1】:

    最后一个使用child_process.spawn() 确实是我推荐的方法,但是您将参数传递给python 可执行文件的方式是错误的。

    每个参数都必须作为数组的单独元素传递。你不能只传递一个字符串。 spawn() 会将字符串解释为单个参数,python 将查找 字面上 名为 ./tools/make-firefox-meta.py `../firefox_debug/` 的文件。当然,这不存在。

    所以不要这样:

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

    你需要这样做:

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

    你还需要正确地发信号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);
    });
    

    【讨论】:

    • 听起来比以前好。 gulp 在 13 毫秒后完成了“python-bsff”,但是他们的 python 文件应该做的更改不存在。任何与目录寻址相关的东西都可能导致这种情况?
    • 它在 13 毫秒后完成,因为您没有正确地发出异步完成信号(请参阅我的编辑)。还可以尝试在命令行上运行python ./tools/make-firefox-meta.py ../firefox_debug/(在您的gulpfile.js 所在的目录中)。如果它在那里工作,它将从 gulp 工作。
    • 现在,它正在工作。我从你那里学到了关于 asyc 任务的信号。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多