8
0
Szymon Kupś 10 лет назад
Родитель
Сommit
dba9a52eb6
6 измененных файлов с 85 добавлено и 64 удалено
  1. 1 1
      dev/tasks/dev.js
  2. 18 8
      dev/tasks/utils/dev-update.js
  3. 9 0
      dev/tasks/utils/tools.js
  4. 0 5
      dev/tests/dev-install.js
  5. 43 50
      dev/tests/dev-update.js
  6. 14 0
      dev/tests/tools.js

+ 1 - 1
dev/tasks/dev.js

@@ -30,7 +30,7 @@ module.exports = ( grunt ) => {
 	} );
 
 	grunt.registerTask( 'dev-update', function() {
-		pluginUpdateTask( ckeditor5Path, packageJSON, workspaceRoot, grunt.log.writeln, grunt.log.error );
+		pluginUpdateTask( ckeditor5Path, packageJSON, workspaceRoot, grunt.log.writeln, grunt.option( 'npm-update' ) );
 	} );
 
 	grunt.registerTask( 'dev-status', function() {

+ 18 - 8
dev/tasks/utils/dev-update.js

@@ -18,9 +18,10 @@ const path = require( 'path' );
  * @param {Object} packageJSON Parsed package.json file from CKEditor5 repository.
  * @param {String} workspaceRoot Relative path to workspace root.
  * @param {Function} writeln Function for log output.
- * @param {Function} writeError Function of error output
+ * @param {Boolean} runNpmUpdate When set to true `npm update` will be executed inside each plugin repository
+ * and inside CKEditor 5 repository.
  */
-module.exports = ( ckeditor5Path, packageJSON, workspaceRoot, writeln, writeError ) => {
+module.exports = ( ckeditor5Path, packageJSON, workspaceRoot, writeln, runNpmUpdate ) => {
 	const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
 
 	// Get all CKEditor dependencies from package.json.
@@ -36,15 +37,24 @@ module.exports = ( ckeditor5Path, packageJSON, workspaceRoot, writeln, writeErro
 				const repositoryAbsolutePath = path.join( workspaceAbsolutePath, dependency );
 
 				// Check if repository's directory already exists.
-				if ( directories.indexOf( dependency ) > -1 ) {
-					try {
-						writeln( `Updating ${ repositoryURL }...` );
-						git.pull( repositoryAbsolutePath, urlInfo.branch );
-					} catch ( error ) {
-						writeError( error );
+				if ( directories.indexOf( urlInfo.name ) > -1 ) {
+					writeln( `Checking out ${ urlInfo.name } to ${ urlInfo.branch }...` );
+					git.checkout( repositoryAbsolutePath, urlInfo.branch );
+
+					writeln( `Pulling changes to ${ urlInfo.name }...` );
+					git.pull( repositoryAbsolutePath, urlInfo.branch );
+
+					if ( runNpmUpdate ) {
+						writeln( `Running "npm update" in ${ urlInfo.name }...` );
+						tools.npmUpdate( repositoryAbsolutePath );
 					}
 				}
 			}
+
+			if ( runNpmUpdate ) {
+				writeln( `Running "npm update" in CKEditor5 repository...` );
+				tools.npmUpdate( ckeditor5Path );
+			}
 		} else {
 			writeln( 'No CKEditor5 plugins in development mode.' );
 		}

+ 9 - 0
dev/tasks/utils/tools.js

@@ -294,6 +294,15 @@ module.exports = {
 		this.shExec( `cd ${ path } && npm uninstall ${ name }` );
 	},
 
+	/**
+	 * Calls `npm update` command in specified path.
+	 *
+	 * @param {String} path
+	 */
+	npmUpdate( path ) {
+		this.shExec( `cd ${ path } && npm update` );
+	},
+
 	/**
 	 * Installs Git hooks in specified repository.
 	 *

+ 0 - 5
dev/tests/dev-install.js

@@ -21,13 +21,9 @@ describe( 'dev-install', () => {
 	const ckeditor5Path = '/path/to/ckeditor';
 	const workspacePath = '..';
 	const workspaceAbsolutePath = path.join( ckeditor5Path, workspacePath );
-
-	let toRestore;
 	const spies = {};
 
 	beforeEach( () => {
-		toRestore = [];
-
 		spies.parseUrl = sinon.spy( git, 'parseRepositoryUrl' );
 		spies.isDirectory = sinon.stub( tools, 'isDirectory' );
 		spies.cloneRepository = sinon.stub( git, 'cloneRepository' );
@@ -42,7 +38,6 @@ describe( 'dev-install', () => {
 	} );
 
 	afterEach( () => {
-		toRestore.forEach( item => item.restore() );
 		Object.keys( spies ).forEach( ( spy ) => spies[ spy ].restore() );
 	} );
 

+ 43 - 50
dev/tests/dev-update.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
-/* global describe, it */
+/* global describe, it, beforeEach, afterEach */
 
 'use strict';
 
@@ -12,18 +12,29 @@ const tools = require( '../tasks/utils/tools' );
 const git = require( '../tasks/utils/git' );
 const path = require( 'path' );
 
-describe( 'dev-init', () => {
+describe( 'dev-update', () => {
 	const updateTask = require( '../tasks/utils/dev-update' );
 	const ckeditor5Path = 'path/to/ckeditor5';
 	const workspaceRoot = '..';
 	const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
 	const emptyFn = () => {};
+	const spies = {};
+
+	beforeEach( () => {
+		spies.getDependencies = sinon.spy( tools, 'getCKEditorDependencies' );
+		spies.checkout = sinon.stub( git, 'checkout' );
+		spies.pull = sinon.stub( git, 'pull' );
+		spies.npmUpdate = sinon.stub( tools, 'npmUpdate' );
+	} );
+
+	afterEach( () => {
+		Object.keys( spies ).forEach( ( spy ) => spies[ spy ].restore() );
+	} );
 
 	it( 'should update dev repositories', () => {
 		const dirs = [ 'ckeditor5-core', 'ckeditor5-devtest' ];
-		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
-		const getDirectoriesStub = sinon.stub( tools, 'getCKE5Directories' ).returns( dirs );
-		const pullStub = sinon.stub( git, 'pull' );
+		spies.getDirectories = sinon.stub( tools, 'getCKE5Directories' ).returns( dirs );
+
 		const json = {
 			dependencies: {
 				'ckeditor5-core': 'ckeditor/ckeditor5-core',
@@ -32,79 +43,61 @@ describe( 'dev-init', () => {
 			}
 		};
 
-		updateTask( ckeditor5Path, json, workspaceRoot, emptyFn, emptyFn );
+		updateTask( ckeditor5Path, json, workspaceRoot, emptyFn, true );
 
-		getDependenciesSpy.restore();
-		getDirectoriesStub.restore();
-		pullStub.restore();
+		sinon.assert.calledTwice( spies.pull );
+		sinon.assert.calledWithExactly( spies.pull.firstCall, path.join( workspaceAbsolutePath, dirs[ 0 ] ), 'master' );
+		sinon.assert.calledWithExactly( spies.pull.secondCall, path.join( workspaceAbsolutePath, dirs[ 1 ] ), 'new-branch' );
 
-		sinon.assert.calledTwice( pullStub );
-		sinon.assert.calledWithExactly( pullStub.firstCall, path.join( workspaceAbsolutePath, dirs[ 0 ] ), 'master' );
-		sinon.assert.calledWithExactly( pullStub.secondCall, path.join( workspaceAbsolutePath, dirs[ 1 ] ), 'new-branch' );
+		sinon.assert.calledThrice( spies.npmUpdate );
+		sinon.assert.calledWithExactly( spies.npmUpdate.firstCall, path.join( workspaceAbsolutePath, dirs[ 0 ] ) );
+		sinon.assert.calledWithExactly( spies.npmUpdate.secondCall, path.join( workspaceAbsolutePath, dirs[ 1 ] ) );
+		sinon.assert.calledWithExactly( spies.npmUpdate.thirdCall, ckeditor5Path );
 	} );
 
-	it( 'should not update when no dependencies are found', () => {
-		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
-		const getDirectoriesStub = sinon.stub( tools, 'getCKE5Directories' );
-		const pullStub = sinon.stub( git, 'pull' );
+	it( 'should update dev repositories without running npm update', () => {
+		const dirs = [ 'ckeditor5-core' ];
+		spies.getDirectories = sinon.stub( tools, 'getCKE5Directories' ).returns( dirs );
+
 		const json = {
 			dependencies: {
+				'ckeditor5-core': 'ckeditor/ckeditor5-core',
+				'ckeditor5-devtest': 'ckeditor/ckeditor5-devtest#new-branch',
 				'other-plugin': '1.2.3'
 			}
 		};
 
-		updateTask( ckeditor5Path, json, workspaceRoot, emptyFn, emptyFn );
-
-		getDependenciesSpy.restore();
-		getDirectoriesStub.restore();
-		pullStub.restore();
-
-		sinon.assert.notCalled( pullStub );
+		updateTask( ckeditor5Path, json, workspaceRoot, emptyFn, false );
+		sinon.assert.calledWithExactly( spies.pull.firstCall, path.join( workspaceAbsolutePath, dirs[ 0 ] ), 'master' );
+		sinon.assert.notCalled( spies.npmUpdate );
 	} );
 
-	it( 'should not update when no plugins in dev mode', () => {
-		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
-		const getDirectoriesStub = sinon.stub( tools, 'getCKE5Directories' ).returns( [] );
-		const pullStub = sinon.stub( git, 'pull' );
+	it( 'should not update when no dependencies are found', () => {
+		spies.getDirectories = sinon.stub( tools, 'getCKE5Directories' );
 		const json = {
 			dependencies: {
-				'ckeditor5-devtest': 'ckeditor/ckeditor5-devtest#new-branch',
 				'other-plugin': '1.2.3'
 			}
 		};
 
-		updateTask( ckeditor5Path, json, workspaceRoot, emptyFn, emptyFn );
+		updateTask( ckeditor5Path, json, workspaceRoot, emptyFn, false );
 
-		getDependenciesSpy.restore();
-		getDirectoriesStub.restore();
-		pullStub.restore();
-
-		sinon.assert.notCalled( pullStub );
+		sinon.assert.notCalled( spies.pull );
+		sinon.assert.notCalled( spies.npmUpdate );
 	} );
 
-	it( 'should write error message when pulling is unsuccessful', () => {
-		const dirs = [ 'ckeditor5-core' ];
-		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
-		const getDirectoriesStub = sinon.stub( tools, 'getCKE5Directories' ).returns( dirs );
-		const error = new Error( 'Error message.' );
-		const pullStub = sinon.stub( git, 'pull' ).throws( error );
+	it( 'should not update when no plugins in dev mode', () => {
+		spies.getDirectories = sinon.stub( tools, 'getCKE5Directories' ).returns( [] );
 		const json = {
 			dependencies: {
-				'ckeditor5-core': 'ckeditor/ckeditor5-core',
 				'ckeditor5-devtest': 'ckeditor/ckeditor5-devtest#new-branch',
 				'other-plugin': '1.2.3'
 			}
 		};
-		const writeErrorSpy = sinon.spy();
-
-		updateTask( ckeditor5Path, json, workspaceRoot, emptyFn, writeErrorSpy );
 
-		getDependenciesSpy.restore();
-		getDirectoriesStub.restore();
-		pullStub.restore();
+		updateTask( ckeditor5Path, json, workspaceRoot, emptyFn, false );
 
-		sinon.assert.calledOnce( pullStub );
-		sinon.assert.calledOnce( writeErrorSpy );
-		sinon.assert.calledWithExactly( writeErrorSpy, error );
+		sinon.assert.notCalled( spies.pull );
+		sinon.assert.notCalled( spies.npmUpdate );
 	} );
 } );

+ 14 - 0
dev/tests/tools.js

@@ -307,6 +307,20 @@ describe( 'utils', () => {
 			} );
 		} );
 
+		describe( 'npmUpdate', () => {
+			it( 'should be defined', () => expect( tools.npmUpdate ).to.be.a( 'function' ) );
+			it( 'should execute npm update command', () => {
+				const shExecStub = sinon.stub( tools, 'shExec' );
+				const path = '/path/to/repository';
+				toRestore.push( shExecStub );
+
+				tools.npmUpdate( path );
+
+				expect( shExecStub.calledOnce ).to.equal( true );
+				expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm update` );
+			} );
+		} );
+
 		describe( 'npmUninstall', () => {
 			it( 'should be defined', () => expect( tools.npmUninstall ).to.be.a( 'function' ) );
 			it( 'should execute npm uninstall command', () => {