【问题标题】:gulp - command-line to run a task on a specific filegulp - 在特定文件上运行任务的命令行
【发布时间】:2016-12-31 07:31:58
【问题描述】:

我有一个任务scriptswatch 上的脚本完成所有正常工作:

var folderScripts = "assets/scripts";

gulp.task('scripts', function(){
        gulp.src([
                folderScripts+'/**/*.js',
                '!'+folderScripts+'/**/_*.js',
                '!'+folderScripts+'/**/*.min.js'
            ])

            // uglify, rename, etc etc... 
            .pipe(gulp.dest( function(file) { return file.base; } ));
});

有时,我可能只需要为该文件夹之外的特定文件运行该任务scripts,例如:assets/plugins/flexsider/flexslider.js 我想知道是否可以在终端上执行类似的操作:

gulp scripts assets/plugins/flexsider/flexslider.js

然后任务脚本会将gulp.src() 内容替换为动态内容,本例为assets/plugins/flexsider/flexslider.js,如下所示:

var folderScripts = "assets/scripts";

gulp.task('scripts', function(){
        gulp.src('assets/plugins/flexsider/flexslider.js') //this would be the "flag" I passed on terminal line
            // uglify, rename, etc etc... 
            .pipe(gulp.dest( function(file) { return file.base; } ));
});

我搜索了 gulp-exec 和类似内容,但我认为这不是我想要的。

谢谢

【问题讨论】:

标签: node.js command-line terminal gulp


【解决方案1】:

安装yargs

npm install yargs --save-dev

你的任务:

gulp.task('scripts', function(){

    // require yargs
    var args = require('yargs').argv;

    var arraySources = [
        folderScripts+'/**/*.js',
        '!'+folderScripts+'/**/_*.js',
        '!'+folderScripts+'/**/*.min.js'];

    // check for an argument called file (or whatever you want)
    // and set the source to the file if the argument exists. Otherwise set it to the array
    var source = args.file? args.file : arraySources;

    gulp.src(source)

        // uglify, rename, etc etc... 
        .pipe(gulp.dest( function(file) { return file.base; } ));
});

根据提示调用您的任务:

gulp scripts --file=assets/plugins/flexsider/flexslider.js

仅在该文件上运行脚本。

gulp scripts 

它将像以前一样运行脚本

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2020-02-02
    • 2017-03-13
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多