Skip to content

Instantly share code, notes, and snippets.

@EudesSilva
Last active May 24, 2017 21:43
Show Gist options
  • Select an option

  • Save EudesSilva/59d3ee00b866229e655dea510dd15c23 to your computer and use it in GitHub Desktop.

Select an option

Save EudesSilva/59d3ee00b866229e655dea510dd15c23 to your computer and use it in GitHub Desktop.

Revisions

  1. EudesSilva revised this gist May 24, 2017. 1 changed file with 152 additions and 4 deletions.
    156 changes: 152 additions & 4 deletions gulp-top-tasks.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,153 @@
    // source maps
    // npm install --save-dev gulp-sourcemaps

    # npm install --save-dev gulp-connect

    //LiveReload
    var gulp = require('gulp'),
    connect = require('gulp-connect');

    gulp.task('connect', function() {
    connect.server({
    root: 'app',
    livereload: true
    });
    });

    gulp.task('html', function () {
    gulp.src('./app/*.html')
    .pipe(connect.reload());
    });

    gulp.task('watch', function () {
    gulp.watch(['./app/*.html'], ['html']);
    });

    gulp.task('default', ['connect', 'watch']);


    //Start and stop server
    gulp.task('jenkins-tests', function() {
    connect.server({
    port: 8888
    });
    // run some headless tests with phantomjs
    // when process exits:
    connect.serverClose();
    });

    //Multiple server
    var gulp = require('gulp'),
    stylus = require('gulp-stylus'),
    connect = require('gulp-connect');

    gulp.task('connectDev', function () {
    connect.server({
    name: 'Dev App',
    root: ['app', 'tmp'],
    port: 8000,
    livereload: true
    });
    });

    gulp.task('connectDist', function () {
    connect.server({
    name: 'Dist App',
    root: 'dist',
    port: 8001,
    livereload: true
    });
    });

    gulp.task('html', function () {
    gulp.src('./app/*.html')
    .pipe(connect.reload());
    });

    gulp.task('stylus', function () {
    gulp.src('./app/stylus/*.styl')
    .pipe(stylus())
    .pipe(gulp.dest('./app/css'))
    .pipe(connect.reload());
    });

    gulp.task('watch', function () {
    gulp.watch(['./app/*.html'], ['html']);
    gulp.watch(['./app/stylus/*.styl'], ['stylus']);
    });

    gulp.task('default', ['connectDist', 'connectDev', 'watch']);









    # npm install --save-dev gulp-json2js

    var json2js = require('gulp-json2js');
    var rename = require("gulp-rename");
    var data = require('gulp-data');

    gulp.task('generateJS', function() {
    gulp.src('./data/localModel.js')
    .pipe(rename('local.js'))
    .pipe(data(function(file) {
    delete require.cache['./data/local.json'];
    return require('./data/local.json');
    }))
    .pipe(json2js())
    .pipe(gulp.dest('./data/'))
    });







    # npm install --save-dev gulp-imagemin

    const gulp = require('gulp');
    const imagemin = require('gulp-imagemin');

    gulp.task('default', () =>
    gulp.src('src/images/*')
    .pipe(imagemin())
    .pipe(gulp.dest('dist/images'))
    );

    //OR
    .pipe(imagemin([
    imagemin.gifsicle({interlaced: true}),
    imagemin.jpegtran({progressive: true}),
    imagemin.optipng({optimizationLevel: 5}),
    imagemin.svgo({plugins: [{removeViewBox: true}]})





    # npm install --save-dev gulp-zip

    const gulp = require('gulp');
    const zip = require('gulp-zip');

    gulp.task('default', () =>
    gulp.src('src/*')
    .pipe(zip('archive.zip'))
    .pipe(gulp.dest('dist'))
    );







    # npm install --save-dev gulp-sourcemaps

    var gulp = require('gulp');
    var concat = require('gulp-concat');
    var sourcemaps = require('gulp-sourcemaps');
    @@ -13,9 +161,9 @@ gulp.task('javascript', function() {
    });


    // concat
    // npm install --save-dev gulp-concat

    # npm install --save-dev gulp-concat

    // opt 1
    var concat = require('gulp-concat');
    gulp.task('scripts', function() {
  2. EudesSilva created this gist May 24, 2017.
    640 changes: 640 additions & 0 deletions gulp-top-tasks.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,640 @@
    // source maps
    // npm install --save-dev gulp-sourcemaps
    var gulp = require('gulp');
    var concat = require('gulp-concat');
    var sourcemaps = require('gulp-sourcemaps');

    gulp.task('javascript', function() {
    return gulp.src('src/**/*.js')
    .pipe(sourcemaps.init())
    .pipe(concat('all.js'))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('dist'));
    });


    // concat
    // npm install --save-dev gulp-concat

    // opt 1
    var concat = require('gulp-concat');
    gulp.task('scripts', function() {
    return gulp.src('./lib/*.js')
    .pipe(concat('all.js'))
    .pipe(gulp.dest('./dist/'));
    });


    // opt 2
    var concat = require('gulp-concat');

    gulp.task('scripts', function() {
    return gulp.src(['./lib/file3.js', './lib/file1.js', './lib/file2.js'])
    .pipe(concat('all.js'))
    .pipe(gulp.dest('./dist/'));
    });


    // opt 3
    var concat = require('gulp-concat');

    gulp.task('scripts', function() {
    return gulp.src(['./lib/file3.js', './lib/file1.js', './lib/file2.js'])
    .pipe(concat({ path: 'new.js', stat: { mode: 0666 }}))
    .pipe(gulp.dest('./dist'));
    });




    # npm install --save-dev gulp-nodemon

    var gulp = require('gulp')
    , nodemon = require('gulp-nodemon')
    , jshint = require('gulp-jshint')

    gulp.task('lint', function () {
    gulp.src('./**/*.js')
    .pipe(jshint())
    })

    gulp.task('develop', function () {
    var stream = nodemon({ script: 'server.js'
    , ext: 'html js'
    , ignore: ['ignored.js']
    , tasks: ['lint'] })

    stream
    .on('restart', function () {
    console.log('restarted!')
    })
    .on('crash', function() {
    console.error('Application has crashed!\n')
    stream.emit('restart', 10) // restart the server in 10 seconds
    })
    })



    # npm install --save-dev vinyl-elasticsearch

    var gulp = require('gulp');
    var elasticsearch = require('vinyl-elasticsearch');

    gulp.task('search', function() {
    return elasticsearch.src(
    {
    index: 'someindex',
    size: 0,
    body: {
    // Begin query.
    query: {
    // Boolean query for matching and excluding items.
    bool: {
    must: [{match: {'somefield': 'somevalue'}}]
    }
    },
    // Aggregate on the results
    aggs: {
    actions: {
    terms: {
    field: 'additionalType',
    order: {'_term' : 'asc'},
    size: 10,
    }
    }
    }
    }
    },
    {
    host: 'https://localhost:9200',
    log: 'trace'
    }
    )
    .pipe(echo());
    });





    # npm install --save-dev gulp-rename

    var rename = require("gulp-rename");

    // rename via string
    gulp.src("./src/main/text/hello.txt")
    .pipe(rename("main/text/ciao/goodbye.md"))
    .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/goodbye.md

    // rename via function
    gulp.src("./src/**/hello.txt")
    .pipe(rename(function (path) {
    path.dirname += "/ciao";
    path.basename += "-goodbye";
    path.extname = ".md"
    }))
    .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/hello-goodbye.md

    // rename via hash
    gulp.src("./src/main/text/hello.txt", { base: process.cwd() })
    .pipe(rename({
    dirname: "main/text/ciao",
    basename: "aloha",
    prefix: "bonjour-",
    suffix: "-hola",
    extname: ".md"
    }))
    .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/bonjour-aloha-hola.md






    # npm install --save-dev del

    const del = require('del');

    del(['tmp/*.js', '!tmp/unicorn.js']).then(paths => {
    console.log('Deleted files and folders:\n', paths.join('\n'));
    });

    // OR
    del.sync(['public/assets/**', '!public/assets/goat.png']);





    # npm install --save-dev gulp-uglify

    var gulp = require('gulp');
    var uglify = require('gulp-uglify');
    var pump = require('pump');

    gulp.task('compress', function (cb) {
    pump([
    gulp.src('lib/*.js'),
    uglify(),
    gulp.dest('dist')
    ],
    cb
    );
    });



    # npm install --save-dev gulp-if

    var gulpif = require('gulp-if');
    var uglify = require('gulp-uglify');

    var condition = true; // TODO: add business logic

    gulp.task('task', function() {
    gulp.src('./src/*.js')
    .pipe(gulpif(condition, uglify()))
    .pipe(gulp.dest('./dist/'));
    });

    // OR Ternary
    var gulpif = require('gulp-if');
    var uglify = require('gulp-uglify');
    var beautify = require('gulp-beautify');

    var condition = function (file) {
    // TODO: add business logic
    return true;
    }

    gulp.task('task', function() {
    gulp.src('./src/*.js')
    .pipe(gulpif(condition, uglify(), beautify()))
    .pipe(gulp.dest('./dist/'));
    });





    # npm install --save-dev gulp-git

    var gulp = require('gulp');
    var git = require('gulp-git');

    // Run git init
    // src is the root folder for git to initialize
    gulp.task('init', function(){
    git.init(function (err) {
    if (err) throw err;
    });
    });

    // Run git init with options
    gulp.task('init', function(){
    git.init({args: '--quiet --bare'}, function (err) {
    if (err) throw err;
    });
    });

    // Run git add
    // src is the file(s) to add (or ./*)
    gulp.task('add', function(){
    return gulp.src('./git-test/*')
    .pipe(git.add());
    });

    // Run git add with options
    gulp.task('add', function(){
    return gulp.src('./git-test/*')
    .pipe(git.add({args: '-f -i -p'}));
    });

    // Run git commit
    // src are the files to commit (or ./*)
    gulp.task('commit', function(){
    return gulp.src('./git-test/*')
    .pipe(git.commit('initial commit'));
    });

    // Run git commit with options
    gulp.task('commit', function(){
    return gulp.src('./git-test/*')
    .pipe(git.commit('initial commit', {args: '-A --amend -s'}));
    });

    // Run git commit without checking for a message using raw arguments
    gulp.task('commit', function(){
    return gulp.src('./git-test/*')
    .pipe(git.commit(undefined, {
    args: '-m "initial commit"',
    disableMessageRequirement: true
    }));
    });

    // Run git commit without appending a path to the commits
    gulp.task('commit', function(){
    return gulp.src('./git-test/*')
    .pipe(git.commit('initial commit', {
    disableAppendPaths: true
    }));
    });

    // Run git commit, passing multiple messages as if calling
    // git commit -m "initial commit" -m "additional message"
    gulp.task('commit', function(){
    return gulp.src('./git-test/*')
    .pipe(git.commit(['initial commit', 'additional message']));
    });

    // Run git commit, emiting 'data' event during progress
    // This is useful when you have long running githooks
    // and want to show progress to your users on screen
    gulp.task('commit', function(){
    return gulp.src('./git-test/*')
    .pipe(git.commit('initial commit', {emitData:true}))
    .on('data',function(data) {
    console.log(data);
    });
    });

    // Run git remote add
    // remote is the remote repo
    // repo is the https url of the repo
    gulp.task('addremote', function(){
    git.addRemote('origin', 'https://github.com/stevelacy/git-test', function (err) {
    if (err) throw err;
    });
    });

    // Run git remote remove
    // remote is the remote repo
    gulp.task('removeremote', function(){
    git.removeRemote('origin', function (err) {
    if (err) throw err;
    });
    });

    // Run git push
    // remote is the remote repo
    // branch is the remote branch to push to
    gulp.task('push', function(){
    git.push('origin', 'master', function (err) {
    if (err) throw err;
    });
    });

    // Run git push with options
    // branch is the remote branch to push to
    gulp.task('push', function(){
    git.push('origin', 'master', {args: " -f"}, function (err) {
    if (err) throw err;
    });
    });

    // Run git push with multiple branches and tags
    gulp.task('push', function(){
    git.push('origin', ['master', 'develop'], {args: " --tags"}, function (err) {
    if (err) throw err;
    });
    });

    // Run git pull
    // remote is the remote repo
    // branch is the remote branch to pull from
    gulp.task('pull', function(){
    git.pull('origin', 'master', {args: '--rebase'}, function (err) {
    if (err) throw err;
    });
    });

    // Run git pull from multiple branches
    gulp.task('pull', function(){
    git.pull('origin', ['master', 'develop'], function (err) {
    if (err) throw err;
    });
    });

    // Run git fetch
    // Fetch refs from all remotes
    gulp.task('fetch', function(){
    git.fetch('', '', {args: '--all'}, function (err) {
    if (err) throw err;
    });
    });

    // Run git fetch
    // Fetch refs from origin
    gulp.task('fetch', function(){
    git.fetch('origin', '', function (err) {
    if (err) throw err;
    });
    });

    // Clone a remote repo
    gulp.task('clone', function(){
    git.clone('https://github.com/stevelacy/gulp-git', function (err) {
    if (err) throw err;
    });
    });

    // Clone remote repo to sub folder ($CWD/sub/folder/git-test)
    gulp.task('clonesub', function() {
    git.clone('https://github.com/stevelacy/git-test', {args: './sub/folder'}, function(err) {
    // handle err
    });
    });

    // Tag the repo with a version
    gulp.task('tag', function(){
    git.tag('v1.1.1', 'Version message', function (err) {
    if (err) throw err;
    });
    });

    // Tag the repo with a version and empty message
    gulp.task('tag', function(){
    git.tag('v1.1.1', '', function (err) {
    if (err) throw err;
    });
    });

    // Tag the repo With signed key
    gulp.task('tagsec', function(){
    git.tag('v1.1.1', 'Version message with signed key', {signed: true}, function (err) {
    if (err) throw err;
    });
    });

    // Create a git branch
    gulp.task('branch', function(){
    git.branch('newBranch', function (err) {
    if (err) throw err;
    });
    });

    // Checkout a git branch
    gulp.task('checkout', function(){
    git.checkout('branchName', function (err) {
    if (err) throw err;
    });
    });

    // Create and switch to a git branch
    gulp.task('checkout', function(){
    git.checkout('branchName', {args:'-b'}, function (err) {
    if (err) throw err;
    });
    });

    // Merge branches to master
    gulp.task('merge', function(){
    git.merge('branchName', function (err) {
    if (err) throw err;
    });
    });

    // Reset a commit
    gulp.task('reset', function(){
    git.reset('SHA', function (err) {
    if (err) throw err;
    });
    });

    // Git rm a file or folder
    gulp.task('rm', function(){
    return gulp.src('./gruntfile.js')
    .pipe(git.rm());
    });

    gulp.task('addSubmodule', function(){
    git.addSubmodule('https://github.com/stevelacy/git-test', 'git-test', { args: '-b master'});
    });

    gulp.task('updateSubmodules', function(){
    git.updateSubmodule({ args: '--init' });
    });

    // Working tree status
    gulp.task('status', function(){
    git.status({args: '--porcelain'}, function (err, stdout) {
    if (err) throw err;
    });
    });

    // Other actions that do not require a Vinyl
    gulp.task('log', function(){
    git.exec({args : 'log --follow index.js'}, function (err, stdout) {
    if (err) throw err;
    });
    });

    // Git clean files
    gulp.task('clean', function() {
    git.clean({ args: '-f' }, function (err) {
    if(err) throw err;
    });
    });

    // Run gulp's default task
    gulp.task('default',['add']);






    # npm install --save-dev yargs

    // short.js
    var argv = require('yargs').argv;
    console.log('(%d,%d)', argv.x, argv.y);

    //call
    // $ ./short.js -x 10 -y 21
    //(10,21)




    # npm install --save-dev gulp-size

    const gulp = require('gulp');
    const size = require('gulp-size');

    gulp.task('default', () =>
    gulp.src('fixture.js')
    .pipe(size())
    .pipe(gulp.dest('dist'))
    );








    # npm install --save-dev gulp-notify

    gulp.task("one", function () {
    return gulp.src("../test/fixtures/1.txt")
    .pipe(notify());
    });

    notify.on('click', function (options) {
    console.log('I clicked something!', options);
    });

    gulp.task("click", function () {
    // Click event only makes sense if wait is true...
    return gulp.src("../test/fixtures/1.txt")
    .pipe(notify({ message: 'Hello 1', wait: true }));
    });

    gulp.task("message", function () {
    return gulp.src("../test/fixtures/1.txt")
    .pipe(notify("This is a message."));
    });

    gulp.task("advanced", function () {
    return gulp.src("../test/fixtures/*")
    .pipe(notify({
    "title": "Open Github",
    "subtitle": "Project web site",
    "message": "Click to open project site",
    "sound": "Frog", // case sensitive
    "icon": path.join(__dirname, "gulp.png"), // case sensitive
    "onLast": true,
    "wait": true
    }));
    });

    gulp.task("error", function () {
    return gulp.src("../test/fixtures/*")
    .pipe(through.obj(function (file, enc, callback) {
    this.emit("error", new Error("Something happend: Error message!"));
    callback();
    }))
    .on("error", notify.onError({
    message: 'Error: <%= error.message %>',
    sound: false // deactivate sound?
    }))
    .on("error", function (err) {
    console.log("Error:", err);
    })
    });

    gulp.task("forceGrowl", function () {
    var custom = notify.withReporter(function (options, callback) {
    new nn.Growl().notify(options, callback);
    });