Skip to content

Commit

Permalink
Internal: Replaced internal "pacote" calls with the decorated functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomek committed Oct 27, 2024
1 parent d34a626 commit 0f2a58f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

import pacote from 'pacote';
import { manifest } from './pacotecacheless.js';

/**
* Checks if the provided version for the package exists in the npm registry.
Expand All @@ -15,9 +15,9 @@ import pacote from 'pacote';
* @returns {Promise}
*/
export default async function checkVersionAvailability( version, packageName ) {
return pacote.manifest( `${ packageName }@${ version }`, { cache: null, preferOnline: true } )
return manifest( `${ packageName }@${ version }` )
.then( () => {
// If `pacote.manifest` resolves, a package with the given version exists.
// If `manifest` resolves, a package with the given version exists.
return false;
} )
.catch( () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import semver from 'semver';
import pacote from 'pacote';
import { manifest } from './pacotecacheless.js';

/**
* This util aims to verify if the given `packageName` can be published with the given `version` on the `npmTag`.
Expand All @@ -15,7 +15,7 @@ import pacote from 'pacote';
* @returns {Promise.<boolean>}
*/
export default async function isVersionPublishableForTag( packageName, version, npmTag ) {
const npmVersion = await pacote.manifest( `${ packageName }@${ npmTag }`, { cache: null, preferOnline: true } )
const npmVersion = await manifest( `${ packageName }@${ npmTag }` )
.then( ( { version } ) => version )
// An `npmTag` does not exist, or it's a first release of a package.
.catch( () => null );
Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-dev-release-tools/lib/utils/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { tools } from '@ckeditor/ckeditor5-dev-utils';
import pacote from 'pacote';
import { packument } from './pacotecacheless.js';
import getChangelog from './getchangelog.js';
import getPackageJson from './getpackagejson.js';

Expand Down Expand Up @@ -38,7 +38,7 @@ export function getLastFromChangelog( cwd = process.cwd() ) {
export function getLastPreRelease( releaseIdentifier, cwd = process.cwd() ) {
const packageName = getPackageJson( cwd ).name;

return pacote.packument( packageName, { cache: null, preferOnline: true } )
return packument( packageName )
.then( result => {
const lastVersion = Object.keys( result.versions )
.filter( version => version.startsWith( releaseIdentifier ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
*/

import { describe, expect, it, vi } from 'vitest';
import pacote from 'pacote';
import checkVersionAvailability from '../../lib/utils/checkversionavailability.js';
import { manifest } from '../../lib/utils/pacotecacheless.js';

vi.mock( 'pacote' );
vi.mock( '../../lib/utils/pacotecacheless.js' );

describe( 'checkVersionAvailability()', () => {
it( 'should resolve to true if version does not exist', async () => {
vi.mocked( pacote.manifest ).mockRejectedValue( 'E404' );
vi.mocked( manifest ).mockRejectedValue( 'E404' );

await expect( checkVersionAvailability( '1.0.1', 'stub-package' ) ).resolves.toBe( true );

expect( pacote.manifest ).toHaveBeenCalledExactlyOnceWith( '[email protected]', expect.any( Object ) );
expect( manifest ).toHaveBeenCalledExactlyOnceWith( '[email protected]' );
} );
it( 'should resolve to false if version exists', async () => {
pacote.manifest.mockResolvedValue( '1.0.1' );
manifest.mockResolvedValue( '1.0.1' );

await expect( checkVersionAvailability( '1.0.1', 'stub-package' ) ).resolves.toBe( false );
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@
*/

import { describe, expect, it, vi } from 'vitest';
import pacote from 'pacote';
import semver from 'semver';

import isVersionPublishableForTag from '../../lib/utils/isversionpublishablefortag.js';
import { manifest } from '../../lib/utils/pacotecacheless.js';

vi.mock( 'pacote' );
vi.mock( '../../lib/utils/pacotecacheless.js' );
vi.mock( 'semver' );

describe( 'isVersionPublishableForTag()', () => {
it( 'should return false if given version is not available', async () => {
vi.mocked( semver.lte ).mockReturnValue( true );
vi.mocked( pacote.manifest ).mockResolvedValue( ( {
vi.mocked( manifest ).mockResolvedValue( ( {
version: '1.0.0'
} ) );

const result = await isVersionPublishableForTag( 'package-name', '1.0.0', 'latest' );

expect( result ).to.equal( false );
expect( semver.lte ).toHaveBeenCalledExactlyOnceWith( '1.0.0', '1.0.0' );
expect( pacote.manifest ).toHaveBeenCalledExactlyOnceWith( 'package-name@latest', expect.any( Object ) );
expect( manifest ).toHaveBeenCalledExactlyOnceWith( 'package-name@latest' );
} );

it( 'should return false if given version is not higher than the latest published', async () => {
vi.mocked( semver.lte ).mockReturnValue( true );

vi.mocked( pacote.manifest ).mockResolvedValue( ( {
vi.mocked( manifest ).mockResolvedValue( ( {
version: '1.0.1'
} ) );

Expand All @@ -40,12 +40,12 @@ describe( 'isVersionPublishableForTag()', () => {
} );

it( 'should return true if given npm tag is not published yet', async () => {
vi.mocked( pacote.manifest ).mockRejectedValue( 'E404' );
vi.mocked( manifest ).mockRejectedValue( 'E404' );

const result = await isVersionPublishableForTag( 'package-name', '1.0.0', 'alpha' );

expect( result ).to.equal( true );
expect( semver.lte ).not.toHaveBeenCalled();
expect( pacote.manifest ).toHaveBeenCalledExactlyOnceWith( 'package-name@alpha', expect.any( Object ) );
expect( manifest ).toHaveBeenCalledExactlyOnceWith( 'package-name@alpha' );
} );
} );
36 changes: 18 additions & 18 deletions packages/ckeditor5-dev-release-tools/tests/utils/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { tools } from '@ckeditor/ckeditor5-dev-utils';
import pacote from 'pacote';
import getChangelog from '../../lib/utils/getchangelog.js';
import getPackageJson from '../../lib/utils/getpackagejson.js';
import { packument } from '../../lib/utils/pacotecacheless.js';

import {
getLastFromChangelog,
Expand All @@ -21,7 +21,7 @@ import {
} from '../../lib/utils/versions.js';

vi.mock( '@ckeditor/ckeditor5-dev-utils' );
vi.mock( 'pacote' );
vi.mock( '../../lib/utils/pacotecacheless.js' );
vi.mock( '../../lib/utils/getchangelog.js' );
vi.mock( '../../lib/utils/getpackagejson.js' );

Expand Down Expand Up @@ -106,20 +106,20 @@ describe( 'versions', () => {
} );

it( 'asks npm for all versions of a package', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {}
} );

return getLastPreRelease( '42.0.0-alpha' )
.then( () => {
expect( vi.mocked( pacote ).packument ).toHaveBeenCalledTimes( 1 );
expect( vi.mocked( pacote ).packument ).toHaveBeenCalledWith( 'ckeditor5', expect.any( Object ) );
expect( vi.mocked( packument ) ).toHaveBeenCalledTimes( 1 );
expect( vi.mocked( packument ) ).toHaveBeenCalledWith( 'ckeditor5' );
} );
} );

it( 'returns null if there is no version for a package', () => {
vi.mocked( pacote ).packument.mockRejectedValue();
vi.mocked( packument ).mockRejectedValue();

return getLastPreRelease( '42.0.0-alpha' )
.then( result => {
Expand All @@ -128,7 +128,7 @@ describe( 'versions', () => {
} );

it( 'returns null if there is no pre-release version matching the release identifier', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230615.0': {},
Expand All @@ -146,7 +146,7 @@ describe( 'versions', () => {
} );

it( 'returns last pre-release version matching the release identifier', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230615.0': {},
Expand All @@ -164,7 +164,7 @@ describe( 'versions', () => {
} );

it( 'returns last pre-release version matching the release identifier (non-chronological versions order)', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230615.0': {},
Expand All @@ -183,7 +183,7 @@ describe( 'versions', () => {
} );

it( 'returns last pre-release version matching the release identifier (sequence numbers greater than 10)', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230615.0': {},
Expand All @@ -203,7 +203,7 @@ describe( 'versions', () => {
} );

it( 'returns last nightly version', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230614.0': {},
Expand All @@ -225,7 +225,7 @@ describe( 'versions', () => {
} );

it( 'returns last nightly version from a specified day', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230614.0': {},
Expand Down Expand Up @@ -253,7 +253,7 @@ describe( 'versions', () => {
} );

it( 'returns last nightly pre-release version', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230613.0': {},
Expand All @@ -279,7 +279,7 @@ describe( 'versions', () => {
} );

it( 'returns pre-release version with id = 0 if pre-release version was never published for the package yet', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230615.0': {},
Expand All @@ -295,7 +295,7 @@ describe( 'versions', () => {
} );

it( 'returns pre-release version with incremented id if older pre-release version was already published', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230615.0': {},
Expand All @@ -311,7 +311,7 @@ describe( 'versions', () => {
} );

it( 'returns nightly version with incremented id if older nightly version was already published', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230615.5': {},
Expand Down Expand Up @@ -340,7 +340,7 @@ describe( 'versions', () => {
} );

it( 'asks for a last nightly pre-release version', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-nightly-20230615.0': {},
Expand Down Expand Up @@ -369,7 +369,7 @@ describe( 'versions', () => {
} );

it( 'asks for a last internal pre-release version', () => {
vi.mocked( pacote ).packument.mockResolvedValue( {
vi.mocked( packument ).mockResolvedValue( {
name: 'ckeditor5',
versions: {
'0.0.0-internal-20230615.0': {},
Expand Down

0 comments on commit 0f2a58f

Please sign in to comment.