【问题标题】:How do I open a new terminal and run a command with Gulp.js如何打开一个新终端并使用 Gulp.js 运行命令
【发布时间】:2018-02-20 22:35:07
【问题描述】:

我正在使用 gulp.js 运行一些任务

我需要 gulp 做的是在新终端中运行一些命令 例如:

cd <directory_name> yarn start

或 运行其他命令,如 yarn install 或 build

以上必须在新终端中完成。

当我运行第一个任务时,它将打开一个新终端并运行子目录中的服务。第二个运行前端项目,因此需要打开另一个新终端并运行命令。

我在 stackOverflow 上查看了这两个问题,但仍然没有得到我需要的东西。

【问题讨论】:

    标签: javascript node.js gulp


    【解决方案1】:

    如果您尝试在 Gulp 中运行 Unix shell 命令,我建议您检查 ShellJsgulp-shell。如果您特别希望在 GUI 中打开一个新终端,您可以尝试运行以下命令之一(取决于您的操作系统):

    gnome-terminal -e command
    

    xterm -e command
    

    konsole -e command
    

    差不多:

    terminal -e command
    

    来源:https://askubuntu.com/questions/46627/

    ... 来自 gulp 任务的 ShellJs 或 gulp-shell。

    【讨论】:

      【解决方案2】:

      您是否尝试过此解决方案?

      var exec = require('child_process').exec;
      
      gulp.task('node exec', function (cb) {
        exec('cd some/where && yarn start', (err, stdout, stderr) => {
          console.log(stdout);
          console.log(stderr);
          cb(err);
        });
      });
      

      Spawns a shell then executes the command within that shell 根据 node.js 文档,完全按照您的意愿行事:https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

      【讨论】:

      • 感谢您的回答,它会运行命令,这是我需要做的一步,但它不会打开新终端,所以如果我正在运行服务器,他需要自己的终端和前端项目的另一个终端。
      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多