Skip to content

Commit

Permalink
Removed redundant npm show call from reassignNpmTags().
Browse files Browse the repository at this point in the history
  • Loading branch information
martnpaneq committed Aug 16, 2023
1 parent ab63a9a commit 2798d87
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 45 deletions.
37 changes: 13 additions & 24 deletions packages/ckeditor5-dev-release-tools/lib/tasks/reassignnpmtags.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
const chalk = require( 'chalk' );
const columns = require( 'cli-columns' );
const { tools } = require( '@ckeditor/ckeditor5-dev-utils' );
const util = require( 'util' );
const exec = util.promisify( require( 'child_process' ).exec );
const assertNpmAuthorization = require( '../utils/assertnpmauthorization' );

/**
Expand All @@ -35,26 +37,21 @@ module.exports = async function reassignNpmTags( { npmOwner, version, packages }
counter.start();

const updateTagPromises = packages.map( async packageName => {
const showLatestVersionRetryable = retry( () => exec( `npm show ${ packageName }@latest version` ) );
const latestVersion = await showLatestVersionRetryable()
.then( version => version.trim() )
.catch( error => {
errors.push( trimErrorMessage( error.message ) );
} );

if ( latestVersion === version ) {
packagesSkipped.push( packageName );

return null;
}

const updateLatestTagRetryable = retry( () => exec( `npm dist-tag add ${ packageName }@${ version } latest` ) );
await updateLatestTagRetryable()
.then( () => {
packagesUpdated.push( packageName );
.then( response => {
if ( response.stdout ) {
packagesUpdated.push( packageName );
} else if ( response.stderr ) {
throw new Error( response.stderr );
}
} )
.catch( error => {
errors.push( trimErrorMessage( error.message ) );
if ( error.message.includes( 'is already set to version' ) ) {
packagesSkipped.push( packageName );
} else {
errors.push( trimErrorMessage( error.message ) );
}
} )
.finally( () => {
counter.increase();
Expand Down Expand Up @@ -89,14 +86,6 @@ function trimErrorMessage( message ) {
return message.replace( /npm ERR!.*\n/g, '' ).trim();
}

/**
* @param {String} command
* @returns {Promise.<String>}
*/
async function exec( command ) {
return tools.shExec( command, { verbosity: 'silent', async: true } );
}

