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. });

prettier.js

  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. * prettier all js and all ts.
  6. *----------*****--------------
  7. */
  8. const prettier = require('prettier');
  9. const fs = require('fs');
  10. const getPrettierFiles = require('./getPrettierFiles');
  11. const prettierConfigPath = require.resolve('../.prettierrc');
  12. const chalk = require('chalk');
  13. let didError = false;
  14. const files = getPrettierFiles();
  15. files.forEach(file => {
  16. const options = prettier.resolveConfig.sync(file, {
  17. config: prettierConfigPath,
  18. });
  19. const fileInfo = prettier.getFileInfo.sync(file);
  20. if (fileInfo.ignored) {
  21. return;
  22. }
  23. try {
  24. const input = fs.readFileSync(file, 'utf8');
  25. const withParserOptions = {
  26. ...options,
  27. parser: fileInfo.inferredParser,
  28. };
  29. const output = prettier.format(input, withParserOptions);
  30. if (output !== input) {
  31. fs.writeFileSync(file, output, 'utf8');
  32. console.log(chalk.green(`${file} is prettier`));
  33. }
  34. } catch (e) {
  35. didError = true;
  36. }
  37. });
  38. if (didError) {
  39. process.exit(1);
  40. }
  41. console.log(chalk.hex('#1890FF')('prettier success!'));

getPrettierFiles.js

  1. const glob = require('glob');
  2. const getPrettierFiles = () => {
  3. let files = [];
  4. const jsFiles = glob.sync('src/**/*.js*', { ignore: ['**/node_modules/**', 'build/**'] });
  5. const tsFiles = glob.sync('src/**/*.ts*', { ignore: ['**/node_modules/**', 'build/**'] });
  6. const configFiles = glob.sync('config/**/*.js*', { ignore: ['**/node_modules/**', 'build/**'] });
  7. const scriptFiles = glob.sync('scripts/**/*.js');
  8. const lessFiles = glob.sync('src/**/*.less*', { ignore: ['**/node_modules/**', 'build/**'] });
  9. files = files.concat(jsFiles);
  10. files = files.concat(tsFiles);
  11. files = files.concat(configFiles);
  12. files = files.concat(scriptFiles);
  13. files = files.concat(lessFiles);
  14. if (!files.length) {
  15. return;
  16. }
  17. return files;
  18. };
  19. module.exports = getPrettierFiles;