Skip to content

Commit

Permalink
Merge pull request #277 from danger/prettier
Browse files Browse the repository at this point in the history
Adds prettier to the codebase
  • Loading branch information
orta committed Jun 9, 2017
2 parents 9899d4b + d861911 commit a45ad2f
Show file tree
Hide file tree
Showing 59 changed files with 697 additions and 522 deletions.
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@
],
"spellchecker.ignoreRegExp": [],
"spellchecker.ignoreFileExtensions": [],
"spellchecker.checkInterval": 5000
"spellchecker.checkInterval": 5000,
"editor.formatOnSave": true,
"prettier.singleQuote": false,
"prettier.trailingComma": "es5",
"prettier.typescriptEnable": [
"typescript",
"typescriptreact"
],
"prettier.semi": false,
"prettier.printWidth": 120
}
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Master

* Adds prettier to the codebase - orta

### 0.20.0

* Fix `danger pr` commands are not running on windows - kwonoj
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"lint-staged": {
"*.@(ts|tsx)": [
"tslint --fix",
"npm run prettier-write --",
"git add"
]
},
Expand All @@ -52,6 +53,9 @@
"test:watch": "jest --watch",
"lint": "tslint \"source/**/*.ts\"",
"lint:fix": "tslint \"source/**/*.ts\" --fix",
"prettier": "prettier",
"prettier-write": "npm run prettier -- --parser typescript --no-semi --trailing-comma es5 --write --print-width 120",
"prettier-project": "npm run prettier-write -- 'source/**/*.{ts,tsx}'",
"prepublish": "in-publish && npm run build && yarn declarations || not-in-publish",
"build": "shx rm -rf ./distribution && tsc && madge ./distribution --circular",
"build:watch": "tsc -w",
Expand Down Expand Up @@ -93,10 +97,12 @@
"jest": "^20.0.0",
"lint-staged": "^3.2.5",
"madge": "^1.4.4",
"prettier": "^1.4.4",
"shx": "^0.2.1",
"ts-jest": "^20.0.0",
"ts-node": "^3.0.0",
"tslint": "^5.3.0",
"tslint-config-prettier": "^1.0.0",
"typedoc": "orta/typedoc#0.5.10-no-types",
"typescript": "2.3.4"
},
Expand Down
40 changes: 28 additions & 12 deletions scripts/danger-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,44 @@ declare module "danger" {
// The definition of all the exposed vars is inside
// the Dangerfile.js file.
const allDangerfile = fs.readFileSync("source/runner/Dangerfile.ts").toString()
const moduleContext = allDangerfile.split("/// Start of Danger DSL definition")[1].split("/// End of Danger DSL definition")[0]
const moduleContext = allDangerfile
.split("/// Start of Danger DSL definition")[1]
.split("/// End of Danger DSL definition")[0]

// we need to add either `declare function` or `declare var` to the interface
// we need to add either `declare function` or `declare var` to the interface
const context = mapLines(moduleContext, (line: string) => {
if ((line.length === 0) || (line.includes("*"))) { return line }
if (line.includes("(")) { return "function " + line.trim() }
if (line.includes(":")) { return "const " + line.trim() }
if (line.length === 0 || line.includes("*")) {
const newLine = line.trim()
// Make sure TSLint passes
if (newLine.startsWith("*")) {
return " " + newLine
}
return newLine
}
if (line.includes("(")) {
return "function " + line.trim()
}
if (line.includes(":")) {
return "const " + line.trim()
}
return ""
})

fileOutput += context

// Remove all JS-y bits
fileOutput = fileOutput.split("\n").filter((line) => {
return !line.startsWith("import") &&
!line.includes("* @type ")
}).join("\n")
fileOutput = fileOutput
.split("\n")
.filter(line => {
return !line.startsWith("import") && !line.includes("* @type ")
})
.join("\n")

const trimmedWhitespaceLines = fileOutput.replace(/\n\s*\n\s*\n/g, "\n")
const noRedundantExports = trimmedWhitespaceLines.replace(/export interface/g, "interface")
.replace(/export type/g, "type")
const indentedBody = mapLines(noRedundantExports, (line) => line.length ? ` ${line}` : "")
const noRedundantExports = trimmedWhitespaceLines
.replace(/export interface/g, "interface")
.replace(/export type/g, "type")
const indentedBody = mapLines(noRedundantExports, line => (line.length ? ` ${line}` : ""))
return header + indentedBody + footer
}

