Repository: mui/material-ui
File: scripts/prettier.js (JavaScript)

Code before:

const glob = require('glob-gitignore');
const prettier = require('prettier');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const { LANGUAGES } = require('docs/src/modules/constants');
const listChangedFiles = require('./listChangedFiles');

function isTranslatedDocument(filename) {
  // markdown files from crowdin end with a 2 letter locale
  return new RegExp(String.raw`-(${LANGUAGES.join('|')})\.md$`).test(filename);
}

function runPrettier(options) {
  const { changedFiles, shouldWrite } = options;

  let didWarn = false;
  let didError = false;

  const eslintIgnorePath = path.join(
    process.env.ROOT_PATH || path.join(__dirname, '..'),
    '.eslintignore',
  );
  const prettierConfigPath = path.join(
    process.env.ROOT_PATH || path.join(__dirname, '..'),
    'prettier.config.js',
  );

  const warnedFiles = [];
  const ignoredFiles = fs
    .readFileSync(eslintIgnorePath, 'utf-8')
    .split(/\r*\n/)
    .filter((notEmpty) => notEmpty);

  const files = glob
    .sync('**/*.{js,md,tsx,ts,json}', {
      ignore: [
        '**/node_modules/**',
        // these are auto-generated
        'docs/pages/api-docs/**/*.md',
        ...ignoredFiles,
      ],
    })
    .filter(
      (f) =>
        (!changedFiles || changedFiles.has(f)) &&
        // These come from crowdin.
        // If we would commit changes crowdin would immediately try to revert.
        // If we want to format these files we'd need to do it in crowdin
        !isTranslatedDocument(f),

Review comment (security):
We should come up with a better strategy. Environment variables are an escape hatch. How is this script used that requires this change?

Code after:

const glob = require('glob-gitignore');
const prettier = require('prettier');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const { LANGUAGES } = require('docs/src/modules/constants');
const listChangedFiles = require('./listChangedFiles');

function isTranslatedDocument(filename) {
  // markdown files from crowdin end with a 2 letter locale
  return new RegExp(String.raw`-(${LANGUAGES.join('|')})\.md$`).test(filename);
}

function runPrettier(options) {
  const { changedFiles, shouldWrite } = options;

  let didWarn = false;
  let didError = false;

  const warnedFiles = [];
  const ignoredFiles = fs
    .readFileSync(path.join(process.cwd(), '.eslintignore'), 'utf-8')
    .split(/\r*\n/)
    .filter((notEmpty) => notEmpty);

  const files = glob
    .sync('**/*.{js,md,tsx,ts,json}', {
      ignore: [
        '**/node_modules/**',
        // these are auto-generated
        'docs/pages/api-docs/**/*.md',
        ...ignoredFiles,
      ],
    })
    .filter(
      (f) =>
        (!changedFiles || changedFiles.has(f)) &&
        // These come from crowdin.
        // If we would commit changes crowdin would immediately try to revert.
        // If we want to format these files we'd need to do it in crowdin
        !isTranslatedDocument(f),
    );

  if (!files.length) {
    return;
  }

  const prettierConfigPath = path.join(process.cwd(), 'prettier.config.js');

  files.forEach((file) => {