0

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

sandrina-p
  • 3,794
  • 8
  • 32
  • 60
  • 1
    Possible duplicate of [Is it possible to pass a flag to Gulp to have it run tasks in different ways?](http://stackoverflow.com/questions/23023650/is-it-possible-to-pass-a-flag-to-gulp-to-have-it-run-tasks-in-different-ways) – Sven Schoenung Aug 24 '16 at 09:10
  • oww sorry i really tried to find something on stackoverflow. thanks! – sandrina-p Aug 24 '16 at 09:13

1 Answers1

1

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

Wilmer SH
  • 1,417
  • 12
  • 20