根据对象数组中的键来执行任务

问题描述 投票:0回答:1

用于将文件复制到目标位置,我正在使用简单的gulp的src和dest

我想根据数组中的键指定此复制操作:

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

我能够根据数组中提到的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

就像我们如何在咕unt咕achieve中取得成就。

javascript gulp
1个回答
0
投票
根据this article“无法在该任务可以使用的命令行上传递参数”。

也就是说,您可以执行以下操作:

const gulp = require("gulp"); const obj = { first: { dest: 'dist/index.scala.html', src: 'app/index.scala.html' }, second: { dest: 'dist/setup.scala.html', src: 'app/setup.scala.html' } }; for (let i in obj) { gulp.task('copy:' + i, cb => { console.log('ary[i]: ', obj[i]); cb(); }); }

注意:我将copy = [ ... ]更改为obj = { ... },因为数组不能像键一样具有字符串(例如'first'),但是对象可以。

然后运行gulp命令:

gulp copy:first

gulp copy:second

© www.soinside.com 2019 - 2024. All rights reserved.