Javascript 根据对象数组中的键吞咽运行任务

Javascript 根据对象数组中的键吞咽运行任务,javascript,gulp,Javascript,Gulp,为了将文件复制到目的地,我使用SimpleGulp的src和dest 我想根据obj中的键指定此复制操作: var copy = { first: { dest: 'dist/index.scala.html', src: 'app/index.scala.html' }, second: { dest: 'dist/se

为了将文件复制到目的地,我使用SimpleGulp的src和dest

我想根据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'
            }
      };
我能够根据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
就像我们在grunt中实现的那样。

根据“不可能在该任务可以使用的命令行上传递参数”

也就是说,你可以这样做:

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();
    });
}
注意:我已将
copy=[
..
]
更改为
copy={
..
}
,因为数组不能将字符串(例如“first”)作为键,但对象可以

然后运行gulp命令:

gulpcopy:第一个

gulpcopy:第二个

你可能还想看看