Skip to content

Commit

Permalink
[Danger Runner] Ensure that Danger does not leave changes to the Dang…
Browse files Browse the repository at this point in the history
…erfile around
  • Loading branch information
orta committed Jan 20, 2017
1 parent 12ecc57 commit b311ed4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ then edit the local `Dangerfile.js` and run `yarn run danger pr https://github.c

This will post the results to your console, instead of on the PR itself.

* Danger changes to your Dangerfile are not persisted after the run - orta
* Add summary comment for danger message - kwonoj
* Add `jest-environment-node` to the Package.json - orta

Expand Down
27 changes: 25 additions & 2 deletions source/runner/DangerfileRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,35 @@ export async function runDangerfileEnvironment(filename: Path, environment: Dang
const runtime = environment.runtime
// Require our dangerfile

updateDangerfile(filename)
runtime.requireModule(filename)
ensureCleanDangerfile(filename, () => {
runtime.requireModule(filename)
})

return environment.context.results
}

/**
* Passes in a dangerfile path, will remove any references to import/require `danger`
* then runs the internal closure with a "safe" version of the Dangerfile.
* Then it will clean itself up afterwards, and use the new version.
*
* Note: We check for equality to not trigger the jest watcher for tests.
*
* @param {string} filename the file path for the dangerfile
* @param {Function} closure code to run with a cleaned Dangerfile
* @returns {void}
*/
function ensureCleanDangerfile(filename: string, closure: Function) {
const originalContents = fs.readFileSync(filename).toString()
updateDangerfile(filename)

closure()

if (originalContents !== fs.readFileSync(filename).toString()) {
fs.writeFileSync(filename, originalContents)
}
}

/**
* Updates a Dangerfile to remove the import for Danger
* @param {string} filename the file path for the dangerfile
Expand Down

0 comments on commit b311ed4

Please sign in to comment.