1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  */
    
  7. 'use strict';
    
  8. 
    
  9. // Based on similar script in Jest
    
  10. // https://github.com/facebook/jest/blob/a7acc5ae519613647ff2c253dd21933d6f94b47f/scripts/prettier.js
    
  11. 
    
  12. const chalk = require('chalk');
    
  13. const glob = require('glob');
    
  14. const prettier = require('prettier');
    
  15. const fs = require('fs');
    
  16. const path = require('path');
    
  17. const listChangedFiles = require('../shared/listChangedFiles');
    
  18. const prettierConfigPath = require.resolve('../../.prettierrc');
    
  19. 
    
  20. const mode = process.argv[2] || 'check';
    
  21. const shouldWrite = mode === 'write' || mode === 'write-changed';
    
  22. const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
    
  23. 
    
  24. const changedFiles = onlyChanged ? listChangedFiles() : null;
    
  25. 
    
  26. const prettierIgnoreFilePath = path.join(
    
  27.   __dirname,
    
  28.   '..',
    
  29.   '..',
    
  30.   '.prettierignore'
    
  31. );
    
  32. const prettierIgnore = fs.readFileSync(prettierIgnoreFilePath, {
    
  33.   encoding: 'utf8',
    
  34. });
    
  35. const ignoredPathsListedInPrettierIgnore = prettierIgnore
    
  36.   .toString()
    
  37.   .replace(/\r\n/g, '\n')
    
  38.   .split('\n')
    
  39.   .filter(line => !!line && !line.startsWith('#'));
    
  40. 
    
  41. const ignoredPathsListedInPrettierIgnoreInGlobFormat =
    
  42.   ignoredPathsListedInPrettierIgnore.map(ignoredPath => {
    
  43.     const existsAndDirectory =
    
  44.       fs.existsSync(ignoredPath) && fs.lstatSync(ignoredPath).isDirectory();
    
  45. 
    
  46.     if (existsAndDirectory) {
    
  47.       return path.join(ignoredPath, '/**');
    
  48.     }
    
  49. 
    
  50.     return ignoredPath;
    
  51.   });
    
  52. 
    
  53. const files = glob
    
  54.   .sync('**/*.js', {
    
  55.     ignore: [
    
  56.       '**/node_modules/**',
    
  57.       '**/cjs/**',
    
  58.       ...ignoredPathsListedInPrettierIgnoreInGlobFormat,
    
  59.     ],
    
  60.   })
    
  61.   .filter(f => !onlyChanged || changedFiles.has(f));
    
  62. 
    
  63. if (!files.length) {
    
  64.   process.exit(0);
    
  65. }
    
  66. 
    
  67. async function main() {
    
  68.   let didWarn = false;
    
  69.   let didError = false;
    
  70. 
    
  71.   await Promise.all(
    
  72.     files.map(async file => {
    
  73.       const options = await prettier.resolveConfig(file, {
    
  74.         config: prettierConfigPath,
    
  75.       });
    
  76.       try {
    
  77.         const input = fs.readFileSync(file, 'utf8');
    
  78.         if (shouldWrite) {
    
  79.           const output = await prettier.format(input, options);
    
  80.           if (output !== input) {
    
  81.             fs.writeFileSync(file, output, 'utf8');
    
  82.           }
    
  83.         } else {
    
  84.           const isFormatted = await prettier.check(input, options);
    
  85.           if (!isFormatted) {
    
  86.             if (!didWarn) {
    
  87.               console.log(
    
  88.                 '\n' +
    
  89.                   chalk.red(
    
  90.                     `  This project uses prettier to format all JavaScript code.\n`
    
  91.                   ) +
    
  92.                   chalk.dim(`    Please run `) +
    
  93.                   chalk.reset('yarn prettier-all') +
    
  94.                   chalk.dim(
    
  95.                     ` and add changes to files listed below to your commit:`
    
  96.                   ) +
    
  97.                   `\n\n`
    
  98.               );
    
  99.               didWarn = true;
    
  100.             }
    
  101.             console.log(file);
    
  102.           }
    
  103.         }
    
  104.       } catch (error) {
    
  105.         didError = true;
    
  106.         console.log('\n\n' + error.message);
    
  107.         console.log(file);
    
  108.       }
    
  109.     })
    
  110.   );
    
  111.   if (didWarn || didError) {
    
  112.     process.exit(1);
    
  113.   }
    
  114. }
    
  115. 
    
  116. main().catch(error => {
    
  117.   console.error(error);
    
  118.   process.exit(1);
    
  119. });