diff --git a/packages/definitions-parser/src/packages.ts b/packages/definitions-parser/src/packages.ts index 561f137a14..7003c0e9fd 100644 --- a/packages/definitions-parser/src/packages.ts +++ b/packages/definitions-parser/src/packages.ts @@ -573,6 +573,9 @@ export class TypingsData extends PackageBase { get files(): readonly string[] { return this.data.files; } + get dtsFiles(): readonly string[] { + return this.data.files.filter(f => f.endsWith(".d.ts")); + } get license(): License { return this.data.license; } diff --git a/packages/publisher/src/generate-packages.ts b/packages/publisher/src/generate-packages.ts index 471a313a6a..496e1e7902 100644 --- a/packages/publisher/src/generate-packages.ts +++ b/packages/publisher/src/generate-packages.ts @@ -98,13 +98,13 @@ async function generateTypingPackage( await writeCommonOutputs( typing, createPackageJSON(typing, version, packages, Registry.NPM), - createReadme(typing), + createReadme(typing, packageFS), Registry.NPM ); await writeCommonOutputs( typing, createPackageJSON(typing, version, packages, Registry.Github), - createReadme(typing), + createReadme(typing, packageFS), Registry.Github ); await Promise.all( @@ -258,7 +258,7 @@ export function createNotNeededPackageJSON( return JSON.stringify(out, undefined, 4); } -export function createReadme(typing: TypingsData): string { +export function createReadme(typing: TypingsData, packageFS: FS): string { const lines: string[] = []; lines.push("# Installation"); lines.push(`> \`npm install --save ${typing.fullNpmName}\``); @@ -275,6 +275,15 @@ export function createReadme(typing: TypingsData): string { lines.push("# Details"); lines.push(`Files were exported from ${definitelyTypedURL}/tree/${sourceBranch}/types/${typing.subDirectoryPath}.`); + if (typing.dtsFiles.length === 1 && packageFS.readFile(typing.dtsFiles[0]).length < 2500) { + const dts = typing.dtsFiles[0] + const url = `${definitelyTypedURL}/tree/${sourceBranch}/types/${typing.subDirectoryPath}/${dts}` + lines.push(`## [${typing.dtsFiles[0]}](${url})`) + lines.push("````ts") + lines.push(packageFS.readFile(dts)) + lines.push("````") + } + lines.push(""); lines.push("### Additional Details"); lines.push(` * Last updated: ${new Date().toUTCString()}`); diff --git a/packages/publisher/test/generate-packages.test.ts b/packages/publisher/test/generate-packages.test.ts index 7827949137..8dc6ee3a98 100644 --- a/packages/publisher/test/generate-packages.test.ts +++ b/packages/publisher/test/generate-packages.test.ts @@ -15,7 +15,7 @@ import { createMockDT } from "@definitelytyped/definitions-parser"; import { testo } from "./utils"; -import { Registry } from "@definitelytyped/utils"; +import { Registry, InMemoryFS, Dir, FS } from "@definitelytyped/utils"; function createRawPackage(license: License): TypingsDataRaw { return { @@ -51,6 +51,19 @@ function createTypesData(): TypesDataFile { function createUnneededPackage() { return new NotNeededPackage("absalom", "alternate", "1.1.1"); } + +function defaultFS(): FS { + const pkg = new Dir(undefined); + pkg.set( + "index.d.ts", + `type T = import("./types"); +` + ); + pkg.set("jquery.test.ts", "// tests"); + const memFS = new InMemoryFS(pkg, "types/mock"); + return memFS +} + testo({ mitLicenseText() { const typing = new TypingsData(createRawPackage(License.MIT), /*isLatest*/ true); @@ -62,21 +75,32 @@ testo({ }, basicReadme() { const typing = new TypingsData(createRawPackage(License.Apache20), /*isLatest*/ true); - expect(createReadme(typing)).toEqual(expect.stringContaining("This package contains type definitions for")); + expect(createReadme(typing, defaultFS())).toEqual(expect.stringContaining("This package contains type definitions for")); }, readmeContainsProjectName() { const typing = new TypingsData(createRawPackage(License.Apache20), /*isLatest*/ true); - expect(createReadme(typing)).toEqual(expect.stringContaining("jquery.org")); + expect(createReadme(typing, defaultFS())).toEqual(expect.stringContaining("jquery.org")); }, readmeOneDependency() { const typing = new TypingsData(createRawPackage(License.Apache20), /*isLatest*/ true); - expect(createReadme(typing)).toEqual( + expect(createReadme(typing, defaultFS())).toEqual( expect.stringContaining("Dependencies: [@types/madeira](https://npmjs.com/package/@types/madeira)") ); }, + readmeContainsSingleFileDTS() { + const typing = new TypingsData(createRawPackage(License.Apache20), /*isLatest*/ true); + expect(createReadme(typing, defaultFS())).toContain("type T = import") + }, + readmeContainsManyDTSFilesDoesNotAmendREADME() { + const rawPkg = createRawPackage(License.Apache20) + // @ts-expect-error - files is readonly + rawPkg.files = ["index.d.ts", "other.d.ts"] + const typing = new TypingsData(rawPkg, /*isLatest*/ true); + expect(createReadme(typing, defaultFS())).not.toContain("type T = import") + }, readmeNoGlobals() { const typing = new TypingsData(createRawPackage(License.Apache20), /*isLatest*/ true); - expect(createReadme(typing)).toEqual(expect.stringContaining("Global values: none")); + expect(createReadme(typing, defaultFS())).toEqual(expect.stringContaining("Global values: none")); }, basicPackageJson() { const packages = AllPackages.from(createTypesData(), readNotNeededPackages(createMockDT().fs));