@samitouri / QOS-React / commits / 1d1f7038ec

Handle prettier error (#11466)

* handle prettier error * error if prettier fail

Joe Lim committed Nov 6, 2017 at 03:17 UTC 1d1f7038ec1c9e49408de7629bed53410c6b63a6
1 file changed +31 -24
scripts/prettier/index.js
+31 -24
@@ -79,6 +79,7 @@ var changedFiles = new Set(
79 );
80
81 let didWarn = false;
82 +let didError = false;
83 Object.keys(config).forEach(key => {
84 const patterns = config[key].patterns;
85 const options = config[key].options;
@@ -97,35 +98,41 @@ Object.keys(config).forEach(key => {
98
99 const args = Object.assign({}, defaultOptions, options);
100 files.forEach(file => {
100 - const input = fs.readFileSync(file, 'utf8');
101 - if (shouldWrite) {
102 - const output = prettier.format(input, args);
103 - if (output !== input) {
104 - fs.writeFileSync(file, output, 'utf8');
105 - }
106 - } else {
107 - if (!prettier.check(input, args)) {
108 - if (!didWarn) {
109 - console.log(
110 - '\n' +
111 - chalk.red(
112 - ` This project uses prettier to format all JavaScript code.\n`
113 - ) +
114 - chalk.dim(` Please run `) +
115 - chalk.reset('yarn prettier-all') +
116 - chalk.dim(
117 - ` and add changes to files listed below to your commit:`
118 - ) +
119 - `\n\n`
120 - );
121 - didWarn = true;
101 + try {
102 + const input = fs.readFileSync(file, 'utf8');
103 + if (shouldWrite) {
104 + const output = prettier.format(input, args);
105 + if (output !== input) {
106 + fs.writeFileSync(file, output, 'utf8');
107 + }
108 + } else {
109 + if (!prettier.check(input, args)) {
110 + if (!didWarn) {
111 + console.log(
112 + '\n' +
113 + chalk.red(
114 + ` This project uses prettier to format all JavaScript code.\n`
115 + ) +
116 + chalk.dim(` Please run `) +
117 + chalk.reset('yarn prettier-all') +
118 + chalk.dim(
119 + ` and add changes to files listed below to your commit:`
120 + ) +
121 + `\n\n`
122 + );
123 + didWarn = true;
124 + }
125 + console.log(file);
126 }
123 - console.log(file);
127 }
128 + } catch (error) {
129 + didError = true;
130 + console.log('\n\n' + error.message);
131 + console.log(file);
132 }
133 });
134 });
135
129 -if (didWarn) {
136 +if (didWarn || didError) {
137 process.exit(1);
138 }