-
Notifications
You must be signed in to change notification settings - Fork 28
/
prettier-check.js
47 lines (42 loc) · 1.02 KB
/
prettier-check.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
const glob = require('glob');
const fs = require('fs');
const prettier = require('prettier');
const prettierrc = require('./.prettierrc.json');
const prettierignore = fs
.readFileSync('./.prettierignore')
.toString()
.split('\n')
.filter(Boolean)
.map(dir => dir + '/**');
const files = process.argv.slice(2).reduce((files, g) => {
return files.concat(glob.sync(g, { ignore: prettierignore }));
}, []);
Promise.all(
files.map(filepath => {
return readFile(filepath).then(data => {
if (
!prettier.check(
data.toString(),
Object.assign({ filepath }, prettierrc)
)
) {
return Promise.reject();
}
});
})
).catch(() => {
// eslint-disable-next-line no-console
console.error('Please run prettier: `npm run prettier`\n');
process.exit(1);
});
function readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}