sandrina-p
sandrina-p

Reputation: 4160

gulp - command-line to run a task on a specific file

I have a task scripts that do all that normal stuff with scripts on watch:

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; } ));
});

Sometimes, I may need to run that task scripts only for a specific file outside that folder, ex: assets/plugins/flexsider/flexslider.js I'm wondering if it would be possible to do something like this on terminal:

gulp scripts assets/plugins/flexsider/flexslider.js

and then the task scripts would replace gulp.src() content for a dynamic content, this case assets/plugins/flexsider/flexslider.js, and would be like this:

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; } ));
});

I searched for gulp-exec and similares but I think it is not what i'm looking for.

Thanks

Upvotes: 0

Views: 12478

Answers (1)

Wilmer SH
Wilmer SH

Reputation: 1417

Install yargs

npm install yargs --save-dev

Your task:

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; } ));
});

Call your task from the prompt:

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

which will run scripts on that file only.

or

gulp scripts 

which will run scripts as before

Upvotes: 1

Related Questions