-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
pretty.js
executable file
·67 lines (60 loc) · 1.64 KB
/
pretty.js
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
#!/usr/bin/env node
/**
* Copyright (c) Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
const { spawnSync } = require('child_process');
const { join } = require('path');
const os = require('os');
const INVERSE = '\x1b[7m';
const RESET = '\x1b[0m';
const YELLOW = '\x1b[33m';
// Simple glob because we use .eslintignore to limit it
const glob = '**/*.{js,ts,jsx,tsx,md,json5,json,html,css}';
const root = join(__dirname, '..');
const executable = join(
root,
'node_modules',
'.bin',
os.platform() === 'win32' ? 'prettier.cmd' : 'prettier',
);
const ignorePath = ['--ignore-path', '.eslintignore'];
const check = process.argv.includes('--check');
const mode = check ? '--list-different' : '--write';
process.chdir(root);
const { stdout, stderr, status, error } = spawnSync(executable, [
...ignorePath,
mode,
glob,
]);
const out = stdout.toString().trim();
const err = stderr.toString().trim();
function print(message) {
if (message) {
process.stdout.write(message + '\n');
}
}
if (status) {
print(out);
print(err);
if (check) {
print(
`\n${YELLOW}Ooops! The files listed above are not correctly formatted.`,
);
print(
[
`Try: ${INVERSE} yarn format ${RESET},`,
`which executes eslint --fix and prettier in a specific order.`,
`If you are using prettier for vscode you can also enable "format on save".`,
`Learn more about contributing in DEVELOPMENT.md`,
].join('\n'),
);
}
}
if (error) {
print(error);
}
process.exit(status == null ? 1 : status);