diff --git a/dangerfile.ts b/dangerfile.ts index f1d544d35..5d1838676 100644 --- a/dangerfile.ts +++ b/dangerfile.ts @@ -17,6 +17,8 @@ declare function schedule(promise: Promise): void declare function schedule(promise: () => Promise): void declare function schedule(callback: (resolve: any) => void): void +warn("this worked") + import * as fs from "fs" schedule(async () => { diff --git a/docs/usage/danger-process.md b/docs/usage/danger-process.md index a4ff4f798..51beedf07 100644 --- a/docs/usage/danger-process.md +++ b/docs/usage/danger-process.md @@ -38,7 +38,7 @@ danger pr https://github.com/danger/danger-js/pull/395 --json > danger-js-395.ds This will work for any open repo, and if you've set up your local shell to include `DANGER_GITHUB_API_TOKEN` then you can use this with any private repository too. The JSON schema is documented in Danger JS's types as [DangerJSONDSLType][]. I -plan to add a full reference for this, similar to the reference for the user's DSL in the future in these docs. +plan to add a full reference for this, similar to the reference for the user's DSL in the future in these docs. _Note"_ The JSON will include your access token, so you probably want to sanitze that before commiting it to the repo. A runner can output anything during the process to STDOUT, and it will be logged to the user. However, Danger JS is listening for a JSON response in this format: diff --git a/scripts/danger_runner.rb b/scripts/danger_runner.rb index 62d88e9fe..36727bd3f 100755 --- a/scripts/danger_runner.rb +++ b/scripts/danger_runner.rb @@ -1,10 +1,10 @@ #!/usr/bin/env ruby -str = STDIN.tty? ? 'Cannot read from STDIN' : $stdin.read +str = STDIN.tty? ? "Cannot read from STDIN" : $stdin.read exit(1) unless str # Have a dumb fake response -require 'json' +require "json" results = { fails: [], warnings: [], messages: [], markdowns: [] }.to_json STDOUT.write(results) diff --git a/source/commands/danger-process.ts b/source/commands/danger-process.ts index c26dc58e0..1269a00fa 100644 --- a/source/commands/danger-process.ts +++ b/source/commands/danger-process.ts @@ -39,10 +39,7 @@ if (process.env["DANGER_VERBOSE"] || app.verbose) { global.verbose = true } -// a dirty wrapper to allow async functionality in the setup -async function run() { - const source = await getRuntimeCISource(app) - +getRuntimeCISource(app).then(source => { // This does not set a failing exit code if (source && !source.isPR) { console.log("Skipping Danger due to not this run not executing on a PR.") @@ -66,18 +63,17 @@ async function run() { jsonOnly: false, } - const dangerDSL = await jsonDSLGenerator(platform) - const processInput = prepareDangerDSL(dangerDSL) - - if (!subprocessName) { - // Just pipe it out to the CLI - process.stdout.write(processInput) - } else { - const exec = new Executor(source, platform, inlineRunner, config) - runDangerSubprocess(subprocessName, processInput, exec) - } + jsonDSLGenerator(platform).then(dangerJSONDSL => { + const processInput = prepareDangerDSL(dangerJSONDSL) + + if (!subprocessName) { + // Just pipe it out to the CLI + process.stdout.write(processInput) + } else { + const exec = new Executor(source, platform, inlineRunner, config) + runDangerSubprocess([subprocessName], processInput, exec) + } + }) } } -} - -run() +}) diff --git a/source/commands/danger-run.ts b/source/commands/danger-run.ts index 8409dc900..9bb0b3fe0 100755 --- a/source/commands/danger-run.ts +++ b/source/commands/danger-run.ts @@ -1,36 +1,28 @@ import * as chalk from "chalk" import * as program from "commander" -import * as debug from "debug" import { getPlatformForEnv } from "../platforms/platform" import { Executor } from "../runner/Executor" -import { dangerfilePath } from "./utils/file-utils" +import runDangerSubprocess, { prepareDangerDSL } from "./utils/runDangerSubprocess" import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs" -import validateDangerfileExists from "./utils/validateDangerfileExists" import getRuntimeCISource from "./utils/getRuntimeCISource" -import inlineRunner from "../runner/runners/inline" -const d = debug("danger:run") -declare const global: any +import inlineRunner from "../runner/runners/inline" +import { jsonDSLGenerator } from "../runner/dslGenerator" +import dangerRunToRunnerCLI from "./utils/dangerRunToRunnerCLI" program.usage("[options]").description("Runs a Dangerfile in JavaScript or TypeScript.") setSharedArgs(program).parse(process.argv) const app = (program as any) as SharedCLI -if (process.env["DANGER_VERBOSE"] || app.verbose) { - global.verbose = true -} - -// a dirty wrapper to allow async functionality in the setup -async function run() { - const source = await getRuntimeCISource(app) - +getRuntimeCISource(app).then(source => { + // This does not set a failing exit code if (source && !source.isPR) { - // This does not set a failing exit code console.log("Skipping Danger due to not this run not executing on a PR.") } + // The optimal path if (source && source.isPR) { const platform = getPlatformForEnv(process.env, source) if (!platform) { @@ -42,27 +34,19 @@ async function run() { } if (platform) { - d(`${chalk.bold("OK")}, everything looks good: ${source.name} on ${platform.name}`) - const dangerFile = dangerfilePath(program) - - const exists = validateDangerfileExists(dangerFile) - if (!exists) { - console.error(chalk.red(`Looks like your path '${dangerFile}' is not a valid path for a Dangerfile.`)) - process.exitCode = 1 - } else { - d(`executing dangerfile at ${dangerFile}`) - + jsonDSLGenerator(platform).then(dangerJSONDSL => { const config = { stdoutOnly: app.textOnly, verbose: app.verbose, jsonOnly: false, } + const processInput = prepareDangerDSL(dangerJSONDSL) + + const runnerCommand = dangerRunToRunnerCLI(process.argv) const exec = new Executor(source, platform, inlineRunner, config) - exec.setupAndRunDanger(dangerFile) - } + runDangerSubprocess(runnerCommand, processInput, exec) + }) } } -} - -run() +}) diff --git a/source/commands/utils/_tests/dangerRunToRunnerCLI.test.ts b/source/commands/utils/_tests/dangerRunToRunnerCLI.test.ts new file mode 100644 index 000000000..3a5358382 --- /dev/null +++ b/source/commands/utils/_tests/dangerRunToRunnerCLI.test.ts @@ -0,0 +1,41 @@ +import dangerRunToRunnerCLI from "../dangerRunToRunnerCLI" + +describe("it can handle the command", () => { + it("`danger`", () => { + expect(dangerRunToRunnerCLI(["danger"])).toEqual("danger runner") + }) + + it("`danger --dangerfile myDangerfile.ts`", () => { + expect(dangerRunToRunnerCLI(["danger", "--dangerfile", "myDangerfile.ts"])).toEqual( + "danger runner --dangerfile myDangerfile.ts".split(" ") + ) + }) + + it("`danger run`", () => { + expect(dangerRunToRunnerCLI(["danger", "run"])).toEqual("danger runner".split(" ")) + }) + + it("`danger run --dangerfile myDangerfile.ts`", () => { + expect(dangerRunToRunnerCLI(["danger", "run", "--dangerfile", "myDangerfile.ts"])).toEqual( + "danger runner --dangerfile myDangerfile.ts".split(" ") + ) + }) + + it("`node distribution/commands/danger-run.js`", () => { + expect(dangerRunToRunnerCLI(["node", "distribution/commands/danger-run.js"])).toEqual( + "node distribution/commands/danger-runner.js".split(" ") + ) + }) + + it("`node distribution/commands/danger-run.js --dangerfile myDangerfile.ts`", () => { + expect( + dangerRunToRunnerCLI(["node", "distribution/commands/danger-run.js", "--dangerfile", "myDangerfile.ts"]) + ).toEqual("node distribution/commands/danger-runner.js --dangerfile myDangerfile.ts".split(" ")) + }) +}) + +it("`node distribution/commands/danger-run.js --dangerfile 'myDanger file.ts'`", () => { + expect( + dangerRunToRunnerCLI(["node", "distribution/commands/danger-run.js", "--dangerfile", "myDanger file.ts"]) + ).toEqual(["node", "distribution/commands/danger-runner.js", "--dangerfile", "myDanger file.ts"]) +}) diff --git a/source/commands/utils/dangerRunToRunnerCLI.ts b/source/commands/utils/dangerRunToRunnerCLI.ts new file mode 100644 index 000000000..c7ba06125 --- /dev/null +++ b/source/commands/utils/dangerRunToRunnerCLI.ts @@ -0,0 +1,26 @@ +const dangerRunToRunnerCLI = (argv: string[]) => { + let newCommand = [] + newCommand.push(argv[0]) + + // e.g. node --inspect distribution/commands/danger-run.js --dangerfile myDangerfile.ts + if (argv.length === 1) { + return ["danger", "runner"] + } else if (argv[0].includes("node")) { + newCommand.push(argv[1].replace("-run", "-runner")) + for (let index = 2; index < argv.length; index++) { + newCommand.push(argv[index]) + } + } else { + // e.g. danger --dangerfile + // if you do `danger run` start looking at args later + newCommand.push("runner") + let index = argv[1] === "run" ? 2 : 1 + for (; index < argv.length; index++) { + newCommand.push(argv[index]) + } + } + + return newCommand +} + +export default dangerRunToRunnerCLI diff --git a/source/commands/utils/runDangerSubprocess.ts b/source/commands/utils/runDangerSubprocess.ts index b7904f677..ab5a2e283 100644 --- a/source/commands/utils/runDangerSubprocess.ts +++ b/source/commands/utils/runDangerSubprocess.ts @@ -13,9 +13,13 @@ export const prepareDangerDSL = (dangerDSL: DangerDSLJSONType) => { return JSON.stringify(dangerDSL, null, " ") + "\n" } -// Runs the Danger process -const runDangerSubprocess = (subprocessName: string, dslJSONString: string, exec: Executor) => { - const child = spawn(subprocessName) +// Runs the Danger process, can either take a simpl +const runDangerSubprocess = (subprocessName: string[], dslJSONString: string, exec: Executor) => { + let processName = subprocessName[0] + let args = subprocessName + args.shift() // mutate and remove the first element + + const child = spawn(processName, args) let allLogs = "" child.stdin.write(dslJSONString) diff --git a/source/runner/_tests/_executor.test.ts b/source/runner/_tests/_executor.test.ts index 08ecb6390..8af52c6a0 100644 --- a/source/runner/_tests/_executor.test.ts +++ b/source/runner/_tests/_executor.test.ts @@ -7,6 +7,7 @@ import inlineRunner from "../runners/inline" const defaultConfig = { stdoutOnly: false, verbose: false, + jsonOnly: false, } describe("setup", () => {