浏览代码

Moved from Grunt to Gulp: gulp lint, gulp lodash tasks introduced.

Szymon Kupś 10 年之前
父节点
当前提交
c8a9c4cc75

+ 0 - 1
packages/ckeditor5-engine/.gitignore

@@ -8,4 +8,3 @@
 .*/**
 
 node_modules/**
-build/**

+ 0 - 13
packages/ckeditor5-engine/dev/tasks/githooks.js

@@ -1,13 +0,0 @@
-'use strict';
-
-module.exports = function( grunt ) {
-	grunt.config.merge( {
-		githooks: {
-			all: {
-				'pre-commit': 'default'
-			}
-		}
-	} );
-
-	grunt.loadNpmTasks( 'grunt-githooks' );
-};

+ 0 - 26
packages/ckeditor5-engine/dev/tasks/jscs.js

@@ -1,26 +0,0 @@
-'use strict';
-
-const tools = require( './utils/tools' );
-
-module.exports = ( grunt ) => {
-	tools.setupMultitaskConfig( grunt, {
-		task: 'jscs',
-		defaultOptions: {
-				config: true
-			},
-		addGitIgnore: 'excludeFiles',
-		targets: {
-			all() {
-				return [ '**/*.js' ];
-			},
-
-			git() {
-				return tools.getGitDirtyFiles().filter( function( file ) {
-					return ( /\.js$/ ).test( file );
-				} );
-			}
-		}
-	} );
-
-	grunt.loadNpmTasks( 'grunt-jscs' );
-};

+ 0 - 26
packages/ckeditor5-engine/dev/tasks/jshint.js

@@ -1,26 +0,0 @@
-'use strict';
-
-const tools = require( './utils/tools' );
-
-module.exports = ( grunt ) => {
-	tools.setupMultitaskConfig( grunt, {
-		task: 'jshint',
-		defaultOptions: {
-				jshintrc: true
-			},
-		addGitIgnore: 'ignores',
-		targets: {
-			all() {
-				return [ '**/*.js' ];
-			},
-
-			git() {
-				return tools.getGitDirtyFiles().filter( function( file ) {
-					return ( /\.js$/ ).test( file );
-				} );
-			}
-		}
-	} );
-
-	grunt.loadNpmTasks( 'grunt-contrib-jshint' );
-};

+ 91 - 0
packages/ckeditor5-engine/dev/tasks/lint/tasks.js

@@ -0,0 +1,91 @@
+/* jshint node: true, esnext: true */
+
+'use strict';
+
+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() );
+
+	const tasks = {
+		/**
+		 * Returns stream containing jshint and jscs reporters.
+		 *
+		 * @returns { Stream }
+		 */
+		lint() {
+			return gulp.src( src )
+				.pipe( lint() );
+		},
+
+		/**
+		 * This method is executed on pre-commit hook, linting only files staged for current commit.
+		 *
+		 * @returns { Stream }
+		 */
+		lintStaged() {
+			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 );
+			}
+		}
+	};
+
+	gulp.task( 'lint', tasks.lint );
+	gulp.task( 'lint-staged', tasks.lintStaged );
+
+	return tasks;
+
+	/**
+	 * Gets the list of ignores from `.gitignore`.
+	 *
+	 * @returns {String[]} The list of ignores.
+	 */
+	function getGitIgnore( ) {
+		let gitIgnoredFiles = fs.readFileSync( '.gitignore', 'utf8' );
+
+		return gitIgnoredFiles
+			// Remove comment lines.
+			.replace( /^#.*$/gm, '' )
+			// Transform into array.
+			.split( /\n+/ )
+			// Remove empty entries.
+			.filter( ( path ) => !!path )
+			// Add `!` for ignore glob.
+			.map( i => '!' + i );
+	}
+
+	/**
+	 * Returns stream with all linting plugins combined.
+	 * @returns { Stream }
+	 */
+	function lint() {
+		const stream = jshint();
+		stream
+			.pipe( jscs() )
+			.pipe( jscs.reporter() )
+			.pipe( jshint.reporter( 'jshint-reporter-jscs' ) );
+
+		return stream;
+	}
+};

+ 0 - 7
packages/ckeditor5-engine/dev/tasks/lodash.js

@@ -1,7 +0,0 @@
-/* jshint node: true */
-
-'use strict';
-
-module.exports = function( grunt ) {
-	grunt.loadNpmTasks( 'grunt-lodash' );
-};

