Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the @latest release when looking for CKEditor 5 version #158

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@

'use strict';

const { spawnSync } = require( 'child_process' );
const { execSync } = require( 'child_process' );

/**
* Returns the version of the specified package.
* Returns version of the specified package.
*
* @param packageName Name of the package to check the version of.
* @return {String}
*/
module.exports = function getPackageVersion( packageName ) {
// See: #39.
const response = spawnSync( 'npm', [ 'view', packageName, '--json' ], {
encoding: 'utf8',
shell: true
} );

try {
return JSON.parse( response.stdout.toString() ).versions.pop();
} catch ( err ) {
throw new Error( 'The specified package has not been published on npm yet.' );
}
return execSync( `npm view ${ packageName } version` ).toString().trim();
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const expect = require( 'chai' ).expect;
const mockery = require( 'mockery' );

describe( 'lib/utils/get-package-version', () => {
let getPackageVersion, spawnSyncStub;
let getPackageVersion, execSyncStub;

beforeEach( () => {
mockery.enable( {
Expand All @@ -19,16 +19,10 @@ describe( 'lib/utils/get-package-version', () => {
warnOnUnregistered: false
} );

spawnSyncStub = sinon.stub().returns( {
stdout: Buffer.from( JSON.stringify( {
versions: [
'30.0.0'
]
} ) )
} );
execSyncStub = sinon.stub().returns( Buffer.from( '30.0.0' ) );

mockery.registerMock( 'child_process', {
spawnSync: spawnSyncStub
execSync: execSyncStub
} );

getPackageVersion = require( '../../lib/utils/get-package-version' );
Expand All @@ -49,27 +43,15 @@ describe( 'lib/utils/get-package-version', () => {
expect( returnedValue ).to.be.a( 'string' );
} );

it( 'calls "npm view" to determine the version', () => {
it( 'calls "npm show" to determine the version', () => {
getPackageVersion( 'ckeditor5' );

expect( spawnSyncStub.firstCall.args[ 0 ] ).to.equal( 'npm' );
expect( spawnSyncStub.firstCall.args[ 1 ] ).to.deep.equal( [ 'view', 'ckeditor5', '--json' ] );
expect( spawnSyncStub.firstCall.args[ 2 ] ).to.be.an( 'object' );
expect( execSyncStub.firstCall.firstArg ).to.equal( 'npm view ckeditor5 version' );
} );

it( 'returns a version matching semantic versioning specification', () => {
const returnedValue = getPackageVersion( 'ckeditor5' );

expect( returnedValue ).to.equal( '30.0.0' );
} );

it( 'throws an error when asking about a non-existing package', () => {
spawnSyncStub.returns( {
stdout: Buffer.from( '' )
} );

expect(
() => getPackageVersion( 'non-existing-foo-package' )
).to.throw( 'The specified package has not been published on npm yet.', Error );
} );
} );