Unverified Commit be6f46de authored by 陈帅's avatar 陈帅 Committed by GitHub

lint-prettier will direct write (#3318)

parent fdeecc67
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
const prettier = require('prettier'); const prettier = require('prettier');
const fs = require('fs'); const fs = require('fs');
const chalk = require('chalk');
const prettierConfigPath = require.resolve('../.prettierrc'); const prettierConfigPath = require.resolve('../.prettierrc');
const files = process.argv.slice(2); const files = process.argv.slice(2);
let didError = false; let didError = false;
let didWarn = false;
files.forEach(file => { files.forEach(file => {
Promise.all([ Promise.all([
...@@ -32,21 +32,19 @@ files.forEach(file => { ...@@ -32,21 +32,19 @@ files.forEach(file => {
...options, ...options,
parser: fileInfo.inferredParser, parser: fileInfo.inferredParser,
}; };
const isPrettier = prettier.check(input, withParserOptions); const output = prettier.format(input, withParserOptions);
if (!isPrettier) { if (output !== input) {
console.log( fs.writeFileSync(file, output, 'utf8');
`\x1b[31m ${file} is no prettier, please use npm run prettier and git add !\x1b[0m` console.log(chalk.green(`${file} is prettier`));
);
didWarn = true;
} }
}) })
.catch(e => { .catch(e => {
didError = true; didError = true;
}) })
.finally(() => { .finally(() => {
if (didWarn || didError) { if (didError) {
process.exit(1); process.exit(1);
} }
console.log('\x1b[32m lint prettier success!\x1b[0m'); console.log(chalk.hex('#1890FF')('prettier success!'));
}); });
}); });
...@@ -6,11 +6,11 @@ ...@@ -6,11 +6,11 @@
*----------*****-------------- *----------*****--------------
*/ */
const glob = require('glob');
const prettier = require('prettier'); const prettier = require('prettier');
const fs = require('fs'); const fs = require('fs');
const getPrettierFiles = require('./getPrettierFiles'); const getPrettierFiles = require('./getPrettierFiles');
const prettierConfigPath = require.resolve('../.prettierrc'); const prettierConfigPath = require.resolve('../.prettierrc');
const chalk = require('chalk');
let didError = false; let didError = false;
...@@ -33,7 +33,7 @@ files.forEach(file => { ...@@ -33,7 +33,7 @@ files.forEach(file => {
const output = prettier.format(input, withParserOptions); const output = prettier.format(input, withParserOptions);
if (output !== input) { if (output !== input) {
fs.writeFileSync(file, output, 'utf8'); fs.writeFileSync(file, output, 'utf8');
console.log(`\x1b[34m ${file} is prettier`); console.log(chalk.green(`${file} is prettier`));
} }
} catch (e) { } catch (e) {
didError = true; didError = true;
...@@ -43,4 +43,4 @@ files.forEach(file => { ...@@ -43,4 +43,4 @@ files.forEach(file => {
if (didError) { if (didError) {
process.exit(1); process.exit(1);
} }
console.log('\x1b[32m prettier success!'); console.log(chalk.hex('#1890FF')('prettier success!'));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment