8
0
Quellcode durchsuchen

Added grunt plugin:install task. Code refractoring.

Szymon Kupś vor 10 Jahren
Ursprung
Commit
37b7134043
6 geänderte Dateien mit 302 neuen und 177 gelöschten Zeilen
  1. 9 176
      dev/tasks/plugin.js
  2. 112 0
      dev/tasks/plugin/common.js
  3. 68 0
      dev/tasks/plugin/create.js
  4. 81 0
      dev/tasks/plugin/inquiries.js
  5. 31 0
      dev/tasks/plugin/install.js
  6. 1 1
      gruntfile.js

+ 9 - 176
dev/tasks/plugin.js

@@ -1,187 +1,20 @@
 'use strict';
-var path = require( 'path' );
-var tools = require( './utils/tools' );
-var inquirer = require( 'inquirer' );
-var fs = require( 'fs' );
-var grunt;
 
-module.exports = function( g ) {
-	grunt = g;
+var createPlugin = require( './plugin/create' );
+var installPlugin = require( './plugin/install' );
+
+module.exports = function( grunt ) {
 	grunt.registerTask( 'plugin', function( target ) {
 		var done = this.async();
 
 		switch ( target ) {
 			case 'create':
-				createNewPlugin( done );
+				createPlugin( grunt, done );
+				break;
+
+			case 'install':
+				installPlugin( grunt, done );
 				break;
 		}
 	} );
 };
-
-// Logs message using grunt.log.writeln.
-function log( message ) {
-	grunt.log.writeln( message );
-}
-
-function createNewPlugin( done ) {
-	var info = { };
-
-	askForPluginName( info )
-		.then( askForRepositoryLocation )
-		.then( askForPluginVersion )
-		.then( askForGitHubUrl )
-		.then( createRepository )
-		.then( updatePluginPackageJSON )
-		.then( updatePackageJson )
-		.then( linkNPM )
-		.then( function() {
-			done();
-		} ).catch( function( e ) {
-			done( e );
-		} );
-}
-
-// Links new plugin directory using npm link.
-function linkNPM( info ) {
-	// Don't use sudo on windows when executing npm link.
-	var isWin = process.platform == 'win32';
-	log( 'Linking plugin using npm link...' );
-
-	return new Promise( function( resolve ) {
-		tools.shExec( ( !isWin ? 'sudo ' : '' ) + 'npm link ' + info.repositoryLocation );
-		resolve( info );
-	} );
-}
-
-// Updates project's package.json by adding GitHub URL to dependencies.
-function updatePackageJson( info ) {
-	return new Promise( function( resolve ) {
-		changePackageJson( path.join( process.cwd(), 'package.json' ), function( json ) {
-			json.dependencies[ info.pluginName ] = info.gitHubUrl;
-
-			return json;
-		} ).then( function() {
-			resolve( info );
-		} );
-	} );
-}
-
-// Updates plugin package.json by adding name of the plugin and version.
-function updatePluginPackageJSON( info ) {
-	return new Promise( function( resolve ) {
-		var packageJSONPath = path.join( info.repositoryLocation, 'package.json' );
-		changePackageJson( packageJSONPath, function( json ) {
-			json.name = info.pluginName;
-			json.version = info.version;
-
-			return json;
-		} ).then( function() {
-			resolve( info );
-		} );
-	} );
-}
-
-// Initializes git repository and merges ckeditor5 boilerplate into master branch.
-function createRepository( info ) {
-	log( 'Initializing plugin repository...' );
-
-	return new Promise( function( resolve ) {
-		var repositoryLocation = info.repositoryLocation;
-		var initializeRepoCommands = [
-			'git init ' + repositoryLocation,
-			'cd ' + repositoryLocation,
-			'git remote add boilerplate git@github.com:ckeditor/ckeditor-boilerplate.git',
-			'git fetch boilerplate ckeditor5',
-			'git merge boilerplate/ckeditor5'
-		];
-
-		tools.shExec( initializeRepoCommands.join( ' && ' ) );
-		resolve( info );
-	} );
-}
-
-// Ask for new plugin name.
-function askForPluginName( info ) {
-	return new Promise( function( resolve ) {
-		inquirer.prompt( [ {
-			name: 'pluginName',
-			message: 'Enter plugin name (without -ckeditor5-plugin postfix):',
-			validate: function( input ) {
-				var regexp = /^[a-zA-Z0-9-_]+$/;
-
-				return regexp.test( input ) ? true : 'Please provide a valid plugin name.';
-			}
-		} ], function( answers ) {
-			info.pluginName = answers.pluginName + '-ckeditor5-plugin';
-			resolve( info );
-		} );
-	} );
-}
-
-// Ask for the location of the repository.
-function askForRepositoryLocation( info ) {
-	var def = path.join( process.cwd(), '..', info.pluginName );
-
-	return new Promise( function( resolve ) {
-		inquirer.prompt( [ {
-			name: 'path',
-			message: 'Enter repository location:',
-			default: def
-		} ], function( answers ) {
-			info.repositoryLocation = answers.path;
-			resolve( info );
-		} );
-	} );
-}
-
-// Ask for initial version of the plugin.
-function askForPluginVersion( info ) {
-	return new Promise( function( resolve ) {
-		inquirer.prompt( [ {
-			name: 'version',
-			message: 'Enter plugin\'s initial version:',
-			default: '0.0.1'
-		} ], function( answers ) {
-			info.version = answers.version;
-			resolve( info );
-		} );
-	} );
-}
-
-// Ask for GitHub Url.
-function askForGitHubUrl( info ) {
-	return new Promise( function( resolve ) {
-		inquirer.prompt( [ {
-			name: 'gitHubUrl',
-			message: 'Enter plugin\'s GitHub URL:',
-			default: 'ckeditor/' + info.pluginName
-		} ], function( answers ) {
-			info.gitHubUrl = answers.gitHubUrl;
-			resolve( info );
-		} );
-	} );
-}
-
-// Changes given package.json.
-function changePackageJson( path, changeFn ) {
-	return new Promise( function( resolve ) {
-		fs.readFile( path, 'utf-8', function( err, file ) {
-			var json;
-
-			if ( err ) {
-				throw err;
-			}
-
-			// Update package.json file.
-			json = JSON.parse( file );
-			json = changeFn( json );
-			fs.writeFile( path, JSON.stringify( json, null, 2 ), 'utf-8', function( err ) {
-				if ( err ) {
-					throw err;
-				}
-
-				resolve( );
-			} );
-		} );
-	} );
-}

+ 112 - 0
dev/tasks/plugin/common.js

@@ -0,0 +1,112 @@
+'use strict';
+var tools = require( '../utils/tools' );
+var path = require( 'path' );
+var fs = require( 'fs' );
+var INITIAL_COMMIT_MESSAGE = 'Initial plugin commit.';
+
+module.exports = {
+	linkPlugin: function( data ) {
+		// Don't use sudo on windows when executing npm link.
+		var isWin = process.platform == 'win32';
+		data.grunt.log.writeln( 'Linking plugin with npm link...' );
+
+		return new Promise( function( resolve ) {
+			var linkCommands = [
+				'cd ' + data.repositoryLocation,
+				( !isWin ? 'sudo ' : '' ) + 'npm link',
+				'cd ' + data.cwd,
+				'npm link ' + data.pluginName
+			];
+
+			tools.shExec( linkCommands.join( ' && ' ) );
+			resolve( data );
+		} );
+	},
+
+	cloneRepository: function( data ) {
+		var repo = 'git@github.com:' + data.gitHubUrl;
+		data.grunt.log.writeln( 'Cloning repository ' + repo + '...' );
+
+		return new Promise( function( resolve ) {
+			var cloneCommands = [
+				'cd ' + path.join( data.repositoryLocation, '..' ),
+				'git clone ' + repo
+			];
+
+			tools.shExec( cloneCommands.join( ' && ' ) );
+			resolve( data );
+		} );
+	},
+
+	/**
+	 * Creates initial git commit for new plugin. New plugin is created by merging CKEditor5 boilerplate and modifying
+	 * package.json. After that initial commit is needed.
+	 * @param data
+	 * @param {String} data.repositoryLocation New plugin location on disk.
+	 */
+	commitNewPlugin: function( data ) {
+		return new Promise( function( resolve ) {
+			var commitCommands = [
+				'cd ' + data.repositoryLocation,
+				'git add package.json',
+				'git commit -m "' + INITIAL_COMMIT_MESSAGE + '"'
+			];
+
+			tools.shExec( commitCommands.join( ' && ' ) );
+			resolve( data );
+		} );
+	},
+
+	/**
+	 * Updates CKEditor5 package.json file by adding new plugin dependency. Returns promise resolved when update is
+	 * finished.
+	 * @param {Object} data
+	 * @param {String} data.cwd Grunt starting directory - full path to CKEditor5 git repository on disk.
+	 * @param {String} data.pluginName New plugin name.
+	 * @param {String} data.gitHubUrl New plugin GitHub url.
+	 * @returns {Promise}
+	 */
+	updatePackageJson: function( data ) {
+		data.grunt.log.writeln( 'Updating CKEditor5 package.json...' );
+
+		return new Promise( function( resolve, reject ) {
+			module.exports.updateJSONFile( path.join( data.cwd, 'package.json' ), function( json ) {
+				if ( !json.dependencies ) {
+					json.dependencies = {};
+				}
+
+				json.dependencies[ data.pluginName ] = data.gitHubUrl;
+
+				return json;
+			} ).then( function() {
+				resolve( data );
+			}, function( error ) {
+				reject( error );
+			} );
+		} );
+	},
+
+	// Changes given package.json.
+	updateJSONFile: function( path, changeFn ) {
+		return new Promise( function( resolve, reject ) {
+			fs.readFile( path, 'utf-8', function( err, file ) {
+				var json;
+
+				if ( err ) {
+					return reject( err );
+				}
+
+				// Update package.json file.
+				json = JSON.parse( file );
+				json = changeFn( json );
+				fs.writeFile( path, JSON.stringify( json, null, 2 ), 'utf-8', function( err ) {
+					if ( err ) {
+						return reject( err );
+					}
+
+					resolve( );
+				} );
+			} );
+		} );
+	}
+};

+ 68 - 0
dev/tasks/plugin/create.js

@@ -0,0 +1,68 @@
+'use strict';
+var inquiries = require( './inquiries' );
+var common = require( './common' );
+var path = require( 'path' );
+var tools = require( '../utils/tools' );
+
+module.exports = function( grunt, done ) {
+	var data = {
+		grunt: grunt,
+		cwd: process.cwd(),
+		pluginName: '',
+		repositoryLocation: '',
+		gitHubUrl: '',
+		version: '0.0.1'
+	};
+
+	inquiries.getPluginName( data )
+		.then( inquiries.getRepositoryLocation )
+		.then( inquiries.getPluginVersion )
+		.then( inquiries.getPluginGitHubUrl )
+		.then( createRepository )
+		.then( updatePluginPackageJSON )
+		.then( common.updatePackageJson )
+		.then( common.linkPlugin )
+		.then( common.commitNewPlugin )
+		.then( function() {
+			done();
+		} ).catch( function( e ) {
+			done( e );
+		} );
+};
+
+// Updates plugin package.json by adding name of the plugin and version.
+function updatePluginPackageJSON( data ) {
+	var packageJSONPath = path.join( data.repositoryLocation, 'package.json' );
+
+	data.grunt.log.writeln( 'Updating plugin\'s package.json...' );
+
+	return new Promise( function( resolve ) {
+		common.updateJSONFile( packageJSONPath, function( json ) {
+			json.name = data.pluginName;
+			json.version = data.version;
+
+			return json;
+		} ).then( function() {
+			resolve( data );
+		} );
+	} );
+}
+
+// Initializes git repository and merges ckeditor5 boilerplate into master branch.
+function createRepository( data ) {
+	data.grunt.log.writeln( 'Initializing plugin repository...' );
+
+	return new Promise( function( resolve ) {
+		var repositoryLocation = data.repositoryLocation;
+		var initializeRepoCommands = [
+			'git init ' + repositoryLocation,
+			'cd ' + repositoryLocation,
+			'git remote add boilerplate git@github.com:ckeditor/ckeditor-boilerplate.git',
+			'git fetch boilerplate ckeditor5',
+			'git merge boilerplate/ckeditor5'
+		];
+
+		tools.shExec( initializeRepoCommands.join( ' && ' ) );
+		resolve( data );
+	} );
+}

+ 81 - 0
dev/tasks/plugin/inquiries.js

@@ -0,0 +1,81 @@
+'use strict';
+var inquirer = require( 'inquirer' );
+var path = require( 'path' );
+var fs = require( 'fs' );
+var DEFAULT_PLUGIN_NAME_PREFIX = 'ckeditor5-plugin-';
+var DEFAULT_GITHUB_URL_PREFIX = 'ckeditor/';
+
+module.exports = {
+	getPluginName: function( data ) {
+		return new Promise( function( resolve ) {
+			inquirer.prompt( [ {
+				name: 'pluginName',
+				message: 'Enter plugin name without ' + DEFAULT_PLUGIN_NAME_PREFIX + ' prefix:',
+				validate: function( input ) {
+					var regexp = /^[a-zA-Z0-9-_]+$/;
+
+					return regexp.test( input ) ? true : 'Please provide a valid plugin name.';
+				},
+				default: data.pluginName ? data.pluginName : null
+			} ], function( answers ) {
+				data.pluginName = DEFAULT_PLUGIN_NAME_PREFIX + answers.pluginName
+				resolve( data );
+			} );
+		} );
+	},
+
+	// Ask for initial version of the plugin.
+	getPluginVersion: function( data ) {
+		return new Promise( function( resolve ) {
+			inquirer.prompt( [ {
+				name: 'version',
+				message: 'Enter plugin\'s initial version:',
+				default: data.version ? data.version : ''
+			} ], function( answers ) {
+				data.version = answers.version;
+				resolve( data );
+			} );
+		} );
+	},
+
+	// Ask for the location of the repository.
+	getRepositoryLocation: function( data ) {
+		var defaultLocation = path.join( data.cwd, '..', data.pluginName );
+
+		return new Promise( function( resolve ) {
+			inquirer.prompt( [ {
+				name: 'path',
+				message: 'Enter repository location:',
+				default: defaultLocation,
+				validate: function( input ) {
+					var status;
+
+					try {
+						status = fs.statSync( input );
+					} catch ( e ) {}
+
+					return status && ( status.isFile() || status.isDirectory() ) ? 'Repository location already exists.' : true;
+				}
+			} ], function( answers ) {
+				data.repositoryLocation = answers.path;
+				resolve( data );
+			} );
+		} );
+	},
+
+	// Ask for GitHub Url.
+	getPluginGitHubUrl: function( data ) {
+		var defaultGitHubUrl = DEFAULT_GITHUB_URL_PREFIX + data.pluginName;
+
+		return new Promise( function( resolve ) {
+			inquirer.prompt( [ {
+				name: 'gitHubUrl',
+				message: 'Enter plugin\'s GitHub URL:',
+				default: defaultGitHubUrl
+			} ], function( answers ) {
+				data.gitHubUrl = answers.gitHubUrl;
+				resolve( data );
+			} );
+		} );
+	}
+};

+ 31 - 0
dev/tasks/plugin/install.js

@@ -0,0 +1,31 @@
+'use strict';
+var inquiries = require( './inquiries' );
+var common = require( './common' );
+
+/**
+ * Executes plugin install grunt task.
+ * @param grunt
+ * @param done
+ */
+module.exports = function( grunt, done ) {
+	var data = {
+		grunt: grunt,
+		cwd: process.cwd(),
+		pluginName: '',
+		repositoryLocation: '',
+		gitHubUrl: ''
+	};
+
+	inquiries.getPluginName( data )
+		.then( inquiries.getRepositoryLocation )
+		.then( inquiries.getPluginGitHubUrl )
+		.then( common.cloneRepository )
+		.then( common.linkPlugin )
+		.then( common.updatePackageJson )
+		.then( function() {
+			done();
+		} )
+		.catch( function( error ) {
+			done( error );
+		} );
+};

+ 1 - 1
gruntfile.js

@@ -28,7 +28,7 @@ module.exports = function( grunt ) {
 			}
 		},
 
-		plugin: { create: { } }
+		plugin: { }
 	} );
 
 	// Finally load the tasks.