Skip to content

Commit

Permalink
Use the end of a process to indicate that the results are ready to se…
Browse files Browse the repository at this point in the history
…nd to the host process - fixes #400
  • Loading branch information
orta committed Oct 22, 2017
1 parent 3634e93 commit 4c0f3a1
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 96 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"lodash.includes": "^4.3.0",
"lodash.isobject": "^2.4.1",
"lodash.keys": "^4.0.8",
"node-cleanup": "^2.1.2",
"node-fetch": "^1.6.3",
"parse-diff": "^0.4.0",
"parse-link-header": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion scripts/run-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const chalk = require("chalk")
const expect = require("expect")

// Toggle this on to update the JSON files for each run
const writeResults = false
const writeResults = true

const runnerFileJS = "distribution/commands/danger-runner.js"

Expand Down
1 change: 1 addition & 0 deletions source/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare module "*/package.json"

declare module "require-from-string"
declare module "node-eval"
declare module "node-cleanup"

// declare module "require-from-string" {
// export interface RequireOptions {
Expand Down
67 changes: 19 additions & 48 deletions source/commands/danger-runner.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs"
import setSharedArgs from "./utils/sharedDangerfileArgs"
import * as nodeCleanup from "node-cleanup"

import * as program from "commander"
import * as getSTDIN from "get-stdin"
import * as chalk from "chalk"

import { contextForDanger } from "../runner/Dangerfile"
import inline from "../runner/runners/inline"
import { Executor } from "../runner/Executor"
import { getPlatformForEnv } from "../platforms/platform"
import getRuntimeCISource from "./utils/getRuntimeCISource"
import { dangerfilePath } from "./utils/file-utils"
import { DangerDSLJSONType } from "../dsl/DangerDSL"
import { jsonToDSL } from "../runner/jsonToDSL"
Expand All @@ -25,64 +24,36 @@ program

setSharedArgs(program).parse(process.argv)

const app = (program as any) as SharedCLI
let foundDSL = false
let runtimeEnv = {} as any

const run = async (jsonString: string) => {
console.log(jsonString)

foundDSL = true
const dslJSON = JSON.parse(jsonString) as { danger: DangerDSLJSONType }
const dsl = await jsonToDSL(dslJSON.danger)
const dangerFile = dangerfilePath(program)

// Set up the runtime env
const context = contextForDanger(dsl)
const runtimeEnv = await inline.createDangerfileRuntimeEnvironment(context)
const results = await inline.runDangerfileEnvironment(dangerFile, undefined, runtimeEnv)
console.log(results)

const config = {
stdoutOnly: app.textOnly,
jsonOnly: true,
verbose: app.verbose,
}

const source = await getRuntimeCISource(app)
const platform = getPlatformForEnv(process.env, source!)

const exec = new Executor(source!, platform, inline, config)
await exec.handleResults(results)
runtimeEnv = await inline.createDangerfileRuntimeEnvironment(context)
await inline.runDangerfileEnvironment(dangerFile, undefined, runtimeEnv)
}

// getSTDIN().then(run)

const stdin = process.stdin

stdin.setEncoding("utf8")

let foundDSL = false

