Skip to content

Commit

Permalink
refactor cdns so Yury is happy
Browse files Browse the repository at this point in the history
  • Loading branch information
carmelc committed Aug 25, 2024
1 parent 48c1a27 commit e225153
Show file tree
Hide file tree
Showing 22 changed files with 425 additions and 325 deletions.
96 changes: 50 additions & 46 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions __tests__/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import path from 'path';

import tempy from 'tempy';
import * as ts from 'typescript';
import latestVersion from 'latest-version';
import { CDN, CdnType } from '../../src';
import { CdnFileIdentifier } from '../../src/cdn-impl/cdn-base';

const createKey = (fileIdentifier: CdnFileIdentifier) => `${fileIdentifier.packageName}@${fileIdentifier.packageVersion}/${fileIdentifier.filePath}`

export const pkg = (pkgIdentifier: string, typesContents: string, {
dependencies = {},
typesName = 'types'
} = {}) => {
const types = 'lib/types.d.ts';
const packageIdentifierParts = pkgIdentifier.split('@');
const packageName = packageIdentifierParts.slice(0, -1).join('@');
const packageVersion = packageIdentifierParts[packageIdentifierParts.length - 1];

const pkgJson = {
[createKey({
packageName,
packageVersion,
filePath: 'package.json',
})]: JSON.stringify({
name: packageName,
version: packageVersion,
[typesName]: types,
dependencies: {
...dependencies
},
})
};

const typesFile = {
[createKey({
packageName,
packageVersion,
filePath: types,
})]: typesContents,
};

return {
...pkgJson,
...typesFile,
};
};

export const getTestCdnImpl: () => CDN & {
setMockedNpmPackages: (pkgs: { [key: string]: string }) => void;
forceErrorOnce: (error: string) => void;
} = () => {

let packagesInfo: { [key: string]: string } = {};
const errorsQueue: string[] = [];

return {
name: 'test' as CdnType,
getPackageJsonPaths: async (packageName, packageVersion) => ['/package.json'],
getFileUniqueIdentifier: createKey,
fetchFromCdn: async (fileIdentifier: CdnFileIdentifier) => {
const forcedError = errorsQueue.pop();

if (forcedError) {
throw new Error(forcedError);
}

const cacheKey = createKey(fileIdentifier);
const content = packagesInfo[cacheKey];
if (content) {
return content;
}
else {
throw new Error(`File not found: ${cacheKey}`);
}
},
setMockedNpmPackages: (pkgs: { [key: string]: string }) => {
packagesInfo = {
...packagesInfo,
...pkgs,
}
},
forceErrorOnce: (error: string) => {
errorsQueue.push(error);
}
};
}


export async function getLatestVersionName(packageName: string): Promise<string> {
try {
const version = await latestVersion(packageName);
console.log(`Latest version of ${packageName}: ${version}`);
return `${packageName}@${version}`;
} catch (error) {
console.error(`Error fetching latest version for ${packageName}:`, error);
throw error;
}
}

export function validateTypescript(code: string, baseDir: string): {
isValid: boolean;
messages?: string[]
} {
const mainFilePath = path.join(baseDir, tempy.file({extension: 'ts'})); // Construct file path inside the directory
ts.sys.writeFile(mainFilePath, code);

const program = ts.createProgram([mainFilePath], {});
const emitResult = program.emit();

// Check for diagnostics (errors)
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
const diagnosticsCurrentFile = allDiagnostics.filter(diagnostic => diagnostic?.file?.fileName === mainFilePath);
return {
isValid: diagnosticsCurrentFile.length === 0,
messages: diagnosticsCurrentFile.map(diagnostic => diagnostic.messageText.toString()),
};
}
38 changes: 38 additions & 0 deletions __tests__/e2e/cdns-e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import tempy from 'tempy';
import { bundle, CdnType } from '../../src';
import fs from 'fs';

describe('Site SDK - Fixed versions', () => {
jest.setTimeout(60000);
let tempDir: string;
let siteBookingsDtsPathUnpkg: string;
let siteBookingsDtsPathJsDelivr: string;
let sdkBookingsDtsPathUnpkg: string;
let sdkBookingsDtsPathJsDelivr: string;

beforeAll(async () => {
tempDir = tempy.directory({ prefix: 'my_temp_dir_' });
siteBookingsDtsPathUnpkg = `${tempDir}/generated/site-bookings-unpkg.d.ts`;
siteBookingsDtsPathJsDelivr = `${tempDir}/generated/site-bookings-jsdelivr.d.ts`;
sdkBookingsDtsPathUnpkg = `${tempDir}/generated/sdk-bookings-unpkg.d.ts`;
sdkBookingsDtsPathJsDelivr = `${tempDir}/generated/sdk-bookings-jsdelivr.d.ts`;
await Promise.all([
bundle('@wix/[email protected]', siteBookingsDtsPathUnpkg, { cdn: CdnType.UNPKG }),
bundle('@wix/[email protected]', siteBookingsDtsPathJsDelivr, { cdn: CdnType.JSDELIVR }),
bundle('@wix/[email protected]', sdkBookingsDtsPathUnpkg, { cdn: CdnType.UNPKG }),
bundle('@wix/[email protected]', sdkBookingsDtsPathJsDelivr, { cdn: CdnType.JSDELIVR }),
]);
});

afterAll(async () => {
await fs.promises.rm(tempDir, { recursive: true });
});

it('site sdk packages should be identical between both unpkg and jsdelivr CDNs', () => {
expect(fs.readFileSync(siteBookingsDtsPathUnpkg, 'utf8')).toEqual(fs.readFileSync(siteBookingsDtsPathJsDelivr, 'utf8'));
});

it('backend sdk packages should be identical between both unpkg and jsdelivr CDNs', () => {
expect(fs.readFileSync(sdkBookingsDtsPathUnpkg, 'utf8')).toEqual(fs.readFileSync(sdkBookingsDtsPathJsDelivr, 'utf8'));
});
});
2 changes: 1 addition & 1 deletion __tests__/e2e/site-sdk-e2e.fixed-versions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tempy from 'tempy';
import { bundle } from '../../src';
import fs from 'fs';
import { validateTypescript } from './utils';
import { validateTypescript } from '../common/utils';

describe('Site SDK - Fixed versions', () => {
jest.setTimeout(60000);
Expand Down
Loading

0 comments on commit e225153

Please sign in to comment.