-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathprettier.ts
41 lines (39 loc) · 1.21 KB
/
prettier.ts
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
import {execSync} from "child_process"
import {resolve} from "path"
// We can run in two modes here. If the argument given is write,
// we format all the files in the project. If it's check, we
// run prettier and if any file would change, return a non-zero exit code.
const action: string = process.argv[2]
switch (action) {
case "write":
execSync(command("write"), {stdio: "inherit"})
break
case "check":
console.log("Checking for files that need formatting:")
try {
execSync(command("check"), {stdio: "inherit"})
console.log("All good.")
} catch (error) {
if (error.status === 1) {
console.log(
"\nThe files listed above need to be formatted correctly. Please run the following command and commit the changes:\n> npm run prettier",
)
} else {
console.error(error)
}
process.exit(1)
}
break
default:
console.log("usage: prettier write|check")
process.exit(1)
}
function command(action: "write" | "check") {
return [
resolve(__dirname, "..", "node_modules", ".bin", "prettier"),
...[
action === "write" ? "--write" : "--list-different",
'"./{lib,scripts,spec}/**/*.{ts,tsx}"',
],
].join(" ")
}