forked from danger/danger-js
/
danger-dts.ts
108 lines (91 loc) · 2.97 KB
/
danger-dts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as fs from "fs"
import * as prettier from "prettier"
const mapLines = (s: string, func: (s: string) => string) => s.split("\n").map(func).join("\n")
const createDTS = () => {
const header = `//
// Autogenerated from scripts/danger-dts.ts
//
import { Octokit as GitHub } from "@octokit/rest"
import { Gitlab } from "@gitbeaker/node"
import { File } from "parse-diff"
`
const footer = ``
let fileOutput = ""
const dslFiles = fs
.readdirSync("source/dsl")
.filter((f) => !f.startsWith("_tests"))
.map((f) => `source/dsl/${f}`)
dslFiles.forEach((file) => {
// Sometimes they have more stuff, in those cases
// offer a way to crop the file.
const content = fs.readFileSync(file).toString()
if (content.includes("/// End of Danger DSL definition")) {
fileOutput += content.split("/// End of Danger DSL definition")[0]
} else {
fileOutput += content
}
fileOutput += "\n"
})
// The definition of all the exposed vars is inside
// the Dangerfile.js file.
const allDangerfile = fs.readFileSync("source/runner/Dangerfile.ts").toString()
const moduleContext = allDangerfile
.split("/// Start of Danger DSL definition")[1]
.split("/// End of Danger DSL definition")[0]
// we need to add either `declare function` or `declare var` to the interface
const context = mapLines(moduleContext, (line: string) => {
if (line.length === 0 || line.includes("*")) {
const newLine = line.trim()
// Make sure TSLint passes
if (newLine.startsWith("*")) {
return " " + newLine
}
return newLine
}
if (line.includes("export type")) {
return line
}
if (line.includes("(")) {
return "declare function " + line.trim()
}
if (line.includes(":")) {
return "declare const " + line.trim()
}
return ""
})
fileOutput += context
const chainDefs = fs.readFileSync("distribution/commands/utils/chainsmoker.d.ts", "utf8")
const chainDefsMinusDefaultExport = chainDefs
.split("\n")
.filter((line) => {
return !line.startsWith("export default function")
})
.join("\n")
fileOutput += chainDefsMinusDefaultExport
// Remove all JS-y bits
fileOutput = fileOutput
.split("\n")
.filter((line) => {
return !line.startsWith("import") && !line.includes("* @type ")
})
.filter((line) => {
return !line.includes("Please don't have")
})
.join("\n")
const trimmedWhitespaceLines = fileOutput.replace(/\n\s*\n\s*\n/g, "\n")
const noRedundantExports = trimmedWhitespaceLines
.replace(/export interface/g, "interface")
.replace(/export type/g, "type")
const indentedBody = mapLines(noRedundantExports, (line) => (line.length ? line : ""))
const def = header + indentedBody + footer
return prettier.format(def, {
parser: "typescript",
printWidth: 120,
semi: false,
singleQuote: false,
trailingComma: "es5",
bracketSpacing: true,
proseWrap: "always",
})
}
export default createDTS