Codibre/anypm

View on GitHub
src/lib/get-types.ts

Summary

Maintainability
A
1 hr
Test Coverage
A
100%
import { packageExists } from './package-exists';

export async function getTypes(packages: string[], current?: Set<string>) {
    function* getPackages() {
        for (const pkg of packages) {
            if (!pkg.startsWith('@types')) {
                const typePackage = `@types/${
                    pkg.startsWith('@') ? pkg.substring(1).replace('/', '__') : pkg
                }`;
                if (
                    !packages.includes(typePackage) &&
                    (!current || current.has(typePackage))
                ) {
                    yield packageExists(typePackage).then((x) =>
                        x ? typePackage : undefined,
                    );
                }
            }
        }
    }

    return (await Promise.all(getPackages())).filter((x) => x) as string[];
}