Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/@types/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ export interface GithubRepo {
head: string;
}

export interface TsType {
ts: 'definitely-typed' | 'included' | false;
definitelyTyped: string | null;
}
export type TsType =
| {
ts: 'included' | false | { possible: true; dtsMain: string };
}
| {
ts: 'definitely-typed';
definitelyTyped: string;
};

export type ModuleType = 'esm' | 'cjs' | 'unknown';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as npm from '../npm';
import { getTypeScriptSupport } from '../typescriptSupport.js';
import { getTypeScriptSupport } from '../typescriptSupport';
import { fileExistsInUnpkg } from '../unpkg';

jest.mock('../npm');
Expand All @@ -10,17 +10,20 @@ describe('getTypeScriptSupport()', () => {
const typesSupport = await getTypeScriptSupport({
name: 'Has Types',
types: { ts: 'included' },
version: '1.0.0',
});

expect(typesSupport).toEqual({ types: { ts: 'included' } });
});

describe('without types/typings', () => {
it('Checks for @types/[name]', async () => {
// @ts-expect-error
npm.validatePackageExists.mockResolvedValue(true);
const atTypesSupport = await getTypeScriptSupport({
name: 'my-lib',
types: { ts: false },
version: '1.0.0',
});
expect(atTypesSupport).toEqual({
types: {
Expand All @@ -31,10 +34,12 @@ describe('getTypeScriptSupport()', () => {
});

it('Checks for @types/[scope__name]', async () => {
// @ts-expect-error
npm.validatePackageExists.mockResolvedValue(true);
const atTypesSupport = await getTypeScriptSupport({
name: '@my-scope/my-lib',
types: { ts: false },
version: '1.0.0',
});
expect(atTypesSupport).toEqual({
types: {
Expand All @@ -45,23 +50,29 @@ describe('getTypeScriptSupport()', () => {
});

it('Checks for a d.ts resolved version of main', async () => {
// @ts-expect-error
npm.validatePackageExists.mockResolvedValue(false);
// @ts-expect-error
fileExistsInUnpkg.mockResolvedValue(true);

const typesSupport = await getTypeScriptSupport({
name: 'my-lib',
types: { ts: { possible: true, dtsMain: 'main.d.ts' } },
version: '1.0.0',
});
expect(typesSupport).toEqual({ types: { ts: 'included' } });
});

it('Handles not having any possible TS types', async () => {
// @ts-expect-error
npm.validatePackageExists.mockResolvedValue(false);
// @ts-expect-error
fileExistsInUnpkg.mockResolvedValue(false);

const typesSupport = await getTypeScriptSupport({
name: 'my-lib',
types: { ts: false },
version: '1.0.0',
});
expect(typesSupport).toEqual({ types: { ts: false } });
});
Expand Down
2 changes: 1 addition & 1 deletion src/saveDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getChangelogs } from './changelog';
import formatPkg from './formatPkg.js';
import * as jsDelivr from './jsDelivr';
import * as npm from './npm';
import { getTSSupport } from './typescriptSupport.js';
import { getTSSupport } from './typescriptSupport';
import { datadog } from './utils/datadog';
import { log } from './utils/log';

Expand Down
11 changes: 8 additions & 3 deletions src/typescriptSupport.js → src/typescriptSupport.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { RawPkg } from './@types/pkg';
import * as npm from './npm';
import { fileExistsInUnpkg } from './unpkg';
import { datadog } from './utils/datadog';
Expand All @@ -9,7 +10,9 @@ import { datadog } from './utils/datadog';
* - { types: { ts: "included" }} - for types shipped with the module.
*
*/
export async function getTypeScriptSupport(pkg) {
export async function getTypeScriptSupport(
pkg: Pick<RawPkg, 'name' | 'types' | 'version'>
): Promise<Pick<RawPkg, 'types'>> {
// Already calculated in `formatPkg`
if (typeof pkg.types.ts === 'string') {
return { types: pkg.types };
Expand Down Expand Up @@ -47,9 +50,11 @@ export async function getTypeScriptSupport(pkg) {
}

/**
* @param {Array<Package>} pkgs
* @param pkgs
*/
export async function getTSSupport(pkgs) {
export async function getTSSupport(
pkgs: Array<Pick<RawPkg, 'name' | 'types' | 'version'>>
): Promise<Array<Pick<RawPkg, 'types'>>> {
const start = Date.now();

const all = await Promise.all(pkgs.map(getTypeScriptSupport));
Expand Down