lint-prettier.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 prettierConfigPath = require.resolve('../.prettierrc');
  11. const files = process.argv.slice(2);
  12. let didError = false;
  13. let didWarn = 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 isPrettier = prettier.check(input, withParserOptions);
  32. if (!isPrettier) {
  33. console.log(
  34. `\x1b[31m ${file} is no prettier, please use npm run prettier and git add !\x1b[0m`
  35. );
  36. didWarn = true;
  37. }
  38. })
  39. .catch(e => {
  40. didError = true;
  41. })
  42. .finally(() => {
  43. if (didWarn || didError) {
  44. process.exit(1);
  45. }
  46. console.log('\x1b[32m lint prettier success!\x1b[0m');
  47. });
  48. });