Skip to content

Commit

Permalink
Try fix the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Jan 1, 2018
1 parent 5d49812 commit 32f832d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -30,8 +30,9 @@ matrix:
- yarn add jest
- yarn jest --outputFile test-results.json --json
- yarn run link
- echo "This is only for running the real danger on this repo"
- danger run --verbose
- echo "This is the real `danger` run on this repo"
- DEBUG="*" danger run --verbose
- DEBUG="*" danger pr https://github.com/danger/danger-js/pull/465 --verbose # To make sure it isn't broken again in a bad build

# Create some fake projects at runtime
- node_js: '7'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -83,7 +83,7 @@
"babel-preset-stage-3": "7.0.0-alpha.19",
"babel-traverse": "7.0.0-alpha.19",
"danger-plugin-jest": "^1.0.1",
"danger-plugin-yarn": "^1.0.0",
"danger-plugin-yarn": "^1.2.1",
"date-fns": "^1.28.3",
"husky": "^0.14.0",
"jest": "22.0.4",
Expand Down
1 change: 1 addition & 0 deletions source/commands/danger-pr.ts
Expand Up @@ -32,6 +32,7 @@ program
.option("-j, --js", "A more human-readable version of the JSON.")

setSharedArgs(program).parse(process.argv)
d(`Starting Danger PR`)

const app = (program as any) as App

Expand Down
13 changes: 11 additions & 2 deletions source/commands/danger-runner.ts
Expand Up @@ -4,6 +4,7 @@ import setSharedArgs from "./utils/sharedDangerfileArgs"
import * as nodeCleanup from "node-cleanup"

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

Expand All @@ -13,6 +14,8 @@ import { dangerfilePath } from "./utils/file-utils"
import { DangerDSLJSONType } from "../dsl/DangerDSL"
import { jsonToDSL } from "../runner/jsonToDSL"

const d = debug("danger:run")

// Given the nature of this command, it can be tricky to test, so I use a command like this:
//
// tslint:disable-next-line:max-line-length
Expand All @@ -21,15 +24,19 @@ import { jsonToDSL } from "../runner/jsonToDSL"
// Which will build danger, then run just the dangerfile runner with a fixtured version of the JSON

program
.usage("[options] dangerfile")
.description("Handles running the Dangerfile, expects a DSL from STDIN, which should be passed from `danger run`.")
.usage("[options]")
.description(
"Handles running the Dangerfile, expects a DSL from STDIN, which should be passed from `danger` or danger run`. You probably don;t need to use this command."
)

setSharedArgs(program).parse(process.argv)
d(`Started Danger Run with ${program.args}`)

let foundDSL = false
let runtimeEnv = {} as any

