-
Notifications
You must be signed in to change notification settings - Fork 3
/
prettier.js
53 lines (48 loc) · 1.45 KB
/
prettier.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
const path = require('path');
const spawn = require('child_process').spawnSync;
const chalk = require('chalk');
function runCommand(cmd, args, cwd = __dirname) {
const displayArgs =
args.length > 25 ? `${args.slice(0, 25)}...` : args.join(' ');
console.log(chalk.dim(`$ cwd ${cwd}\n$ ${cmd} ${displayArgs}\n`)); // eslint-disable-line
const result = spawn(cmd, args, {
cwd,
shell: true,
stdio: 'inherit',
});
if (result.error || result.status !== 0) {
const message = 'Error running command.';
const error = new Error(message);
error.stack = message;
throw error;
}
}
const shouldWrite = process.argv[2] === 'write';
const isWindows = process.platform === 'win32';
const prettier = isWindows ? 'prettier.cmd' : 'prettier';
const prettierCmd = path.resolve(__dirname, `node_modules/.bin/${prettier}`);
const options = {
config: './.prettierrc.js',
};
// prettier-ignore
const args = Object.keys(options)
.map(key => `--${key}=${options[key]}`)
.concat(
`--${shouldWrite ? 'write' : 'l'}`,
'"src/**/*(*.js|*.jsx|*.ts|*.tsx)"'
);
try {
runCommand(prettierCmd, args);
} catch (e) {
if (!shouldWrite) {
// prettier-ignore
console.log(
`${chalk.red(`\nThis project uses prettier to format all JavaScript code.\n`) +
chalk.dim(`Please run `) +
chalk.reset('yarn prettier') +
chalk.dim(` and add changes to files listed above to your commit.`)
}\n`
);
process.exitCode = 1;
}
}