Procházet zdrojové kódy

Changed logic of dev-install task.

Szymon Kupś před 10 roky
rodič
revize
1673a00a18

+ 15 - 6
dev/tasks/utils/dev-install.js

@@ -13,13 +13,15 @@ const path = require( 'path' );
  * This tasks install specified module in development mode. It can be executed by typing:
  * 		grunt dev-install --plugin <git_hub_url|npm_name|path_on_disk>
  *
+ *
  * It performs following steps:
  * 1. If GitHub URL is provided - clones the repository.
  * 2. If NPM module name is provided - gets GitHub URL from NPM and clones the repository.
  * 3. If path on disk is provided - it is used directly.
- * 4. Links plugin directory into `ckeditor5/node_modules/`.
- * 5. Adds dependency with local path to `ckeditor5/package.json`.
- * 6. Runs `npm install` in `ckeditor5/`.
+ * 4. Runs `npm install` in plugin repository.
+ * 5. If plugin exists in `ckeditor5/node_modules/` - runs `npm uninstall plugin_name`.
+ * 6. Links plugin directory into `ckeditor5/node_modules/`.
+ * 7. Adds dependency to `ckeditor5/package.json`.
  *
  * @param {String} ckeditor5Path Absolute path to `ckeditor5` repository.
  * @param {String} workspaceRoot Relative path to workspace root directory.
@@ -81,7 +83,17 @@ module.exports = ( ckeditor5Path, workspaceRoot, name, writeln ) => {
 			git.checkout( repositoryPath, urlInfo.branch );
 		}
 
+		// Run `npm install` in new repository.
+		writeln( `Running "npm install" in ${ urlInfo.name }...` );
+		tools.npmInstall( repositoryPath );
+
 		const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
+
+		if ( tools.isDirectory( linkPath ) ) {
+			writeln( `Uninstalling ${ urlInfo.name } from CKEditor5 node_modules...` );
+			tools.npmUninstall( ckeditor5Path, urlInfo.name );
+		}
+
 		writeln( `Linking ${ linkPath } to ${ repositoryPath }...` );
 		tools.linkDirectories( repositoryPath, linkPath );
 
@@ -92,9 +104,6 @@ module.exports = ( ckeditor5Path, workspaceRoot, name, writeln ) => {
 
 			return json;
 		} );
-
-		writeln( 'Running "npm install" in CKEditor5 repository...' );
-		tools.npmInstall( ckeditor5Path );
 	} else {
 		throw new Error( 'Please provide valid GitHub URL, NPM module name or path.' );
 	}

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

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

+ 15 - 10
dev/tests/dev-install.js

@@ -37,6 +37,7 @@ describe( 'dev-install', () => {
 		spies.checkout = sinon.stub( git, 'checkout' );
 		spies.getGitUrlFromNpm = sinon.stub( tools, 'getGitUrlFromNpm' );
 		spies.readPackageName = sinon.stub( tools, 'readPackageName' );
+		spies.npmUninstall = sinon.stub( tools, 'npmUninstall' );
 	} );
 
 	afterEach( () => {
@@ -47,10 +48,11 @@ describe( 'dev-install', () => {
 	it( 'should use GitHub URL', () => {
 		spies.isDirectory.onFirstCall().returns( false );
 		spies.isDirectory.onSecondCall().returns( false );
+		spies.isDirectory.onThirdCall().returns( true );
 
 		installTask( ckeditor5Path, workspacePath, repositoryUrl, () => {} );
 
-		sinon.assert.calledTwice( spies.isDirectory );
+		sinon.assert.calledThrice( spies.isDirectory );
 		sinon.assert.calledOnce( spies.parseUrl );
 		sinon.assert.calledWithExactly( spies.parseUrl, repositoryUrl );
 
@@ -63,8 +65,14 @@ describe( 'dev-install', () => {
 		sinon.assert.calledOnce( spies.checkout );
 		sinon.assert.calledWithExactly( spies.checkout, repositoryPath, urlInfo.branch );
 
+		sinon.assert.calledOnce( spies.npmInstall );
+		sinon.assert.calledWithExactly( spies.npmInstall, repositoryPath );
+
 		const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
 
+		sinon.assert.calledOnce( spies.npmUninstall );
+		sinon.assert.calledWithExactly( spies.npmUninstall, ckeditor5Path, urlInfo.name );
+
 		sinon.assert.calledOnce( spies.linkDirectories );
 		sinon.assert.calledWithExactly( spies.linkDirectories, repositoryPath, linkPath );
 
@@ -76,9 +84,6 @@ describe( 'dev-install', () => {
 		const json = updateFn( {} );
 		expect( json.dependencies ).to.be.a( 'object' );
 		expect( json.dependencies[ urlInfo.name ] ).to.equal( repositoryUrl );
-
-		sinon.assert.calledOnce( spies.npmInstall );
-		sinon.assert.calledWithExactly( spies.npmInstall, ckeditor5Path );
 	} );
 
 	it( 'should use npm module name', () => {
@@ -88,7 +93,7 @@ describe( 'dev-install', () => {
 
 		installTask( ckeditor5Path, workspacePath, moduleName, () => {} );
 
-		sinon.assert.calledTwice( spies.isDirectory );
+		sinon.assert.calledThrice( spies.isDirectory );
 		sinon.assert.calledTwice( spies.parseUrl );
 		sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
 		expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
@@ -103,6 +108,9 @@ describe( 'dev-install', () => {
 		sinon.assert.calledOnce( spies.checkout );
 		sinon.assert.calledWithExactly( spies.checkout, repositoryPath, urlInfo.branch );
 
+		sinon.assert.calledOnce( spies.npmInstall );
+		sinon.assert.calledWithExactly( spies.npmInstall, repositoryPath );
+
 		const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
 
 		sinon.assert.calledOnce( spies.linkDirectories );
@@ -116,9 +124,6 @@ describe( 'dev-install', () => {
 		const json = updateFn( {} );
 		expect( json.dependencies ).to.be.a( 'object' );
 		expect( json.dependencies[ urlInfo.name ] ).to.equal( repositoryUrl );
-
-		sinon.assert.calledOnce( spies.npmInstall );
-		sinon.assert.calledWithExactly( spies.npmInstall, ckeditor5Path );
 	} );
 
 	it( 'should use local relative path', () => {
@@ -128,7 +133,7 @@ describe( 'dev-install', () => {
 
 		installTask( ckeditor5Path, workspacePath, '../ckeditor5-core', () => {} );
 
-		sinon.assert.calledTwice( spies.isDirectory );
+		sinon.assert.calledThrice( spies.isDirectory );
 		sinon.assert.calledOnce( spies.readPackageName );
 	} );
 
@@ -139,7 +144,7 @@ describe( 'dev-install', () => {
 
 		installTask( ckeditor5Path, workspacePath, '/ckeditor5-core', () => {} );
 
-		sinon.assert.calledTwice( spies.isDirectory );
+		sinon.assert.calledThrice( spies.isDirectory );
 		sinon.assert.calledOnce( spies.readPackageName );
 	} );
 

+ 15 - 0
dev/tests/tools.js

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