Skip to content

Commit

Permalink
Merge pull request #158 from ckeditor/i/155
Browse files Browse the repository at this point in the history
Other (generator): When generating a new package, the generator uses the latest stable CKEditor 5 release. Closes #155.
  • Loading branch information
psmyrek committed Jul 5, 2023
2 parents af82dd8 + e810220 commit 40a9b62
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
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 );
} );
} );

0 comments on commit 40a9b62

Please sign in to comment.