Skip to content

Commit

Permalink
Merge pull request #40 from ckeditor/ck/adjust-version-typing
Browse files Browse the repository at this point in the history
Feature: Add testing versions support to injector.
  • Loading branch information
Mati365 authored Nov 4, 2024
2 parents 0a2de0e + d3fde1a commit 518448b
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/cdn/ck/createCKCdnBaseBundlePack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { without } from '../../utils/without.js';
import { getCKBaseBundleInstallationInfo } from '../../installation-info/getCKBaseBundleInstallationInfo.js';
import { createCKDocsUrl } from '../../docs/createCKDocsUrl.js';

import { createCKCdnUrl, type CKCdnVersion } from './createCKCdnUrl.js';
import { createCKCdnUrl, type CKCdnUrlCreator } from './createCKCdnUrl.js';
import type { CKCdnVersion } from './isCKCdnVersion.js';

import './globals.js';

Expand Down Expand Up @@ -118,5 +119,5 @@ export type CKCdnBaseBundlePackConfig = {
/**
* The function that creates custom CDN URLs.
*/
createCustomCdnUrl?: typeof createCKCdnUrl;
createCustomCdnUrl?: CKCdnUrlCreator;
};
12 changes: 6 additions & 6 deletions src/cdn/ck/createCKCdnUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
* For licensing, see LICENSE.md.
*/

import type { SemanticVersion } from '../../utils/version/isSemanticVersion.js';
import type { CKCdnVersion } from './isCKCdnVersion.js';

/**
* The URL of the CKEditor CDN.
*/
export const CK_CDN_URL = 'https://cdn.ckeditor.com';

/**
* A version of a file on the CKEditor CDN.
*/
export type CKCdnVersion = SemanticVersion;

/**
* Creates a URL to a file on the CKEditor CDN.
*
Expand All @@ -32,3 +27,8 @@ export type CKCdnVersion = SemanticVersion;
export function createCKCdnUrl( bundle: string, file: string, version: CKCdnVersion ): string {
return `${ CK_CDN_URL }/${ bundle }/${ version }/${ file }`;
}

/**
* A function that creates a URL to a file on the CKEditor CDN.
*/
export type CKCdnUrlCreator = typeof createCKCdnUrl;
60 changes: 60 additions & 0 deletions src/cdn/ck/isCKCdnVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/

import { isSemanticVersion, type SemanticVersion } from '../../utils/version/isSemanticVersion.js';

/**
* A version of the CKEditor that is used for testing purposes.
*/
export type CKCdnTestingVersion =
| 'nightly'
| 'alpha'
| 'internal';

/**
* A version of a file on the CKEditor CDN.
*/
export type CKCdnVersion =
| SemanticVersion
| CKCdnTestingVersion;

/**
* Checks if the given string is a version of a file on the CKEditor CDN.
*
* @param version - The string to check.
* @returns `true` if the string is a version of a file on the CKEditor CDN, `false` otherwise.
* @example
* ```ts
* isCKCdnTestingVersion( '1.2.3-nightly-abc' ); // -> true
* isCKCdnTestingVersion( '1.2.3-internal-abc' ); // -> true
* isCKCdnTestingVersion( '1.2.3-alpha.1' ); // -> true
* isCKCdnTestingVersion( '1.2.3' ); // -> false
* isCKCdnTestingVersion( 'nightly' ); // -> true
* ```
*/
export function isCKCdnTestingVersion( version: string | undefined ): version is CKCdnTestingVersion {
if ( !version ) {
return false;
}

return [ 'nightly', 'alpha', 'internal' ].some( testVersion => version.includes( testVersion ) );
}

/**
* Checks if the given string is a version of a file on the CKEditor CDN.
*
* @param version - The string to check.
* @returns `true` if the string is a version of a file on the CKEditor CDN, `false` otherwise.
* @example
* ```ts
* isCKCdnVersion( 'nightly' ); // -> true
* isCKCdnVersion( 'alpha' ); // -> true
* isCKCdnVersion( 'rc-1.2.3' ); // -> true
* isCKCdnVersion( '1.2.3' ); // -> true
* ```
*/
export function isCKCdnVersion( version: string | undefined ): version is CKCdnVersion {
return isSemanticVersion( version ) || isCKCdnTestingVersion( version );
}
12 changes: 10 additions & 2 deletions src/cdn/loadCKEditorCloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
type CKBoxCdnBundlePackConfig
} from './ckbox/createCKBoxCdnBundlePack.js';

