简体   繁体   English

Gulp 根据 object 数组中的键运行任务

[英]Gulp run task as per key in array of object

For copying files to destination I am using simple gulp's src and dest为了将文件复制到目的地,我使用简单的 gulp 的 src 和 dest

I want to specify this copy action as per key in obj:我想根据 obj 中的键指定此复制操作:

    var copy = {
            first: {
                dest: 'dist/index.scala.html',
                src: 'app/index.scala.html'
            },
            second: {
                dest: 'dist/setup.scala.html',
                src: 'app/setup.scala.html'
            }
      };

I am able to create copy task and copy files as per src and dest mentioned in object But I need something like:我可以根据 object 中提到的 src 和 dest 创建复制任务和复制文件,但我需要类似的东西:

    gulp copy:first //this will only copy from src to dest as specifided under 'first' key

    gulp copy:second //this will only copy from src to dest as specifided under 'second' key

Like how we achieve in grunt.就像我们在咕噜声中实现的一样。

According to this article "it's not possible to pass arguments on the command line which can be used by that task".根据这篇文章“不可能在该任务可以使用的命令行上传递 arguments”。

That said, you can do something like this:也就是说,您可以执行以下操作:

const gulp = require("gulp");

const copy = {
    first: {
        dest: 'dist/index.scala.html',
        src: 'app/index.scala.html'
    },
    second: {
        dest: 'dist/setup.scala.html',
        src: 'app/setup.scala.html'
    }
};

for (let key in copy) {
    gulp.task('copy:' + key, cb => {
        console.log('copy[key]: ', copy[key]);
        cb();
    });
}

Note: I've changed copy = [ ... ] to copy = { ... } because arrays can't have strings (such as 'first') as keys but objects can.注意:我已将copy = [ ... ]更改为copy = { ... }因为 arrays 不能将字符串(例如“first”)作为键,但对象可以。

Then to run the gulp commands:然后运行 gulp 命令:

gulp copy:first

gulp copy:second

You may also want to have a look at Pass Parameter to Gulp Task .您可能还想查看Pass Parameter to Gulp Task

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM