8
0
Pārlūkot izejas kodu

Added pre-commit git hook with jshint and jscs.

Szymon Kupś 10 gadi atpakaļ
vecāks
revīzija
77c08ae0f7
2 mainītis faili ar 41 papildinājumiem un 4 dzēšanām
  1. 39 4
      dev/tasks/gulp/lint/tasks.js
  2. 2 0
      package.json

+ 39 - 4
dev/tasks/gulp/lint/tasks.js

@@ -6,16 +6,37 @@ const gulp = require( 'gulp' );
 const jshint = require( 'gulp-jshint' );
 const jscs = require( 'gulp-jscs' );
 const fs = require( 'fs' );
+const guppy = require( 'git-guppy' )( gulp );
+const gulpFilter = require( 'gulp-filter' );
+const gutil = require( 'gulp-util' );
 
 module.exports = ( config ) => {
 	const src = [ '**/*.js' ].concat( config.IGNORED_FILES.map( i => '!' + i ), getGitIgnore() );
 
 	gulp.task( 'lint', () => {
 		return gulp.src( src )
-			.pipe( jscs() )
-			.pipe( jshint() )
-			.pipe( jscs.reporter() )
-			.pipe( jshint.reporter( 'jshint-reporter-jscs' ) );
+			.pipe( lint() );
+	} );
+
+	gulp.task( 'pre-commit', () => {
+		return guppy.stream( 'pre-commit' )
+			.pipe( gulpFilter( src ) )
+			.pipe( lint() )
+
+			// Error reporting for gulp.
+			.pipe( jscs.reporter( 'fail' ) )
+			.on( 'error', errorHandler )
+			.pipe( jshint.reporter( 'fail' ) )
+			.on( 'error', errorHandler );
+
+		/**
+		 * Handles error from jscs and jshint fail reporters. Stops node process with error code
+		 * and prints error message to the console.
+		 */
+		function errorHandler() {
+			gutil.log( gutil.colors.red( 'Linting failed, commit aborted' ) );
+			process.exit( 1 );
+		}
 	} );
 
 	/**
@@ -36,4 +57,18 @@ module.exports = ( config ) => {
 			// Add `!` for ignore glob.
 			.map( i => '!' + i );
 	}
+
+	/**
+	 * Returns stream with all linting plugins combined.
+	 * @returns { Stream }
+	 */
+	function lint() {
+		const stream = jscs();
+		stream
+			.pipe( jshint() )
+			.pipe( jscs.reporter() )
+			.pipe( jshint.reporter( 'jshint-reporter-jscs' ) );
+
+		return stream;
+	}
 };

+ 2 - 0
package.json

@@ -24,6 +24,7 @@
     "benderjs-sinon": "^0.3.0",
     "chai": "^3.4.0",
     "del": "^2.0.2",
+    "git-guppy": "^1.0.1",
     "grunt": "^0",
     "grunt-contrib-jshint": "^0",
     "grunt-githooks": "^0",
@@ -39,6 +40,7 @@
     "gulp-sourcemaps": "^1.6.0",
     "gulp-util": "^3.0.7",
     "gulp-watch": "^4.3.5",
+    "guppy-pre-commit": "^0.3.0",
     "inquirer": "^0.11.0",
     "istanbul": "^0.4.1",
     "jshint-reporter-jscs": "^0.1.0",