forked from duolingo/pre-commit-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.ts
More file actions
352 lines (328 loc) · 9.93 KB
/
entry.ts
File metadata and controls
352 lines (328 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env node
import { exec } from "child_process";
import { readFile, writeFile } from "fs";
import { dirname } from "path";
/** Maximum characters per line in Python */
const PYTHON_LINE_LENGTH = 100;
/**
* Path to an empty file that we can provide to linters/formatters as a config
* file in order to force those tools' default behavior
*/
const EMPTY_FILE = "/emptyfile";
/** CLI options to use in all Prettier invocations */
const PRETTIER_OPTIONS = [
"--ignore-path",
EMPTY_FILE,
"--loglevel",
"warn",
"--no-config",
"--no-editorconfig",
"--write",
];
/** Runs a shell command, promising combined stdout and stderr */
const run = (command: string | string[]) =>
new Promise<string>((resolve, reject) => {
exec(
typeof command === "string"
? command
: command.map(arg => `'${arg}'`).join(" "),
{ maxBuffer: Infinity },
(ex, stdout, stderr) => (ex ? reject : resolve)(stdout + stderr),
);
});
/** Reads a file, transforms its contents, and writes the result if different */
const transformFile = (path: string, transform: (before: string) => string) =>
new Promise<string>((resolve, reject) => {
readFile(path, "utf8", (err, data) => {
// File unreadable
if (err) {
reject(err);
return;
}
// File empty
if (data === "") {
resolve();
return;
}
// File unmodified
const after = transform(data);
if (data === after) {
resolve();
return;
}
// File modified
writeFile(path, after, "utf8", err => (err ? reject(err) : resolve()));
});
});
const enum HookName {
Black = "Black",
GoogleJavaFormat = "google-java-format",
Ktlint = "ktlint",
PrettierJs = "Prettier (JS)",
PrettierNonJs = "Prettier (non-JS)",
Svgo = "SVGO",
TerraformFmt = "terraform fmt",
WhitespaceFixer = "Whitespace fixer",
}
interface Hook {
/**
* Runs the tool and throws a display message iff violations were found that
* the user must fix manually.
*
* Formatters should return their combined stdout+stderr output only in case
* of parsing errors due to malformed source code, while linters should
* return their detected violations.
*
* The message is thrown (instead of returned) both so that unexpected
* linter crashes are surfaced to the user and for simplicity in the case of
* the many linters that exit with nonzero iff there are violations.
*/
action: (sources: string[]) => Promise<unknown>;
/** Hooks that must complete before this one begins */
dependsOn?: HookName[];
/** Source files to exclude */
exclude?: RegExp;
/** Source files to include */
include: RegExp;
}
interface LockableHook extends Hook {
/** Upon resolution, indicates that this hook has completed */
lock: Promise<unknown>;
/** Promise that must resolve before this hook begins execution */
locksToWaitFor?: Promise<unknown>;
/** Marks this hook as complete */
unlock: () => void;
}
/** Wraps a non-lockable hook to add properties used for locking */
const createLockableHook = (hook: Hook): LockableHook => {
let unlock = () => undefined as void;
const lock = new Promise(resolve => {
unlock = resolve;
});
return { ...hook, lock, unlock };
};
/** Hooks expressed in a format similar to .pre-commit-config.yaml */
const HOOKS: Record<HookName, LockableHook> = {
[HookName.Black]: createLockableHook({
action: async sources => {
// Detect Python 2 based on its syntax and common functions
let pythonVersionArgs: string[];
try {
// Would just use `git grep -q` but it doesn't seem to exit early?!
(await run(
`git grep -E "^([^#]*[^#.]\\b(basestring|(iter(items|keys|values)|raw_input|unicode|xrange)\\()| *print ['\\"])" '*.py' | grep -qE .`,
)).trim().length;
pythonVersionArgs = ["--fast", "--target-version", "py27"];
} catch (ex) {
pythonVersionArgs = ["--target-version", "py36"];
}
await run([
"black",
"--config",
EMPTY_FILE,
"--line-length",
`${PYTHON_LINE_LENGTH}`,
"--quiet",
...pythonVersionArgs,
...sources,
]);
},
dependsOn: [HookName.WhitespaceFixer],
include: /\.py$/,
}),
[HookName.GoogleJavaFormat]: createLockableHook({
action: sources =>
run([
"java",
"-jar",
"/google-java-format-1.7-all-deps.jar",
"--replace",
...sources,
]),
dependsOn: [HookName.WhitespaceFixer],
include: /\.java$/,
}),
[HookName.Ktlint]: createLockableHook({
action: async sources => {
try {
await run([
"/ktlint",
"--experimental", // Enables indentation formatting
"--format",
...sources,
]);
} catch (ex) {
// ktlint just failed to autocorrect some stuff, e.g. long lines
}
},
dependsOn: [HookName.WhitespaceFixer],
include: /\.kt$/,
}),
[HookName.PrettierJs]: createLockableHook({
action: sources =>
run([
"prettier",
...PRETTIER_OPTIONS,
"--trailing-comma",
"es5",
...sources,
]),
dependsOn: [HookName.WhitespaceFixer],
exclude: /\b(compressed|custom|min|minified|pack|prod|production)\b/,
include: /\.js$/,
}),
[HookName.PrettierNonJs]: createLockableHook({
action: sources =>
run([
"prettier",
...PRETTIER_OPTIONS,
"--trailing-comma",
"all",
...sources,
]),
dependsOn: [HookName.WhitespaceFixer],
include: /\.(html?|markdown|md|tsx?|ya?ml)$/,
}),
[HookName.Svgo]: createLockableHook({
action: sources =>
run([
"svgo",
`--disable=${[
"addAttributesToSVGElement",
"addClassesToSVGElement",
"cleanupEnableBackground",
"cleanupIDs",
"cleanupListOfValues",
"cleanupNumericValues",
"collapseGroups", // Can cause shape misalignment
"convertColors",
"convertEllipseToCircle",
"convertPathData",
"convertShapeToPath",
"convertStyleToAttrs",
"convertTransform",
"inlineStyles",
"mergePaths",
"minifyStyles",
"moveElemsAttrsToGroup",
"moveGroupAttrsToElems",
"prefixIds",
"removeAttributesBySelector",
"removeAttrs",
"removeDesc",
"removeDimensions",
"removeDoctype",
"removeEditorsNSData",
"removeElementsByAttr",
"removeEmptyAttrs",
"removeEmptyContainers",
"removeEmptyText",
"removeHiddenElems",
"removeMetadata",
"removeNonInheritableGroupAttrs",
"removeOffCanvasPaths",
"removeRasterImages",
"removeScriptElement",
"removeStyleElement",
"removeTitle",
"removeUnknownsAndDefaults", // Can turn shapes black
"removeUnusedNS",
"removeUselessDefs", // Blows away SVG fonts
"removeUselessStrokeAndFill",
"removeViewBox",
"removeXMLNS",
"removeXMLProcInst",
"reusePaths",
"sortAttrs",
"sortDefsChildren",
].join(",")}`,
`--enable=${["cleanupAttrs", "removeComments"].join(",")}`,
"--quiet",
...sources,
]),
dependsOn: [HookName.WhitespaceFixer],
include: /\.svg$/,
}),
[HookName.TerraformFmt]: createLockableHook({
action: async sources => {
const dirs = Array.from(new Set(sources.map(source => dirname(source))));
await Promise.all(
dirs.map(dir => run(["terraform", "fmt", "-write=true", dir])),
);
},
dependsOn: [HookName.WhitespaceFixer],
include: /\.tf$/,
}),
// Strip trailing whitespace, strip BOF newlines, require single EOF newline
[HookName.WhitespaceFixer]: createLockableHook({
action: sources =>
Promise.all(
sources.map(source =>
transformFile(source, data => {
const eol = /\r/.test(data) ? "\r\n" : "\n";
return (
data.replace(/[^\S\r\n]+$/gm, "").replace(/^[\r\n]+|\s+$/g, "") +
eol
);
}),
),
),
include: /./,
}),
};
/** Files that match this pattern should never be processed */
const GLOBAL_EXCLUDES = /(^|\/)(build|node_modules)\//;
/** Prefixes a string to all nonempty lines of input */
const prefixLines = (() => {
const maxPrefixLength = Math.max(
...Object.keys(HOOKS).map(name => name.length),
);
return (prefix: string, lines: string) =>
lines
.split("\n")
.filter(line => line.trim().length)
.map(line => `${prefix}:`.padEnd(maxPrefixLength + 2) + line)
.join("\n");
})();
(async () => {
// Determine list of source files to process
const sources = process.argv.slice(2); // Strips ['/usr/bin/node', '/entry']
// Set up hook locks
Object.values(HOOKS).forEach(hook => {
if (hook.dependsOn) {
hook.locksToWaitFor = Promise.all(
hook.dependsOn.map(hookName => HOOKS[hookName].lock),
);
}
});
// Run all hooks in parallel
let success = true;
await Promise.all(
Object.entries(HOOKS).map(
async ([name, { action, exclude, include, locksToWaitFor, unlock }]) => {
// Wait until necessary hooks have completed
await locksToWaitFor;
// Determine set of source files to process
const includedSources = sources.filter(
source =>
include.test(source) &&
!GLOBAL_EXCLUDES.test(source) &&
!(exclude && exclude.test(source)),
);
// Run hook
if (includedSources.length) {
try {
await action(includedSources);
} catch (ex) {
success = false;
console.error(prefixLines(name, `${ex}`));
}
}
// Mark this hook as complete
unlock();
},
),
);
// Exit with appropriate code
success || process.exit(1);
})();