-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
425 additions
and
325 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.