lint-prettier.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * copy to https://github.com/facebook/react/blob/master/scripts/prettier/index.js
  3. * prettier api doc https://prettier.io/docs/en/api.html
  4. *----------*****--------------
  5. * lint file is prettier
  6. *----------*****--------------
  7. */
  8. const prettier = require('prettier');
  9. const fs = require('fs');
  10. const chalk = require('chalk');
  11. const prettierConfigPath = require.resolve('../.prettierrc');
  12. const files = process.argv.slice(2);
  13. let didError = false;
  14. files.forEach(file => {
  15. Promise.all([
  16. prettier.resolveConfig(file, {
  17. config: prettierConfigPath,
  18. }),
  19. prettier.getFileInfo(file),
  20. ])
  21. .then(resolves => {
  22. const [options, fileInfo] = resolves;
  23. if (fileInfo.ignored) {
  24. return;
  25. }
  26. const input = fs.readFileSync(file, 'utf8');
  27. const withParserOptions = {
  28. ...options,
  29. parser: fileInfo.inferredParser,
  30. };
  31. const output = prettier.format(input, withParserOptions);
  32. if (output !== input) {
  33. fs.writeFileSync(file, output, 'utf8');
  34. console.log(chalk.green(`${file} is prettier`));
  35. }
  36. })
  37. .catch(e => {
  38. didError = true;
  39. })
  40. .finally(() => {
  41. if (didError) {
  42. process.exit(1);
  43. }
  44. console.log(chalk.hex('#1890FF')('prettier success!'));
  45. });
  46. });