module.exports = function (grunt) {
	"use strict";

	// 1. All configuration goes here 
	grunt.initConfig({
		pkg: grunt.file.readJSON('package.json'),
		imagemin: {
			dynamic: {
				files: [{
					expand: true,
					cwd: 'assets/images/rawSize/',
					src: ['*.{png,jpg,gif}', '**/*.{png,jpg,gif}'],
					dest: 'assets/images/'
				}]
			}
		},
		jshint: {
			// 定义用于检测的文件
			files: ['Gruntfile.js', 'assets/js/src/*.js'],
			options: {
				jshintrc: '.jshintrc',
				reporter: require('jshint-stylish')
			}
		},
		shell: {
			jekyll: {
				command: 'jekyll build --baseurl ""',
				stdout: true
			}
		},
		// js代码联合
		concat: {
			options: {
				//定义一个用于插入合并输出文件之间的字符
				separator: ';',
			},
			scripts: {
				src: ['assets/js/src/*.js'],
				dest: 'assets/js/build/<%= pkg.name %>.js'
			}
			// vendor: {
				// src: ['js/vendor/*.js'],
				// dest: 'js/build/vendor.js'
			// }
		},
		// js代码压缩
		uglify: {
			options: {
				//生成一个banner注释并插入到输出文件的顶部
				banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
			},
			scripts: {
				files: {
					'assets/js/build/<%= pkg.name %>.min.js': ['<%= concat.scripts.dest %>']
				}
			}
			// vendor: {
				// files: {
					// 'js/build/vendor.min.js': ['<%= concat.vendor.dest %>']
				// }
			// }
		},
		// css代码前缀补全
		autoprefixer: {
			options: {
				browsers: ['last 2 version', '> 5%', 'ie 8', 'ie 9']
			},
			// 没有提供dest将默认覆盖源文件
			multiple_files: {
				expand: true,
				flatten: true,
				src: ['assets/css/*.css', '!assets/css/*.min.css'],
				dest: 'assets/css/prefixer/'
			}
		},
		// css联合压缩,按顺序压缩
		cssmin: {
			min: {
				options: {
					banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
				},
				files: {
					'assets/css/<%= pkg.name %>.min.css': ['assets/css/prefixer/typo.css', 'assets/css/prefixer/font-awesome.css', 'assets/css/prefixer/main.css', 'assets/css/prefixer/highlight.css']
				}
			}
		},
		watch: {
			options: {
				livereload: true,
			},
			jekyllSources: {
				files: [
					'*.html', '*.yml', 'assets/js/**.js', 'assets/css/*.css', '!assets/css/*.min.css',
					'_posts/**', '_includes/**', '_drafts/**'
				],
				tasks: "shell:jekyll"
			},
			scripts: {
				files: [
					'<%= jshint.files %>'
				],
				tasks: ['jshint'],
				options: {
					spawn: false,
				},
			},
			css: {
				files: ['assets/css/*.css', '!assets/css/*.min.css'],
				tasks: ['autoprefixer', 'cssmin'],
				options: {
					spawn: false,
				},
			} 
		}
	});

	grunt.loadNpmTasks('grunt-contrib-imagemin');
	grunt.loadNpmTasks('grunt-contrib-jshint');
	grunt.loadNpmTasks('grunt-contrib-concat');
	grunt.loadNpmTasks('grunt-contrib-uglify');
	grunt.loadNpmTasks('grunt-shell');
	grunt.loadNpmTasks('grunt-autoprefixer');
	grunt.loadNpmTasks('grunt-contrib-cssmin');
	grunt.loadNpmTasks('grunt-contrib-watch');

	grunt.registerTask('build', ['jshint', 'concat', 'uglify', 'autoprefixer', 'cssmin', 'shell']);
	grunt.registerTask('default', ['jshint', 'concat', 'uglify', 'autoprefixer', 'cssmin', 'shell', 'watch']);
};
