-
Notifications
You must be signed in to change notification settings - Fork 22
feat(typescript): use Typescript index + many changes #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
3b37ab3
b4b98ef
01953ce
56478ca
13fd22d
df6a377
2bd42fc
1155b1d
6c0c9e9
3537e0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import * as api from '../index.js'; | ||
|
|
||
| describe('loadTypesIndex()', () => { | ||
| it('should download and cache all @types', async () => { | ||
| expect(api.typesCache).not.toHaveProperty('algoliasearch'); | ||
| expect(api.isDefinitelyTyped('algoliasearch')).toBe(undefined); | ||
|
|
||
| await api.loadTypesIndex(); | ||
| expect(api.typesCache).toHaveProperty('algoliasearch'); | ||
| expect(api.typesCache).toHaveProperty('algoliasearch/lite'); | ||
|
|
||
| expect(api.typesCache.algoliasearch).toBe('algoliasearch'); | ||
| expect(api.typesCache['algoliasearch/lite']).toBe('algoliasearch'); | ||
| expect(api.typesCache.doesnotexist).toBe(undefined); | ||
|
|
||
| expect(api.isDefinitelyTyped({ name: 'algoliasearch' })).toBe( | ||
| 'algoliasearch' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('checkForSupport()', () => { | ||
| it('If types are already calculated - return early', async () => { | ||
| const typesSupport = await api.checkForSupport({ | ||
| name: 'Has Types', | ||
| types: { ts: 'included' }, | ||
| }); | ||
|
|
||
| expect(typesSupport).toEqual({ types: { ts: 'included' } }); | ||
| }); | ||
|
|
||
| describe('without types/typings', () => { | ||
| it('Checks for @types/[name]', async () => { | ||
| const atTypesSupport = await api.checkForSupport({ | ||
| name: 'lodash.valuesin', | ||
| types: { ts: false }, | ||
| }); | ||
| expect(atTypesSupport).toEqual({ | ||
| types: { | ||
| ts: 'definitely-typed', | ||
| definitelyTyped: '@types/lodash.valuesin', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('Checks for @types/[scope__name]', async () => { | ||
| const atTypesSupport = await api.checkForSupport({ | ||
| name: '@mapbox/geojson-area', | ||
| types: { ts: false }, | ||
| }); | ||
| expect(atTypesSupport).toEqual({ | ||
| types: { | ||
| ts: 'definitely-typed', | ||
| definitelyTyped: '@types/mapbox__geojson-area', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('Handles not having any possible TS types', async () => { | ||
| const typesSupport = await api.checkForSupport({ | ||
| name: 'my-lib', | ||
| types: { ts: false }, | ||
| }); | ||
| expect(typesSupport).toEqual({ types: { ts: false } }); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import got from 'got'; | ||
|
|
||
| import config from '../config.js'; | ||
| import datadog from '../datadog.js'; | ||
| import log from '../log.js'; | ||
|
|
||
| const typesCache = {}; | ||
|
|
||
| /** | ||
| * Microsoft build a index.json with all @types/* on each publication | ||
| * https://github.com/microsoft/types-publisher/blob/master/src/create-search-index.ts | ||
| * | ||
| */ | ||
| async function loadTypesIndex() { | ||
| const start = Date.now(); | ||
| const { body } = await got(config.typescriptTypesIndex, { | ||
| decompress: true, | ||
| json: true, | ||
| }); | ||
|
|
||
| log.info(`📦 Typescript preload, found ${body.length} @types`); | ||
|
|
||
| // m = modules associate | ||
| // t = @types/<name> | ||
| body.forEach(type => { | ||
| type.m.forEach(m => { | ||
| typesCache[m] = type.t; | ||
|
||
| }); | ||
| }); | ||
|
|
||
| datadog.timing('typescript.loadTypesIndex', Date.now() - start); | ||
| } | ||
|
|
||
| function isDefinitelyTyped({ name }) { | ||
| return typesCache[name]; | ||
| } | ||
|
|
||
| /** | ||
| * Basically either | ||
| * - { types: { ts: false }} for no existing TypeScript support | ||
| * - { types: { ts: "@types/module" }} - for definitely typed support | ||
| * - { types: { ts: "included" }} - for types shipped with the module | ||
| * @param {Package} pkg | ||
| */ | ||
| export function checkForSupport(pkg) { | ||
| // Already calculated in `formatPkg` | ||
| if (typeof pkg.types.ts === 'string') { | ||
| return { types: pkg.types }; | ||
| } | ||
|
|
||
| // The 2nd most likely is definitely typed | ||
| const defTyped = isDefinitelyTyped({ name: pkg.name }); | ||
| if (defTyped) { | ||
| return { | ||
| types: { | ||
| ts: 'definitely-typed', | ||
| definitelyTyped: `@types/${defTyped}`, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| return { types: { ts: false } }; | ||
| } | ||
|
|
||
| /** | ||
| * Check if packages have Typescript definitions | ||
| * @param {Array<Package>} pkgs | ||
| */ | ||
| async function checkForSupportMultiple(pkgs) { | ||
| const start = Date.now(); | ||
|
|
||
| const all = await Promise.all(pkgs.map(checkForSupport)); | ||
|
|
||
| datadog.timing('getTSSupport', Date.now() - start); | ||
| return all; | ||
| } | ||
|
|
||
| export { | ||
| loadTypesIndex, | ||
| typesCache, | ||
| isDefinitelyTyped, | ||
| checkForSupportMultiple, | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.