|
1 | 1 | import fs = require('fs')
|
2 | 2 | import os = require('os')
|
3 | 3 | import path = require('path')
|
| 4 | +import cp = require('child_process') |
| 5 | +import stripAnsi = require('strip-ansi') |
4 | 6 | import { markdown, danger, fail } from "danger"
|
5 | 7 | const suggestionsDir = [os.homedir(), ".dts", "suggestions"].join('/')
|
6 | 8 | const lines: string[] = []
|
@@ -67,10 +69,52 @@ newDTSFiles.forEach(dts => {
|
67 | 69 | }
|
68 | 70 | })
|
69 | 71 |
|
70 |
| -for (const filename of danger.git.modified_files.concat(danger.git.created_files)) { |
71 |
| - danger.git.diffForFile(filename).then(d => { |
72 |
| - if (d.added.indexOf("\t") > -1) { // This is a dumb check for tabs, in lieu of running `prettier` on the diff'd parts |
73 |
| - fail("The root .editorconfig style specifies spaces for whitespace. Please use spaces in new or changed types.", filename, d.after.split("\n").findIndex(e => e.indexOf("\t") > -1) + 1) |
| 72 | +function chunked<T>(arr: T[], size: number): T[][] { |
| 73 | + const result: T[][] = []; |
| 74 | + for (let i = 0; i < arr.length; i += size) { |
| 75 | + result.push(arr.slice(i, i + size)) |
| 76 | + } |
| 77 | + return result; |
| 78 | +} |
| 79 | + |
| 80 | +const unformatted = []; |
| 81 | +const allFiles = [...danger.git.created_files, ...danger.git.modified_files]; |
| 82 | +// We batch this in chunks to avoid hitting max arg length issues. |
| 83 | +for (const files of chunked(allFiles, 50)) { |
| 84 | + const result = cp.spawnSync( |
| 85 | + process.execPath, |
| 86 | + ["node_modules/dprint/bin.js", "check", ...files], |
| 87 | + { encoding: "utf8", maxBuffer: 100 * 1024 * 1024 } |
| 88 | + ); |
| 89 | + if (result.status !== 0) { |
| 90 | + for (const line of result.stdout.split(/\r?\n/)) { |
| 91 | + const match = stripAnsi(line).match(/^from (.*):$/); |
| 92 | + if (match) { |
| 93 | + unformatted.push(path.relative(process.cwd(), match[1])); |
| 94 | + } |
74 | 95 | }
|
75 |
| - }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +if (unformatted.length > 0) { |
| 100 | + const message = [ |
| 101 | + "## Formatting", |
| 102 | + "", |
| 103 | + ]; |
| 104 | + |
| 105 | + message.push(`The following files are not formatted: |
| 106 | +1. ` + unformatted.slice(0,5).join('\n1. ')) |
| 107 | + if (unformatted.length > 5) { |
| 108 | + const extras = unformatted.slice(5) |
| 109 | + message.push(` |
| 110 | +<details> |
| 111 | +<summary>as well as these ${extras.length} other files...</summary> |
| 112 | +<p>${extras.join(", ")}</p> |
| 113 | +</details> |
| 114 | +`) |
| 115 | + } |
| 116 | + |
| 117 | + message.push("Consider running `npx dprint fmt` on these files to make review easier.") |
| 118 | + |
| 119 | + markdown(message.join("\n")) |
76 | 120 | }
|
0 commit comments