stdin.on("readable", () => {
const data = stdin.read()
if (data) {
const trimmed = data.toString().trim()
console.log("> ", trimmed)
if (trimmed.startsWith("{") && trimmed.endsWith("}") && trimmed.includes("danger")) {
console.log(">>>")
console.log(trimmed)
console.log(">>>")
foundDSL = true
run(trimmed)
}
// Wait till the end of the process to print out the results
nodeCleanup(() => {
if (foundDSL) {
process.stdout.write(JSON.stringify(runtimeEnv.results, null, 2))
}
})

stdin.on("end", () => {
console.log("stdend")
})

// Add a timeout so that CI doesn't run forever if something has broken.
setTimeout(() => {
if (!foundDSL) {
console.error(chalk.red("Timeout: Failed to get the Danger DSL after 10 seconds"))
console.error(chalk.red("Timeout: Failed to get the Danger DSL after 1 second"))
process.exitCode = 1
process.exit(1)
}
}, 10000)
}, 1000)

// Start waiting on STDIN for the DSL
getSTDIN().then(run)
2 changes: 1 addition & 1 deletion source/dsl/DangerResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ export interface DangerRuntimeContainer extends DangerResults {
/**
* Asynchronous functions to be run after parsing
*/
scheduled: any[]
scheduled?: any[]
}
2 changes: 1 addition & 1 deletion source/runner/Dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function contextForDanger(dsl: DangerDSLType): DangerContext {
scheduled: [],
}

const schedule = (fn: any) => results.scheduled.push(fn)
const schedule = (fn: any) => results.scheduled && results.scheduled.push(fn)
const fail = (message: MarkdownString) => results.fails.push({ message })
const warn = (message: MarkdownString) => results.warnings.push({ message })
const message = (message: MarkdownString) => results.messages.push({ message })
Expand Down
8 changes: 4 additions & 4 deletions source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ export class Executor {
if (this.options.jsonOnly) {
// Format for Danger Process
const results = {
failures: fails,
warnings: warnings,
messages: messages,
markdowns: markdowns,
fails,
warnings,
messages,
markdowns,
}
process.stdout.write(JSON.stringify(results, null, 2))
} else {
Expand Down
3 changes: 3 additions & 0 deletions source/runner/_tests/fixtures/__DangerfileNoScheduledAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*eslint-disable */

setTimeout(() => warn("Totally Async"), 1000)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"failures": [],
"fails": [],
"warnings": [
{
"message": "Async Function"
Expand All @@ -9,5 +9,6 @@
}
],
"messages": [],
"markdowns": []
"markdowns": [],
"scheduled": [null]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"failures": [],
"fails": [],
"warnings": [
{
"message": "Async Function"
Expand All @@ -9,5 +9,6 @@
}
],
"messages": [],
"markdowns": []
"markdowns": [],
"scheduled": [null]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"failures": [],
"fails": [],
"warnings": [
{
"message": "Scheduled a callback"
}
],
"messages": [],
"markdowns": []
"markdowns": [],
"scheduled": [null]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"failures": [],
"fails": [],
"warnings": [],
"messages": [],
"markdowns": []
"markdowns": [],
"scheduled": []
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"failures": [
"fails": [
{
"message": "this is a failure"
}
Expand All @@ -14,5 +14,6 @@
"message": "this is a message"
}
],
"markdowns": ["this is a *markdown*"]
"markdowns": ["this is a *markdown*"],
"scheduled": []
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"failures": [],
"fails": [],
"warnings": [],
"messages": [],
"markdowns": []
"markdowns": [],
"scheduled": []
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"failures": [
"fails": [
{
"message": "Asynchronous Failure"
}
Expand All @@ -14,5 +14,6 @@
"message": "Asynchronous Message"
}
],
"markdowns": ["Asynchronous Markdown"]
"markdowns": ["Asynchronous Markdown"],
"scheduled": [{}, {}, {}, {}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"fails": [],
"warnings": [
{
"message": "Totally Async"
}
],
"messages": [],
"markdowns": [],
"scheduled": []
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"failures": [
"fails": [
{
"message":
"@types dependencies were added to package.json, as a dependency for others.<br/><i>You need to move &#64;types/danger into \"devDependencies\"?</i>"
}
],
"warnings": [],
"messages": [],
"markdowns": []
"markdowns": [],
"scheduled": []
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"failures": [],
"fails": [],
"warnings": [
{
"message": "Asynchronous Warning"
}
],
"messages": [],
"markdowns": []
"markdowns": [],
"scheduled": [{}]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"failures": [],
"fails": [],
"warnings": [],
"messages": [
{
"message": "Honey, we got Types"
}
],
"markdowns": []
"markdowns": [],
"scheduled": []
}
24 changes: 3 additions & 21 deletions source/runner/runners/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,11 @@ export async function runDangerfileEnvironment(

_require(compiled, filename, {})

const results = environment.results
await Promise.all(
results.scheduled.map((fnOrPromise: any) => {
if (fnOrPromise instanceof Promise) {
return fnOrPromise
}
if (fnOrPromise.length === 1) {
// callback-based function
return new Promise(res => fnOrPromise(res))
}
return fnOrPromise()
})
)

return {
fails: results.fails,
warnings: results.warnings,
messages: results.messages,
markdowns: results.markdowns,
}
return environment.results
} catch (error) {
console.error("Unable to evaluate the Dangerfile")
return resultsForCaughtError(filename, content, error)
environment.results = resultsForCaughtError(filename, content, error)
return environment.results
}
}

Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3622,6 +3622,10 @@ netrc@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/netrc/-/netrc-0.1.4.tgz#6be94fcaca8d77ade0a9670dc460914c94472444"

node-cleanup@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/node-cleanup/-/node-cleanup-2.1.2.tgz#7ac19abd297e09a7f72a71545d951b517e4dde2c"

node-fetch@^1.6.3:
version "1.6.3"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.6.3.tgz#dc234edd6489982d58e8f0db4f695029abcd8c04"
Expand Down

0 comments on commit 4c0f3a1

Please sign in to comment.