Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the module is a single dts file, show it in the README #261

Merged
merged 4 commits into from
May 20, 2021
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
3 changes: 3 additions & 0 deletions packages/definitions-parser/src/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 12 additions & 3 deletions packages/publisher/src/generate-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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}\``);
Expand All @@ -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))
orta marked this conversation as resolved.
Show resolved Hide resolved
lines.push("````")
}

lines.push("");
lines.push("### Additional Details");
lines.push(` * Last updated: ${new Date().toUTCString()}`);
Expand Down
34 changes: 29 additions & 5 deletions packages/publisher/test/generate-packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down