'use strict';

module.exports = function (grunt) {
  require('load-grunt-tasks')(grunt);

  grunt.initConfig({
    autoprefixer: {
      options: {
        browsers: ['> 1% in US']
      },

      build: {
        src: 'public/css/main.css'
      }
    },
    clean: {
      temp: ['.tmp'],
      dist: ['public']
    },
    copy: {
      main: {
        files: [
          {
            expand: true,
            cwd: 'app/',
            src: [
              '**',
              '!**/*.jade',
              '!**/*.{sass,scss}',
              '!**/*.js'
            ],
            dest: 'public/',
            filter: 'isFile'
          }
        ]
      }
    },

    concat: {
      iife: {
        options: {
          banner: ';(function(){',
          footer: '}());'
        },

        src: ['public/js/main.min.js'],
        dest: 'public/js/main.min.js'
      }
    },

    connect: {
      options: {
        port: 8888,
        open: true,
        useAvailablePort: true,
        hostname: 'localhost'
      },

      server: {
        options: {
          livereload: true,

          middleware: function (connect) {
            return [
              connect.static('public'),
              connect().use('/scripts', connect.static('./app/scripts')),
              connect().use('/bower_components', connect.static('./bower_components'))
            ];
          }
        }
      },
    },

    jade: {
      compile: {
        options: {
          pretty: true
        },
        files: [{expand: true, cwd: 'app/', src: ['**/*.jade', '!**/_*.jade'], dest: 'public/', ext: '.html'}]
      }
    },
    sass: {
      options: {
        sourceMap: true
      },
      dist: {
        files: {
          'public/css/main.css': 'app/styles/main.scss'
        }
      }
    },

    usemin: {
      html: ['public/**/*.html']
    },

    useminPrepare: {
      html: ['public/index.html'],

      options: {
        dest: 'public',
        root: 'app'
      }
    },

    watch: {
      bower: {
        files: ['bower.json'],
        tasks: ['wiredep']
      },

      livereload: {
        options: {
          livereload: true
        },

        files: [
          'public/**/*.html',
          'public/css/**/*.css',
          'public/js/**/*.js',
          'app/scripts/**/*.js'
        ]
      },

      other: {
        files: ['app/**', '!app/**/*.jade', '!app/**/*.{sass,scss}'],
        tasks: ['copy']
      },
      jade: {
        files: ['app/**/*.jade'],
        tasks: ['jade']
      },
      sass: {
        files: ['app/**/*.{sass,scss}'],
        tasks: ['sass', 'autoprefixer']
      }
    },

    wiredep: {
      build: {
        src: ['public/**/*.html']
      }
    }
  });

  grunt.registerTask('default', []);
  grunt.registerTask('build', [
    'clean',
    'copy',
    'jade',
    'sass',
    'autoprefixer',
    'wiredep',
    'combineJs'
  ]);
  grunt.registerTask('serve', ['build', 'connect', 'watch']);
  grunt.registerTask('combineJs', [
    'clean:temp',
    'wiredep',
    'useminPrepare',
    'concat:generated',
    'uglify:generated',
    'usemin',
    'concat:iife',
    'clean:temp'
  ]);
};

@@ -2775,96 +2775,16 @@
 ', %5B
-%0A    'clean',%0A    'copy',%0A    'jade',%0A    'sass',%0A    'autoprefixer',%0A    'wiredep',%0A   
+'setup',
  'co
@@ -2791,19 +2791,16 @@
 mbineJs'
-%0A  
 %5D);%0A  gr
@@ -2827,21 +2827,21 @@
 rve', %5B'
-build
+setup
 ', 'conn
@@ -2879,25 +2879,21 @@
 erTask('
-combineJs
+setup
 ', %5B%0A   
@@ -2903,30 +2903,122 @@
 lean
-:temp',%0A    'wiredep',
+',%0A    'copy',%0A    'jade',%0A    'sass',%0A    'autoprefixer',%0A    'wiredep'%0A  %5D);%0A  grunt.registerTask('combineJs', %5B
 %0A   

refactor grunt tasks