+ 26 - 0
packages/ckeditor5-engine/dev/tasks/lodash/tasks.js

@@ -0,0 +1,26 @@
+/* jshint node: true, esnext: true */
+
+'use strict';
+
+const gulp = require( 'gulp' );
+const build = require( 'lodash-cli' );
+
+module.exports = function() {
+	const tasks = {
+		lodash( done ) {
+			build( [
+				'modularize',
+				'modern',
+				'exports=es',
+				'include=clone,extend,isPlainObject,isObject,isArray,last,isEqual',
+				'--development',
+				'--output', 'src/lib/lodash'
+			], ( e ) => {
+				done( e instanceof Error ? e : null );
+			} );
+		}
+	};
+	gulp.task( 'lodash', tasks.lodash );
+
+	return tasks;
+};

+ 0 - 140
packages/ckeditor5-engine/dev/tasks/utils/tools.js

@@ -1,140 +0,0 @@
-'use strict';
-
-let dirtyFiles,
-	ignoreList;
-
-module.exports = {
-	/**
-	 * Check if a task (including its optional target) is in the queue of tasks to be executed by Grunt.
-	 *
-	 * @param grunt {Object} The Grunt object.
-	 * @param task {String} The task name. May optionally include the target (e.g. 'task:target').
-	 * @returns {Boolean} "true" if the task is in the queue.
-	 */
-	checkTaskInQueue( grunt, task ) {
-		const cliTasks = grunt.cli.tasks;
-
-		// Check if the task has been called directly.
-		const isDirectCall = ( cliTasks.indexOf( task ) > -1 );
-		// Check if this is a "default" call and that the task is inside "default".
-		const isDefaultTask = ( cliTasks.indexOf( 'default' ) > -1 ) || !cliTasks.length;
-		// Hacking Grunt hard.
-		const isTaskInDefault = isDefaultTask && ( grunt.task._tasks.default.info.indexOf( '"' + task + '"' ) > -1 );
-
-		return isDirectCall || isTaskInDefault;
-	},
-
-	/**
-	 * Configures a multi-task and defines targets that are queued to be run by Grunt.
-	 *
-	 * @param grunt {Object} The Grunt object.
-	 * @param options {Object} A list of options for the method. See the jscs and jshint tasks for example.
-	 */
-	setupMultitaskConfig( grunt, options ) {
-		const task = options.task;
-		const taskConfig = {};
-		const config = taskConfig[ task ] = {
-			options: options.defaultOptions
-		};
-
-		// "all" is the default target to be used if others are not to be run.
-		const all = options.targets.all;
-		let isAll = true;
-
-		delete options.targets.all;
-
-		Object.getOwnPropertyNames( options.targets ).forEach( function( target ) {
-			if ( this.checkTaskInQueue( grunt, task + ':' + target ) ) {
-				config[ target ] = options.targets[ target ]();
-				isAll = false;
-			}
-		}, this );
-
-		if ( isAll ) {
-			config.all = all();
-		}
-
-		// Append .gitignore entries to the ignore list.
-		if ( options.addGitIgnore ) {
-			let ignoreProp = task + '.options.' + options.addGitIgnore;
-			let ignores = grunt.config.get( ignoreProp ) || [];
-
-			ignores = ignores.concat( this.getGitIgnore( grunt ) );
-			grunt.config.set( ignoreProp, ignores );
-		}
-
-		// Merge over configurations set in gruntfile.js.
-		grunt.config.merge( taskConfig );
-	},
-
-	/**
-	 * Gets the list of ignores from `.gitignore`.
-	 *
-	 * @param grunt {Object} The Grunt object.
-	 * @returns {String[]} The list of ignores.
-	 */
-	getGitIgnore( grunt ) {
-		if ( !ignoreList ) {
-			ignoreList = grunt.file.read( '.gitignore' );
-
-			ignoreList = ignoreList
-				// Remove comment lines.
-				.replace( /^#.*$/gm, '' )
-				// Transform into array.
-				.split( /\n+/ )
-				// Remove empty entries.
-				.filter( function( path ) {
-					return !!path;
-				} );
-		}
-
-		return ignoreList;
-	},
-
-	/**
-	 * Gets the list of files that are supposed to be included in the next Git commit.
-	 *
-	 * @returns {String[]} A list of file paths.
-	 */
-	getGitDirtyFiles() {
-		// Cache it, so it is executed only once when running multiple tasks.
-		if ( !dirtyFiles ) {
-			dirtyFiles = this
-				// Compare the state of index with HEAD.
-				.shExec( 'git diff-index --name-only HEAD' )
-				// Remove trailing /n to avoid an empty entry.
-				.replace( /\s*$/, '' )
-				// Transform into array.
-				.split( '\n' );
-
-			// If nothing is returned, the array will one one empty string only.
-			if ( dirtyFiles.length == 1 && !dirtyFiles[ 0 ] ) {
-				dirtyFiles = [];
-			}
-		}
-
-		return dirtyFiles;
-	},
-
-	/**
-	 * Executes a shell command.
-	 *
-	 * @param command {String} The command to be executed.
-	 * @returns {String} The command output.
-	 */
-	shExec( command ) {
-		const sh = require( 'shelljs' );
-		sh.config.silent = true;
-
-		const ret = sh.exec( command );
-
-		if ( ret.code ) {
-			throw new Error(
-				'Error while executing `' + command + '`:\n\n' +
-				ret.output
-			);
-		}
-
-		return ret.output;
-	}
-};

+ 0 - 57
packages/ckeditor5-engine/gruntfile.js

@@ -1,57 +0,0 @@
-/* jshint node: true, esnext: true, varstmt: true */
-
-'use strict';
-
-module.exports = ( grunt ) => {
-	// First register the "default" task, so it can be analyzed by other tasks.
-	grunt.registerTask( 'default', [ 'jshint:git', 'jscs:git' ] );
-
-	// Files that will be ignored by the "jscs" and "jshint" tasks.
-	const ignoreFiles = [
-		'src/lib/**',
-		// Automatically loaded from .gitignore. Add more if necessary.
-	];
-
-	// Basic configuration which will be overloaded by the tasks.
-	grunt.initConfig( {
-		pkg: grunt.file.readJSON( 'package.json' ),
-
-		lodash: {
-			build: {
-				dest: 'src/lib/lodash',
-				options: {
-					modifier: 'modern',
-					modularize: true,
-					exports: 'es',
-					flags: [
-						'development'
-					],
-					include: [
-						'clone',
-						'extend',
-						'isPlainObject',
-						'isObject',
-						'isArray',
-						'last',
-						'isEqual'
-					]
-				}
-			}
-		},
-
-		jshint: {
-			options: {
-				ignores: ignoreFiles
-			}
-		},
-
-		jscs: {
-			options: {
-				excludeFiles: ignoreFiles
-			}
-		}
-	} );
-
-	// Finally load the tasks.
-	grunt.loadTasks( 'dev/tasks' );
-};

+ 18 - 0
packages/ckeditor5-engine/gulpfile.js

@@ -0,0 +1,18 @@
+/* jshint node: true, esnext: true */
+
+'use strict';
+
+const gulp = require( 'gulp' );
+
+const config = {
+	ROOT_DIR: '.',
+	WORKSPACE_DIR: '..',
+
+	// Files ignored by jshit and jscs tasks. Files from .gitignore will be added automatically during tasks execution.
+	IGNORED_FILES: [ 'src/lib/**' ]
+};
+
+require( './dev/tasks/lint/tasks' )( config );
+require( './dev/tasks/lodash/tasks' )();
+
+gulp.task( 'pre-commit', [ 'lint-staged' ] );

+ 9 - 7
packages/ckeditor5-engine/package.json

@@ -7,13 +7,15 @@
   ],
   "dependencies": {},
   "devDependencies": {
-    "grunt": "^0",
-    "grunt-jscs": "^2.0.0",
-    "grunt-contrib-jshint": "^0",
-    "grunt-githooks": "^0",
-    "shelljs": "^0",
-    "lodash-cli": "^3",
-    "grunt-lodash": "^0.5.0"
+    "git-guppy": "^1.0.1",
+    "gulp": "^3.9.0",
+    "gulp-filter": "^3.0.1",
+    "gulp-jscs": "^3.0.2",
+    "gulp-jshint": "^2.0.0",
+    "guppy-pre-commit": "^0.3.0",
+    "jshint": "^2.9.1",
+    "jshint-reporter-jscs": "^0.1.0",
+    "lodash-cli": "^3"
   },
   "author": "CKSource (http://cksource.com/)",
   "license": "See LICENSE.md",