/**
* @param {Function} callback
* @param {Number} times
Expand Down
39 changes: 18 additions & 21 deletions packages/ckeditor5-dev-release-tools/tests/tasks/reassignnpmtags.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe( 'reassignNpmTags()', () => {

stubs = {
tools: {
shExec: sinon.stub(),
createSpinner: sinon.stub().callsFake( () => {
return stubs.spinner;
} )
Expand All @@ -43,15 +42,18 @@ describe( 'reassignNpmTags()', () => {
columns: sinon.stub(),
console: {
log: sinon.stub( console, 'log' )
}
},
util: {
promisify: sinon.stub().callsFake( () => stubs.exec )
},
exec: sinon.stub()
};

stubs.tools.shExec.withArgs( sinon.match( 'npm show' ) ).resolves( '1.0.0' );

mockery.registerMock( '@ckeditor/ckeditor5-dev-utils', { tools: stubs.tools } );
mockery.registerMock( '../utils/assertnpmauthorization', stubs.assertNpmAuthorization );
mockery.registerMock( 'cli-columns', stubs.columns );
mockery.registerMock( 'chalk', stubs.chalk );
mockery.registerMock( 'util', stubs.util );

reassignNpmTags = require( '../../lib/tasks/reassignnpmtags' );
} );
Expand All @@ -64,7 +66,7 @@ describe( 'reassignNpmTags()', () => {

it( 'should throw an error when assertNpmAuthorization throws error', async () => {
stubs.assertNpmAuthorization.throws( new Error( 'User not logged in error' ) );
const npmDistTagAdd = stubs.tools.shExec.withArgs( sinon.match( 'npm dist-tag add' ) );
const npmDistTagAdd = stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) );

try {
await reassignNpmTags( { npmOwner: 'correct-npm-user', version: '1.0.1', packages: [ 'package1' ] } );
Expand All @@ -76,25 +78,18 @@ describe( 'reassignNpmTags()', () => {
expect( npmDistTagAdd.callCount ).to.equal( 0 );
} );

it( 'should still try to update tags when can not obtain package version from npm', async () => {
stubs.tools.shExec.withArgs( sinon.match( 'npm show' ) ).throws( new Error( 'Can not obtain package version.' ) );
const npmDistTagAdd = stubs.tools.shExec.withArgs( sinon.match( 'npm dist-tag add' ) );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.1', packages: [ 'package1', 'package2' ] } );

expect( npmDistTagAdd.callCount ).to.equal( 2 );
} );

it( 'should skip updating tags when provided version matches existing version for tag latest', async () => {
const npmDistTagAdd = stubs.tools.shExec.withArgs( sinon.match( 'npm dist-tag add' ) );
stubs.columns.returns( 'package1 | package2' );
stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) ).throws( new Error( 'is already set to version' ) );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.0', packages: [ 'package1', 'package2' ] } );

expect( npmDistTagAdd.callCount ).to.equal( 0 );
expect( stubs.console.log.firstCall.args[ 0 ] ).to.equal( '⬇️ Packages skipped:' );
expect( stubs.console.log.secondCall.args[ 0 ] ).to.deep.equal( 'package1 | package2' );
} );

it( 'should update tags when tag latest for provided version does not yet exist', async () => {
const npmDistTagAdd = stubs.tools.shExec.withArgs( sinon.match( 'npm dist-tag add' ) );
const npmDistTagAdd = stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) ).resolves( { stdout: '+latest' } );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.1', packages: [ 'package1', 'package2' ] } );

Expand All @@ -103,7 +98,7 @@ describe( 'reassignNpmTags()', () => {
} );

it( 'should continue updating packages even if first package update fails', async () => {
const npmDistTagAdd = stubs.tools.shExec.withArgs( sinon.match( 'npm dist-tag add' ) );
const npmDistTagAdd = stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) );
npmDistTagAdd.onFirstCall().throws( new Error( 'Npm error while updating tag.' ) );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.1', packages: [ 'package1', 'package2' ] } );
Expand Down Expand Up @@ -131,7 +126,7 @@ describe( 'reassignNpmTags()', () => {
} );

it( 'should increase the spinner counter after failure processing a package', async () => {
const npmDistTagAdd = stubs.tools.shExec.withArgs( sinon.match( 'npm dist-tag add' ) );
const npmDistTagAdd = stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) );
npmDistTagAdd.onFirstCall().throws( new Error( 'Npm error while updating tag.' ) );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.1', packages: [ 'package1' ] } );
Expand All @@ -140,7 +135,7 @@ describe( 'reassignNpmTags()', () => {
} );

it( 'should finish the spinner once all packages have been processed', async () => {
const npmDistTagAdd = stubs.tools.shExec.withArgs( sinon.match( 'npm dist-tag add' ) );
const npmDistTagAdd = stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) );
npmDistTagAdd.onFirstCall().throws( new Error( 'Npm error while updating tag.' ) );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.1', packages: [ 'package1', 'package2' ] } );
Expand All @@ -154,6 +149,7 @@ describe( 'reassignNpmTags()', () => {
} );

it( 'should display skipped packages in a column', async () => {
stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) ).throws( new Error( 'is already set to version' ) );
stubs.columns.returns( '1 | 2 | 3' );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.0', packages: [ 'package1', 'package2' ] } );
Expand All @@ -168,6 +164,7 @@ describe( 'reassignNpmTags()', () => {
} );

it( 'should display processed packages in a column', async () => {
stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) ).resolves( { stdout: '+latest' } );
stubs.columns.returns( '1 | 2 | 3' );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.1', packages: [ 'package1', 'package2' ] } );
Expand All @@ -182,7 +179,7 @@ describe( 'reassignNpmTags()', () => {
} );

it( 'should display errors found during processing a package', async () => {
const npmDistTagAdd = stubs.tools.shExec.withArgs( sinon.match( 'npm dist-tag add' ) );
const npmDistTagAdd = stubs.exec.withArgs( sinon.match( 'npm dist-tag add' ) );
npmDistTagAdd.throws( new Error( 'Npm error while updating tag.' ) );

await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.1', packages: [ 'package1' ] } );
Expand Down

0 comments on commit 2798d87

Please sign in to comment.