Expand Down
3 changes: 1 addition & 2 deletions source/api/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export function api(url: string | node_fetch.Request, init: node_fetch.RequestIn
d(output.join(" "))
}
const originalFetch: any = node_fetch
return originalFetch(url, init)
.then(async (response: node_fetch.Response) => {
return originalFetch(url, init).then(async (response: node_fetch.Response) => {
// Handle failing errors
if (!response.ok) {
const responseJSON = await response.json()
Expand Down
3 changes: 1 addition & 2 deletions source/ci_source/ci_source.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

/** A json object that represents the outer ENV */
export type Env = any

/** Key details about a repo */
export interface RepoMetaData {
/** A path like "artsy/eigen" */
repoSlug: string,
repoSlug: string
/** The ID for the pull/merge request "11" */
pullRequestID: string
}
Expand Down
15 changes: 10 additions & 5 deletions source/ci_source/ci_source_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ export function ensureEnvKeysExist(env: Env, keys: string[]): boolean {
});
return !includes(hasKeys, false);*/

return keys.map((key: string) => env.hasOwnProperty(key) && env[key] != null && env[key].length > 0)
.filter(x => x === false).length === 0
return (
keys
.map((key: string) => env.hasOwnProperty(key) && env[key] != null && env[key].length > 0)
.filter(x => x === false).length === 0
)
}

/**
Expand All @@ -31,8 +34,10 @@ export function ensureEnvKeysAreInt(env: Env, keys: string[]): boolean {
})
return !includes(hasKeys, false);*/

return keys.map((key: string) => env.hasOwnProperty(key) && !isNaN(parseInt(env[key])))
.filter(x => x === false).length === 0
return (
keys.map((key: string) => env.hasOwnProperty(key) && !isNaN(parseInt(env[key]))).filter(x => x === false).length ===
0
)
}

/**
Expand All @@ -48,7 +53,7 @@ export async function getPullRequestIDForBranch(metadata: RepoMetaData, env: Env
return 0
}
const api = new GitHubAPI(metadata, token)
const prs = await api.getPullRequests() as any[]
const prs = (await api.getPullRequests()) as any[]
const prForBranch: GitHubPRDSL = find(prs, (pr: GitHubPRDSL) => pr.head.ref === branch)
if (prForBranch) {
return prForBranch.number
Expand Down
8 changes: 3 additions & 5 deletions source/ci_source/get_ci_source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {providers} from "./providers"
import { providers } from "./providers"
import * as fs from "fs"
import { resolve } from "path"
import { Env, CISource } from "./ci_source"
Expand All @@ -10,10 +10,8 @@ import { Env, CISource } from "./ci_source"
* @returns {?CISource} a CI source if it's OK, otherwise Danger can't run.
*/
export function getCISourceForEnv(env: Env): CISource | undefined {
const availableProviders = [...providers as any]
.map(Provider => new Provider(env))
.filter(x => x.isCI)
return (availableProviders && availableProviders.length > 0) ? availableProviders[0] : undefined
const availableProviders = [...(providers as any)].map(Provider => new Provider(env)).filter(x => x.isCI)
return availableProviders && availableProviders.length > 0 ? availableProviders[0] : undefined
}

/**
Expand Down
21 changes: 14 additions & 7 deletions source/ci_source/providers/Buildkite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers"

export class Buildkite implements CISource {
constructor(private readonly env: Env) {
}
constructor(private readonly env: Env) {}

get name(): string { return "Buildkite" }
get name(): string {
return "Buildkite"
}

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ["BUILDKITE"])
Expand All @@ -19,12 +20,18 @@ export class Buildkite implements CISource {

private _parseRepoURL(): string {
const repoURL = this.env.BUILDKITE_REPO
const regexp = new RegExp("([\/:])([^\/]+\/[^\/.]+)(?:.git)?$")
const regexp = new RegExp("([/:])([^/]+/[^/.]+)(?:.git)?$")
const matches = repoURL.match(regexp)
return matches ? matches[2] : ""
}

get pullRequestID(): string { return this.env.BUILDKITE_PULL_REQUEST }
get repoSlug(): string { return this._parseRepoURL() }
get supportedPlatforms(): string[] { return ["github"] }
get pullRequestID(): string {
return this.env.BUILDKITE_PULL_REQUEST
}
get repoSlug(): string {
return this._parseRepoURL()
}
get supportedPlatforms(): string[] {
return ["github"]
}
}
25 changes: 15 additions & 10 deletions source/ci_source/providers/Circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers"
*
*/
export class Circle implements CISource {
constructor(private readonly env: Env) {
}
constructor(private readonly env: Env) {}

get name(): string { return "Circle CI" }
get name(): string {
return "Circle CI"
}

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ["CIRCLE_BUILD_NUM"])
Expand All @@ -48,14 +49,14 @@ export class Circle implements CISource {
return ensureEnvKeysExist(this.env, mustHave) && ensureEnvKeysAreInt(this.env, ["CIRCLE_PR_NUMBER"])
}

private _prParseURL(): {owner?: string, reponame?: string, id?: string} {
private _prParseURL(): { owner?: string; reponame?: string; id?: string } {
const prUrl = this.env.CI_PULL_REQUEST || ""
const splitSlug = prUrl.split("/")
if (splitSlug.length === 7) {
const owner = splitSlug[3]
const reponame = splitSlug[4]
const id = splitSlug[6]
return {owner, reponame, id}
return { owner, reponame, id }
}
return {}
}
Expand All @@ -64,16 +65,20 @@ export class Circle implements CISource {
if (this.env.CIRCLE_PR_NUMBER) {
return this.env.CIRCLE_PR_NUMBER
} else {
const {id} = this._prParseURL()
const { id } = this._prParseURL()
return id || ""
}
}

get repoSlug(): string {
const {owner, reponame} = this._prParseURL()
return (owner && reponame) ? `${owner}/${reponame}` : ""
const { owner, reponame } = this._prParseURL()
return owner && reponame ? `${owner}/${reponame}` : ""
}

get repoURL(): string { return this.env.CIRCLE_REPOSITORY_URL }
get supportedPlatforms(): string[] { return ["github"] }
get repoURL(): string {
return this.env.CIRCLE_REPOSITORY_URL
}
get supportedPlatforms(): string[] {
return ["github"]
}
}
11 changes: 7 additions & 4 deletions source/ci_source/providers/Codeship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { ensureEnvKeysExist, getPullRequestIDForBranch } from "../ci_source_help
*/
export class Codeship implements CISource {
private default = { prID: "0" }
constructor(private readonly env: Env) {
}
constructor(private readonly env: Env) {}

async setup(): Promise<any> {
const prID = await getPullRequestIDForBranch(this, this.env, this.branchName)
this.default.prID = prID.toString()
}

get name(): string { return "Codeship" }
get name(): string {
return "Codeship"
}

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ["CODESHIP"])
Expand All @@ -35,7 +36,9 @@ export class Codeship implements CISource {
return ""
}

get supportedPlatforms(): string[] { return ["github"] }
get supportedPlatforms(): string[] {
return ["github"]
}

private get branchName(): string {
return this.env.CI_BRANCH
Expand Down
25 changes: 15 additions & 10 deletions source/ci_source/providers/DockerCloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { ensureEnvKeysExist } from "../ci_source_helpers"
* Docs: TODO
*/
export class DockerCloud implements CISource {
constructor(private readonly env: Env) {
}
constructor(private readonly env: Env) {}

get name(): string { return "Docker Cloud" }
get name(): string {
return "Docker Cloud"
}

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ["DOCKER_REPO"])
Expand All @@ -23,28 +24,32 @@ export class DockerCloud implements CISource {
return ensureEnvKeysExist(this.env, mustHave)
}

private _prParseURL(): {owner?: string, reponame?: string, id?: string} {
private _prParseURL(): { owner?: string; reponame?: string; id?: string } {
const prUrl = this.env.PULL_REQUEST_URL || ""
const splitSlug = prUrl.split("/")
if (splitSlug.length === 7) {
const owner = splitSlug[3]
const reponame = splitSlug[4]
const id = splitSlug[6]
return {owner, reponame, id}
return { owner, reponame, id }
}
return {}
}

get pullRequestID(): string {
const {id} = this._prParseURL()
const { id } = this._prParseURL()
return id || ""
}

get repoSlug(): string {
const {owner, reponame} = this._prParseURL()
return (owner && reponame) ? `${owner}/${reponame}` : ""
const { owner, reponame } = this._prParseURL()
return owner && reponame ? `${owner}/${reponame}` : ""
}

get repoURL(): string { return this.env.SOURCE_REPOSITORY_URL }
get supportedPlatforms(): string[] { return ["github"] }
get repoURL(): string {
return this.env.SOURCE_REPOSITORY_URL
}
get supportedPlatforms(): string[] {
return ["github"]
}
}

0 comments on commit a45ad2f

Please sign in to comment.