const run = async (jsonString: string) => {
d("Got STDIN for Danger Run")
foundDSL = true
const dslJSON = JSON.parse(jsonString) as { danger: DangerDSLJSONType }
const dsl = await jsonToDSL(dslJSON.danger)
Expand All @@ -38,11 +45,13 @@ const run = async (jsonString: string) => {
// Set up the runtime env
const context = contextForDanger(dsl)
runtimeEnv = await inline.createDangerfileRuntimeEnvironment(context)
d(`Evaluating ${dangerFile}`)
await inline.runDangerfileEnvironment(dangerFile, undefined, runtimeEnv)
}

// Wait till the end of the process to print out the results
nodeCleanup(() => {
d("Detected process has finished, sending the results back to the host process")
if (foundDSL) {
process.stdout.write(JSON.stringify(runtimeEnv.results, null, 2))
}
Expand Down
8 changes: 6 additions & 2 deletions source/commands/danger.ts
Expand Up @@ -24,11 +24,15 @@ program
.command("process", "Like `run` but lets another process handle evaluating a Dangerfile")
.command("pr", "Runs your changes against an existing PR")
.command("runner", "Runs a dangerfile against a DSL passed in via STDIN")
.command("run", "Runs danger on your local system")
.command("run", "Runs danger on your local system", { isDefault: true })

setSharedArgs(program).parse(process.argv)

const app = (program as any) as SharedCLI
if (app.args.length === 0) {

const commands = ["init", "process", "pr", "runner", "run"]
const hasSubcommand = commands.find(c => app.args.includes(c))
if (!hasSubcommand) {
d(`Running default runner`)
runRunner(app)
}
6 changes: 6 additions & 0 deletions source/commands/run/runner.ts
@@ -1,4 +1,5 @@
import chalk from "chalk"
import * as debug from "debug"

import { getPlatformForEnv, Platform } from "../../platforms/platform"
import { Executor, ExecutorOptions } from "../../runner/Executor"
Expand All @@ -11,12 +12,15 @@ import { jsonDSLGenerator } from "../../runner/dslGenerator"
import dangerRunToRunnerCLI from "../utils/dangerRunToRunnerCLI"
import { CISource } from "../../ci_source/ci_source"

const d = debug("danger:process_runner")

export interface RunnerConfig {
source: CISource
platform: Platform
}

export const runRunner = async (app: SharedCLI, config?: RunnerConfig) => {
d(`Starting sub-process run with ${app.args}`)
const source = (config && config.source) || (await getRuntimeCISource(app))

// This does not set a failing exit code
Expand Down Expand Up @@ -48,6 +52,8 @@ export const runRunner = async (app: SharedCLI, config?: RunnerConfig) => {
const processInput = prepareDangerDSL(dangerJSONDSL)

const runnerCommand = dangerRunToRunnerCLI(process.argv)
d(`Preparing to run: ${runnerCommand}`)

const exec = new Executor(source, platform, inlineRunner, config)
runDangerSubprocess(runnerCommand, processInput, exec)
}
Expand Down
15 changes: 15 additions & 0 deletions source/commands/utils/_tests/dangerRunToRunnerCLI.test.ts
Expand Up @@ -39,3 +39,18 @@ it("`node distribution/commands/danger-run.js --dangerfile 'myDanger file.ts'`",
dangerRunToRunnerCLI(["node", "distribution/commands/danger-run.js", "--dangerfile", "myDanger file.ts"])
).toEqual(["node", "distribution/commands/danger-runner.js", "--dangerfile", "myDanger file.ts"])
})

it("`node distribution/commands/danger-pr.js --dangerfile 'myDanger file.ts'`", () => {
expect(
dangerRunToRunnerCLI(["node", "distribution/commands/danger-pr.js", "--dangerfile", "myDanger file.ts"])
).toEqual(["node", "distribution/commands/danger-runner.js", "--dangerfile", "myDanger file.ts"])
})

it("`danger pr --dangerfile 'myDanger file.ts'`", () => {
expect(dangerRunToRunnerCLI(["danger", "pr", "--dangerfile", "myDanger file.ts"])).toEqual([
"danger",
"runner",
"--dangerfile",
"myDanger file.ts",
])
})
6 changes: 4 additions & 2 deletions source/commands/utils/dangerRunToRunnerCLI.ts
Expand Up @@ -3,18 +3,20 @@ const dangerRunToRunnerCLI = (argv: string[]) => {
newCommand.push(argv[0])

// e.g. node --inspect distribution/commands/danger-run.js --dangerfile myDangerfile.ts
// or node distribution/commands/danger-pr.js --dangerfile myDangerfile.ts
if (argv.length === 1) {
return ["danger", "runner"]
} else if (argv[0].includes("node")) {
newCommand.push(argv[1].replace("-run", "-runner"))
const newJSFile = argv[1].replace("-run", "-runner").replace("-pr", "-runner")
newCommand.push(newJSFile)
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
let index = argv[1] === "run" || argv[1] === "pr" ? 2 : 1
for (; index < argv.length; index++) {
newCommand.push(argv[index])
}
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Expand Up @@ -34,9 +34,9 @@
version "9.1.8"
resolved "https://registry.yarnpkg.com/@types/highlight.js/-/highlight.js-9.1.8.tgz#d227f18bcb8f3f187e16965f2444859a04689758"

"@types/jest@^21.1.9":
version "21.1.9"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-21.1.9.tgz#a913d6224e9d8a1f24908af536f65b5bf4d489bc"
"@types/jest@^22.0.0":
version "22.0.0"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-22.0.0.tgz#23009941178aad7979a9591e026f7a96054d7bc0"

"@types/json5@^0.0.29":
version "0.0.29"
Expand Down Expand Up @@ -1576,9 +1576,9 @@ danger-plugin-jest@^1.0.1:
optionalDependencies:
serve "^5.1.5"

danger-plugin-yarn@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/danger-plugin-yarn/-/danger-plugin-yarn-1.2.0.tgz#3681db67f71e65dc6dd7a0a31ef3e7f56e9b29d3"
danger-plugin-yarn@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/danger-plugin-yarn/-/danger-plugin-yarn-1.2.1.tgz#e161d898ab3ddd5a80695c38471205476af9da81"
dependencies:
date-fns "^1.28.5"
lodash.flatten "^4.4.0"
Expand Down

0 comments on commit 32f832d

Please sign in to comment.