From 97a419d42cf53db622e048a17ffece636a2337a4 Mon Sep 17 00:00:00 2001 From: Orta Date: Thu, 20 May 2021 16:18:50 +0100 Subject: [PATCH 1/4] If the module is a single dts file, show it in the README --- packages/definitions-parser/src/packages.ts | 3 +++ packages/publisher/src/generate-packages.ts | 14 +++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) 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..d315245905 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,14 @@ 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) { + const dts = typing.dtsFiles[0] + lines.push("## " + path.basename(dts)) + lines.push("```ts") + lines.push(packageFS.readFile(dts)) + lines.push("```") + } + lines.push(""); lines.push("### Additional Details"); lines.push(` * Last updated: ${new Date().toUTCString()}`); From 75fbfb454a22e0f05a1211ba4b4247e4566f25b1 Mon Sep 17 00:00:00 2001 From: Orta Date: Thu, 20 May 2021 17:42:40 +0100 Subject: [PATCH 2/4] Fix tests --- .../publisher/test/generate-packages.test.ts | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) 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)); From c6faead13266c26d7c5829c60d9f547024fd2fee Mon Sep 17 00:00:00 2001 From: Orta Date: Thu, 20 May 2021 18:06:46 +0100 Subject: [PATCH 3/4] Put a size cap on the readme length --- packages/publisher/src/generate-packages.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/publisher/src/generate-packages.ts b/packages/publisher/src/generate-packages.ts index d315245905..6cd1e15f34 100644 --- a/packages/publisher/src/generate-packages.ts +++ b/packages/publisher/src/generate-packages.ts @@ -275,9 +275,9 @@ export function createReadme(typing: TypingsData, packageFS: FS): string { lines.push("# Details"); lines.push(`Files were exported from ${definitelyTypedURL}/tree/${sourceBranch}/types/${typing.subDirectoryPath}.`); - if (typing.dtsFiles.length === 1) { + if (typing.dtsFiles.length === 1 && packageFS.readFile(typing.dtsFiles[0]).length < 2500) { const dts = typing.dtsFiles[0] - lines.push("## " + path.basename(dts)) + lines.push(`## ${typing.dtsFiles[0]}`) lines.push("```ts") lines.push(packageFS.readFile(dts)) lines.push("```") From 21f044d0cbb2ffc9cc94ef11add1c5ea35eeb9d4 Mon Sep 17 00:00:00 2001 From: Orta Date: Thu, 20 May 2021 18:44:44 +0100 Subject: [PATCH 4/4] Adds 4 backticks for the codeblock and links to the file --- packages/publisher/src/generate-packages.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/publisher/src/generate-packages.ts b/packages/publisher/src/generate-packages.ts index 6cd1e15f34..496e1e7902 100644 --- a/packages/publisher/src/generate-packages.ts +++ b/packages/publisher/src/generate-packages.ts @@ -277,10 +277,11 @@ export function createReadme(typing: TypingsData, packageFS: FS): string { if (typing.dtsFiles.length === 1 && packageFS.readFile(typing.dtsFiles[0]).length < 2500) { const dts = typing.dtsFiles[0] - lines.push(`## ${typing.dtsFiles[0]}`) - lines.push("```ts") + 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("");