如何在Gulp中运行外部文件?(How do I run an external file within Gulp?)

系统教程 行业动态 更新时间:2024-06-14 16:53:07
如何在Gulp中运行外部文件?(How do I run an external file within Gulp?)

目前我正在使用“gulp-run”插件来运行.bat文件。 该插件现已被弃用,我正在寻找现在执行.bat的最佳方法。

当前代码:

var gulp = require('gulp'); var run = require('gulp-run'); module.exports = function() { run('c:/xxx/xxx/runme.bat').exec(); };

根据@cmrn建议的解决方案

var exec = require('child_process').exec; var batchLocation = 'c:/xxx/xxx/runme.bat'; gulp.task('task', function (cb) { exec(batchLocation, function (err, stdout, stderr) { console.log(stdout); console.log(stderr); cb(err); }); })

At the moment I'm using the "gulp-run" plugin to run a .bat file. That plugin has now been deprecated and I'm looking for the best way to execute the .bat now.

Current code:

var gulp = require('gulp'); var run = require('gulp-run'); module.exports = function() { run('c:/xxx/xxx/runme.bat').exec(); };

Solution as per @cmrn suggestion:

var exec = require('child_process').exec; var batchLocation = 'c:/xxx/xxx/runme.bat'; gulp.task('task', function (cb) { exec(batchLocation, function (err, stdout, stderr) { console.log(stdout); console.log(stderr); cb(err); }); })

最满意答案

如果您需要将脚本作为gulp流的一部分运行(即在pipe() ),您可以使用gulp-exec 。 如果没有,您可以使用下面复制的child_process.exec -exec README中描述的child_process.exec 。

var exec = require('child_process').exec;

gulp.task('task', function (cb) {
  exec('ping localhost', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})

If you need to run the script as part of a gulp stream (i.e. in pipe()) you can use gulp-exec. If not, you can just use child_process.exec as described in the gulp-exec README, copied below.

var exec = require('child_process').exec;

gulp.task('task', function (cb) {
  exec('ping localhost', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})

                    
                     
          

更多推荐

本文发布于:2023-04-05 21:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/5925fa3e847c891418ef50ca356b5f04.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   如何在   Gulp   file   run

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!