|
| 1 | +import fs = require('fs') |
| 2 | +import os = require('os') |
| 3 | +import { markdown, danger } from "danger" |
| 4 | +const suggestionsDir = [os.homedir(), ".dts", "suggestions"].join('/') |
| 5 | +const lines: string[] = [] |
| 6 | +const missingProperty = /module exports a property named '(.+?)', which is missing/ |
| 7 | + |
| 8 | +if (fs.existsSync(suggestionsDir)) { |
| 9 | + lines.push('Inspecting the JavaScript source for this package found some properties that are not in the .d.ts files.') |
| 10 | + lines.push('The check for missing properties isn\'t always right, so take this list as advice, not a requirement.') |
| 11 | + for (const suggestionFile of fs.readdirSync(suggestionsDir)) { |
| 12 | + const path = [suggestionsDir, suggestionFile].join('/') |
| 13 | + const suggestions = fs.readFileSync(path, "utf8").split('\n').map(x => JSON.parse(x)) as Array<{ fileName: string, ruleName: string, message: string }> |
| 14 | + const packageName = suggestionFile.slice(0, suggestionFile.indexOf('.txt')) |
| 15 | + const missingProperties: { [s: string]: string[] } = {} |
| 16 | + for (const s of suggestions) { |
| 17 | + const fileName = s.fileName.slice(s.fileName.indexOf("types/" + packageName + "/") + ("types/" + packageName + "/").length) |
| 18 | + const match = s.message.match(missingProperty) |
| 19 | + const identifier = match ? match[1] : s.message |
| 20 | + if (fileName in missingProperties) { |
| 21 | + missingProperties[fileName].push(identifier) |
| 22 | + } |
| 23 | + else { |
| 24 | + missingProperties[fileName] = [identifier] |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const topUnpkgURL = `https://unpkg.com/browse/${packageName}@latest/`; |
| 29 | + lines.push("## " + packageName + ` ([<kbd>unpkg</kbd>](${topUnpkgURL}))`) |
| 30 | + for (const fileName in missingProperties) { |
| 31 | + if (Object.keys(missingProperties).length > 1) { |
| 32 | + const originalJS = fileName.replace(".d.ts", ".js") |
| 33 | + const unpkgURL = `https://unpkg.com/browse/${packageName}@latest/${originalJS}` |
| 34 | + const dtsName = packageName.replace("@", "").replace("/", "__") |
| 35 | + const dtsURL = `https://github.com/DefinitelyTyped/DefinitelyTyped/blob/${danger.github.pr.head.sha}/types/${dtsName}/${fileName}` |
| 36 | + |
| 37 | + lines.push(`### ${fileName} ([<kbd>unpkg</kbd>](${unpkgURL}), [<kbd>d.ts</kbd>](${dtsURL}))`); |
| 38 | + } |
| 39 | + const properties = missingProperties[fileName] |
| 40 | + lines.push(`was missing the following properties: |
| 41 | +1. ` + properties.slice(0,5).join('\n1. ')) |
| 42 | + if (properties.length > 5) { |
| 43 | + const extras = properties.slice(5) |
| 44 | + lines.push(` |
| 45 | +<details> |
| 46 | +<summary>as well as these ${extras.length} other properties...</summary> |
| 47 | +<p>${extras.join(", ")}</p> |
| 48 | +</details> |
| 49 | +`) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + markdown(lines.join('\n')) |
| 54 | +} |
| 55 | + |
0 commit comments