lint-prettier.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 glob = require('glob')
  9. const prettier = require('prettier')
  10. const fs = require('fs')
  11. const prettierConfigPath = require.resolve('../.prettierrc')
  12. const files = process.argv.slice(2)
  13. let didError = false
  14. let didWarn = false
  15. files.forEach(file => {
  16. const options = prettier.resolveConfig.sync(file, {
  17. config: prettierConfigPath
  18. })
  19. try {
  20. const fileInfo = prettier.getFileInfo.sync(file)
  21. if (fileInfo.ignored) {
  22. return
  23. }
  24. const input = fs.readFileSync(file, 'utf8')
  25. const withParserOptions = {
  26. ...options,
  27. parser: fileInfo.inferredParser
  28. }
  29. const isPrettier = prettier.check(input, withParserOptions)
  30. if (!isPrettier) {
  31. console.log(`\x1b[31m ${file} is no prettier, please use npm run prettier and git add !`)
  32. didWarn = true
  33. }
  34. } catch (e) {
  35. didError = true
  36. }
  37. })
  38. if (didWarn || didError) {
  39. process.exit(1)
  40. }
  41. console.log('\x1b[32m lint prettier success!')