Skip to content

Commit

Permalink
Merge pull request #139 from danger/21-danger-pr-repl
Browse files Browse the repository at this point in the history
Add support for `danger pr --repl`
  • Loading branch information
orta committed Feb 1, 2017
2 parents 6ad3916 + a596366 commit b984983
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// Add your own contribution below

* Add `danger pr --repl`, which drops into a Node.js REPL after evaluating the dangerfile - macklinu

### 0.11.0 - 0.11.2

* Add support for [Docker Cloud](https://cloud.docker.com) - camacho
Expand Down
45 changes: 43 additions & 2 deletions source/commands/danger-pr.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import * as program from "commander"
import * as debug from "debug"
import * as fs from "fs"
import * as repl from "repl"
import * as jsome from "jsome"

import { FakeCI } from "../ci_source/providers/Fake"
import { GitHub } from "../platforms/GitHub"
import { Executor } from "../runner/Executor"
import { pullRequestParser } from "../platforms/github/pullRequestParser"
import { runDangerfileEnvironment } from "../runner/DangerfileRunner"
import { DangerContext } from "../runner/Dangerfile"

const d = debug("danger:pr")

program
.option("-d, --dangerfile [filePath]", "Specify custom dangefile other than default dangerfile.js")
.option("-d, --dangerfile [filePath]", "Specify custom dangerfile other than default dangerfile.js")
.option("-r, --repl", "Drop into a Node REPL after evaluating the dangerfile")
.parse(process.argv)

const dangerFile = (program as any).dangerfile || "dangerfile.js"
Expand Down Expand Up @@ -61,5 +64,43 @@ async function runDanger(source: FakeCI, platform: GitHub, file: string) {

const runtimeEnv = await exec.setupDanger()
const results = await runDangerfileEnvironment(file, runtimeEnv)
jsome(results)
if (program["repl"]) {
openRepl(runtimeEnv.context)
} else {
jsome(results)
}
}

function openRepl(dangerContext: DangerContext): void {
/**
* Injects a read-only, global variable into the REPL
*
* @param {repl.REPLServer} repl The Node REPL created via `repl.start()`
* @param {string} name The name of the global variable
* @param {*} value The value of the global variable
*/
function injectReadOnlyProperty(repl: repl.REPLServer, name: string, value: any) {
Object.defineProperty(repl["context"], name, {
configurable: false,
enumerable: true,
value
})
}

/**
* Sets up the Danger REPL with `danger` and `results` global variables
*
* @param {repl.REPLServer} repl The Node REPL created via `repl.start()`
*/
function setup(repl: repl.REPLServer) {
injectReadOnlyProperty(repl, "danger", dangerContext.danger)
injectReadOnlyProperty(repl, "results", dangerContext.results)
}

const dangerRepl = repl.start({ prompt: "> " })
setup(dangerRepl)
dangerRepl.on("exit", () => process.exit())
// Called when `.clear` is executed in the Node REPL
// This ensures that `danger` and `results` are not cleared from the REPL context
dangerRepl.on("reset", () => setup(dangerRepl))
}

0 comments on commit b984983

Please sign in to comment.