import type { CKCdnUrlCreator } from './ck/createCKCdnUrl.js';
import type { ConditionalBlank } from '../types/ConditionalBlank.js';
import type { CKCdnVersion, createCKCdnUrl } from './ck/createCKCdnUrl.js';

import { isCKCdnTestingVersion, type CKCdnVersion } from './ck/isCKCdnVersion.js';

import {
loadCKCdnResourcesPack,
Expand Down Expand Up @@ -83,6 +85,12 @@ export function loadCKEditorCloud<Config extends CKEditorCloudConfig>(
}
} = config;

if ( isCKCdnTestingVersion( version ) ) {
console.warn(
'You are using a testing version of CKEditor 5. Please remember that it is not suitable for production environments.'
);
}

const pack = combineCKCdnBundlesPacks( {
CKEditor: createCKCdnBaseBundlePack( {
version,
Expand Down Expand Up @@ -187,5 +195,5 @@ export type CKEditorCloudConfig<Plugins extends CdnPluginsPacks = CdnPluginsPack
/**
* The function that creates custom CDN URLs.
*/
createCustomCdnUrl?: typeof createCKCdnUrl;
createCustomCdnUrl?: CKCdnUrlCreator;
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export { isCKEditorFreeLicense } from './license/isCKEditorFreeLicense.js';

export {
CK_CDN_URL,
createCKCdnUrl
createCKCdnUrl,
type CKCdnUrlCreator
} from './cdn/ck/createCKCdnUrl.js';

export {
Expand Down
6 changes: 3 additions & 3 deletions src/installation-info/getCKBaseBundleInstallationInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

import type { BundleInstallationInfo } from './types.js';

import { isSemanticVersion } from '../utils/version/isSemanticVersion.js';
import { isCKCdnVersion, type CKCdnVersion } from '../cdn/ck/isCKCdnVersion.js';

/**
* Returns information about the base CKEditor bundle installation.
*/
export function getCKBaseBundleInstallationInfo(): BundleInstallationInfo | null {
export function getCKBaseBundleInstallationInfo(): BundleInstallationInfo<CKCdnVersion> | null {
const { CKEDITOR_VERSION, CKEDITOR } = window;

if ( !isSemanticVersion( CKEDITOR_VERSION ) ) {
if ( !isCKCdnVersion( CKEDITOR_VERSION ) ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* For licensing, see LICENSE.md.
*/

import { isCKCdnTestingVersion } from '../cdn/ck/isCKCdnVersion.js';
import type { LicenseKeyVersion } from '../license/LicenseKey.js';

import { destructureSemanticVersion } from '../utils/version/destructureSemanticVersion.js';
Expand All @@ -22,6 +23,13 @@ export function getSupportedLicenseVersionInstallationInfo(): LicenseKeyVersion
}

const { version } = installationInfo;

// Assume that the testing version is always the newest one
// so we can return the highest supported license version.
if ( isCKCdnTestingVersion( version ) ) {
return 3;
}

const { major } = destructureSemanticVersion( version );

// License V3 was released in CKEditor 44.0.0.
Expand Down
4 changes: 2 additions & 2 deletions src/installation-info/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type BundleInstallationSource = 'npm' | 'cdn';
/**
* Information about the currently installed CKEditor.
*/
export type BundleInstallationInfo = {
export type BundleInstallationInfo<V extends string = SemanticVersion> = {

/**
* The source from which CKEditor was installed.
Expand All @@ -23,5 +23,5 @@ export type BundleInstallationInfo = {
/**
* The version of CKEditor.
*/
version: SemanticVersion;
version: V;
};
2 changes: 1 addition & 1 deletion tests/cdn/ck/createCKCdnBaseBundlePack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { describe, it, expect, beforeEach } from 'vitest';

import type { CKCdnVersion } from '@/cdn/ck/createCKCdnUrl.js';
import type { CKCdnVersion } from '@/cdn/ck/isCKCdnVersion.js';

import {
createCKCdnBaseBundlePack,
Expand Down
2 changes: 1 addition & 1 deletion tests/cdn/ck/createCKCdnPremiumBundlePack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { describe, it, expect, beforeEach } from 'vitest';

import type { CKCdnVersion } from '@/cdn/ck/createCKCdnUrl.js';
import type { CKCdnVersion } from '@/cdn/ck/isCKCdnVersion.js';

import { loadCKCdnResourcesPack } from '@/cdn/utils/loadCKCdnResourcesPack.js';
import { removeAllCkCdnResources } from '@/test-utils/cdn/removeAllCkCdnResources.js';
Expand Down
35 changes: 35 additions & 0 deletions tests/cdn/ck/isCKCdnVersion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/

import { describe, it, expect } from 'vitest';
import { isCKCdnVersion, isCKCdnTestingVersion } from '@/cdn/ck/isCKCdnVersion.js';

const testCases = [
{ version: 'alpha', isTesting: true, isVersion: true },
{ version: 'nightly', isTesting: true, isVersion: true },
{ version: 'rc-1.2.3', isTesting: false, isVersion: false },
{ version: '1.2.3', isTesting: false, isVersion: true },
{ version: 'beta', isTesting: false, isVersion: false },
{ version: '1.2', isTesting: false, isVersion: false },
{ version: '0.0.0-nightly-20241104.0', isTesting: true, isVersion: true },
{ version: '0.0.0-internal-20241104.0', isTesting: true, isVersion: true },
{ version: '43.3.0-alpha.12 ', isTesting: true, isVersion: true }
];

describe( 'isCKCdnTestingVersion', () => {
for ( const { version, isTesting } of testCases ) {
it( `should return ${ isTesting } for "${ version }"`, () => {
expect( isCKCdnTestingVersion( version ) ).toBe( isTesting );
} );
}
} );

describe( 'isCKCdnVersion', () => {
for ( const { version, isVersion } of testCases ) {
it( `should return ${ isVersion } for "${ version }"`, () => {
expect( isCKCdnVersion( version ) ).toBe( isVersion );
} );
}
} );
24 changes: 24 additions & 0 deletions tests/cdn/loadCKEditorCloud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,37 @@ describe( 'loadCKEditorCloud', () => {
removeAllCkCdnResources();

vi.spyOn( console, 'error' ).mockImplementation( () => undefined );
vi.spyOn( console, 'warn' ).mockImplementation( () => undefined );

window.FakePlugin = { fake: 'fake' };
} );

afterEach( () => {
vi.restoreAllMocks();
} );

for ( const version of [ 'alpha', 'internal' ] as const ) {
it( `should raise warning if ${ version } version is used`, async () => {
const { CKEditor } = await loadCKEditorCloud( {
version
} );

expect( CKEditor.ClassicEditor ).toBeDefined();
expect( console.warn ).toBeCalledWith(
'You are using a testing version of CKEditor 5. Please remember that it is not suitable for production environments.'
);
} );
}

it( 'should not raise a warning if non-testing version is passed', async () => {
const { CKEditor } = await loadCKEditorCloud( {
version: '43.0.0'
} );

expect( CKEditor.ClassicEditor ).toBeDefined();
expect( console.warn ).not.toBeCalled();
} );

it( 'should be possible to load base ckeditor with base features', async () => {
const { CKEditor, CKBox, CKEditorPremiumFeatures } = await loadCKEditorCloud( {
version: '43.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ describe( 'getSupportedLicenseVersionInstallationInfo', () => {
mockEditorVersion( '37.0.0' );
expect( getSupportedLicenseVersionInstallationInfo() ).toEqual( 1 );
} );

it( 'should return license V3 for test versions', () => {
for ( const version of [ 'alpha', 'nightly', 'internal' ] ) {
mockEditorVersion( version );
expect( getSupportedLicenseVersionInstallationInfo() ).toEqual( 3 );
}
} );
} );

function mockEditorVersion( version: string ): void {
Expand Down

0 comments on commit 518448b

Please sign in to comment.