Skip to content

Commit

Permalink
Add support for ensuring the dts passes all linters etc
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed May 7, 2017
1 parent 5658734 commit b09f6c7
Show file tree
Hide file tree
Showing 15 changed files with 723 additions and 651 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ env/development.env
docs/doc_generate/
docs/docs_generate/
docs/js_ref_dsl_docs.json

types/index.d.ts
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-3": "^6.22.0",
"date-fns": "^1.28.3",
"dtslint": "^0.1.2",
"husky": "^0.13.3",
"in-publish": "^2.0.0",
"jest": "^20.0.0",
Expand Down
3 changes: 2 additions & 1 deletion scripts/create-danger-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ const output = dts()
// This is so you can get it for this repo 👍
fs.writeFileSync("source/danger.d.ts", output)
fs.writeFileSync("distribution/danger.d.ts", output)
fs.writeFileSync("types/index.d.ts", output)

console.log("Awesome - shipped to source/danger.d.ts and distribution/danger.d.ts")
console.log("Awesome - shipped to source/danger.d.ts, distribution/danger.d.ts and types/index.d.ts")
25 changes: 18 additions & 7 deletions scripts/danger-dts.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import * as fs from "fs"

const mapLines = (s: string, func: (s: string) => string) => s.split("\n").map(func).join("\n")

const createDTS = () => {
const header = `
const header = `//
// Autogenerated from scripts/danger-dts.ts
//
import * as GitHub from "github"
declare module 'danger' {
`
const footer = `}
`

let fileOutput = ""

const extras = ["source/platforms/messaging/violation.ts"]
Expand All @@ -29,13 +36,13 @@ import * as GitHub from "github"
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 = moduleContext.split("\n").map((line: string) => {
// 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("*"))) { return line }
if (line.includes("(")) { return "declare function " + line.trim() }
if (line.includes(":")) { return "declare const " + line.trim() }
if (line.includes("(")) { return "function " + line.trim() }
if (line.includes(":")) { return "const " + line.trim() }
return ""
}).join("\n")
})

fileOutput += context

Expand All @@ -45,7 +52,11 @@ import * as GitHub from "github"
!line.includes("* @type ")
}).join("\n")

return header + fileOutput.replace(/\n\s*\n\s*\n/g, "\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}` : "")
return header + indentedBody + footer
}

export default createDTS

0 comments on commit b09f6c7

Please sign in to comment.