如何在gruntjs任务中运行MULTIPLE shell命令?

Rai*_*ere 11 node.js gruntjs

我目前使用grunt-shellgrunt任务运行shell命令.有没有更好的方法在一个任务中运行多个命令,而不是用'&&'将它们串在一起?

我的Gruntfile(部分):

grunt.initConfig({
  shell: {
    deploy: {
      options: { stdout: true },
      command: 'mkdir -p static/styles && cp public/styles/main.css static/styles'
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

一系列命令不起作用,但它会很好:

grunt.initConfig({
  shell: {
    deploy: {
      options: { stdout: true },
      command: [
        'mkdir -p static/styles',
        'cp public/styles/main.css static/styles'
      ]
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

Sin*_*hus 15

你可以把它们加在一起:

grunt.initConfig({
  shell: {
    deploy: {
      options: { stdout: true },
      command: [
        'mkdir -p static/styles',
        'cp public/styles/main.css static/styles'
      ].join('&&')
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

我选择不支持数组的原因是有些人可能想要;作为分隔符而不是&&,这样可以更容易地完成上述操作.

  • 使用`&&`导致它只在前一个命令成功时执行以下命令.使用`;`意味着它将继续执行命令.我用一个例子更新了文档.你不是第一个问这个:) (3认同)