Skip to content

Commit

Permalink
Add a Danger rule about updating the d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Mar 18, 2017
1 parent 54886c9 commit 77ee5cd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 47 deletions.
10 changes: 8 additions & 2 deletions dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ if (packageChanged && !lockfileChanged) {
warn(`${message} - <i>${idea}</i>`)
}

// TODO: Sync process? Or use the async operators
// Want to compare the output of `node ./scripts/create-danger-dts.js` to the current DTS
import dtsGenerator from "./scripts/danger-dts"
const currentDTS = dtsGenerator()
const savedDTS = fs.readFileSync("source/danger.d.ts").toString()
if (currentDTS !== savedDTS) {
const message = "There are changes to the Danger DSL which are not reflected in the current danger.d.ts."
const idea = "Please run `node ./scripts/create-danger-dts.js` and update this PR."
fail(`${message} - <i>${idea}</i>`)
}
52 changes: 7 additions & 45 deletions scripts/create-danger-dts.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,10 @@
var fs = require("fs")
var fileOutput = ""
const dts = require("./danger-dts")
const fs = require("fs")

const extras = ["source/platforms/messaging/violation.ts"]
const dslFiles = fs.readdirSync("source/dsl").map(f => `source/dsl/${f}`)

dslFiles.concat(extras).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 = moduleContext.split("\n").map((line) => {
if ((line.length === 0) || (line.includes("*"))) { return line }
if (line.includes("(")) { return " function " + line.trim() }
if (line.includes(":")) { return " var " + line.trim() }
}).join("\n")

fileOutput += context

// Remove all JS-y bits
fileOutput = fileOutput.split("\n").filter((line) => {
return !line.startsWith("import") &&
!line.includes("* @type ")
}).join("\n")

// We don't export in the definitions files
// fileOutput = fileOutput.replace(/export interface/gi, "interface")

// Remove any 2 line breaks
const flowTyped = fileOutput.replace(/\n\s*\n/g, "\n")
const output = dts()

// This is so you can get it for this repo 👍
fs.writeFileSync("source/danger.d.ts", flowTyped)
fs.writeFileSync("distribution/danger.d.ts", flowTyped)
console.log("Awesome - shipped to source/danger.dts")
fs.writeFileSync("source/danger.d.ts", output)
fs.writeFileSync("distribution/danger.d.ts", output)

console.log("Awesome - shipped to source/danger.d.ts and distribution/danger.d.ts")
45 changes: 45 additions & 0 deletions scripts/danger-dts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var fs = require("fs")

const createDTS = () => {
var fileOutput = ""

const extras = ["source/platforms/messaging/violation.ts"]
const dslFiles = fs.readdirSync("source/dsl").map(f => `source/dsl/${f}`)

dslFiles.concat(extras).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 = moduleContext.split("\n").map((line) => {
if ((line.length === 0) || (line.includes("*"))) { return line }
if (line.includes("(")) { return " function " + line.trim() }
if (line.includes(":")) { return " var " + line.trim() }
}).join("\n")

fileOutput += context

// Remove all JS-y bits
fileOutput = fileOutput.split("\n").filter((line) => {
return !line.startsWith("import") &&
!line.includes("* @type ")
}).join("\n")

// Remove any 2 line breaks
return fileOutput.replace(/\n\s*\n/g, "\n")
}

module.exports = createDTS

0 comments on commit 77ee5cd

Please sign in to comment.