Skip to content

Commit

Permalink
Merge pull request #123 from arethetypeswrong/dt-utils
Browse files Browse the repository at this point in the history
Add `resolveImplementationPackageForTypesPackage` util and recognize tsup
  • Loading branch information
andrewbranch authored Dec 18, 2023
2 parents 6c014d8 + 2ccf898 commit ed1cd2a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-ants-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arethetypeswrong/core": patch
---

Add tsup to recognized build tools
5 changes: 5 additions & 0 deletions .changeset/green-crews-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arethetypeswrong/core": patch
---

Add `resolveImplementationPackageForTypesPackage` util
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $ attw @reduxjs__toolkit@2.0.0-beta.0.tgz -f table-flipped
Build tools:
- typescript@~4.9
- @microsoft/api-extractor@^7.13.2
- tsup@^6.7.0
🎭 Import resolved to a CommonJS type declaration file, but an ESM JavaScript file. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md
Expand Down
88 changes: 51 additions & 37 deletions packages/core/src/createPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,10 @@ export async function createPackageFromNpm(
throw new Error(parsed.error);
}
const packageName = parsed.data.name;
const specs =
const { tarballUrl, packageVersion } =
parsed.data.versionKind === "none" && typeof definitelyTyped === "string"
? [
parsePackageSpec(`${packageName}@${major(definitelyTyped)}.${minor(definitelyTyped)}`).data!,
parsePackageSpec(`${packageName}@${major(definitelyTyped)}`).data!,
parsePackageSpec(`${packageName}@latest`).data!,
]
: [parsed.data];
const { tarballUrl, packageVersion } = await getNpmTarballUrl(specs, before);
? await resolveImplementationPackageForTypesPackage(packageName, definitelyTyped, { before })
: await getNpmTarballUrl([parsed.data], before);
const pkg = await createPackageFromTarballUrl(tarballUrl);
if (!definitelyTyped || pkg.containsTypes()) {
return pkg;
Expand Down Expand Up @@ -143,43 +138,62 @@ export async function createPackageFromNpm(
return pkg;
}

export async function resolveImplementationPackageForTypesPackage(
typesPackageName: string,
typesPackageVersion: string,
options?: Pick<CreatePackageFromNpmOptions, "before">,
): Promise<ResolvedPackageId> {
if (!typesPackageName.startsWith("@types/")) {
throw new Error(`'resolveImplementationPackageForTypesPackage' expects an @types package name and version`);
}
const packageName = ts.unmangleScopedPackageName(typesPackageName.slice("@types/".length));
return getNpmTarballUrl(
[
parsePackageSpec(`${packageName}@${major(typesPackageVersion)}.${minor(typesPackageVersion)}`).data!,
parsePackageSpec(`${packageName}@${major(typesPackageVersion)}`).data!,
parsePackageSpec(`${packageName}@latest`).data!,
],
options?.before,
);
}

export async function resolveTypesPackageForPackage(
packageName: string,
packageVersion: string,
before?: Date,
): Promise<{ packageName: string; packageVersion: string; tarballUrl: string } | undefined> {
): Promise<ResolvedPackageId | undefined> {
const typesPackageName = ts.getTypesPackageName(packageName);
try {
return {
packageName: typesPackageName,
...(await getNpmTarballUrl(
[
{
name: typesPackageName,
versionKind: "range",
version: `${major(packageVersion)}.${minor(packageVersion)}`,
},
{
name: typesPackageName,
versionKind: "range",
version: `${major(packageVersion)}`,
},
{
name: typesPackageName,
versionKind: "tag",
version: "latest",
},
],
before,
)),
};
return await getNpmTarballUrl(
[
{
name: typesPackageName,
versionKind: "range",
version: `${major(packageVersion)}.${minor(packageVersion)}`,
},
{
name: typesPackageName,
versionKind: "range",
version: `${major(packageVersion)}`,
},
{
name: typesPackageName,
versionKind: "tag",
version: "latest",
},
],
before,
);
} catch {}
}

async function getNpmTarballUrl(
packageSpecs: readonly ParsedPackageSpec[],
before?: Date,
): Promise<{ tarballUrl: string; packageVersion: string }> {
export interface ResolvedPackageId {
packageName: string;
packageVersion: string;
tarballUrl: string;
}

async function getNpmTarballUrl(packageSpecs: readonly ParsedPackageSpec[], before?: Date): Promise<ResolvedPackageId> {
const fetchPackument = packageSpecs.some(
(spec) => spec.versionKind === "range" || (spec.versionKind === "tag" && spec.version !== "latest"),
);
Expand Down Expand Up @@ -227,7 +241,7 @@ async function getNpmTarballUrl(
}

if (packageVersion && tarballUrl) {
return { packageVersion, tarballUrl };
return { packageName: packageSpec.name, packageVersion, tarballUrl };
}
}
throw new Error(`Failed to find a matching version for ${packageSpecs[0].name}`);
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export type BuildTool =
| "microbundle"
| "@microsoft/api-extractor"
| "tshy"
| "@rspack/cli";
| "@rspack/cli"
| "tsup";

export interface Analysis {
packageName: string;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,5 @@ export const allBuildTools = Object.keys({
"@microsoft/api-extractor": true,
tshy: true,
"@rspack/cli": true,
tsup: true,
} satisfies Record<BuildTool, any>) as BuildTool[];
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"buildTools": {
"typescript": "~4.9",
"@microsoft/api-extractor": "^7.13.2"
"@microsoft/api-extractor": "^7.13.2",
"tsup": "^6.7.0"
},
"entrypoints": {
"./package.json": {
Expand Down

0 comments on commit ed1cd2a

Please sign in to comment.