All files / scripts check-format.js

0% Statements 0/15
0% Branches 0/2
0% Functions 0/2
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                                         
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
import chalk from 'chalk';
 
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(__dirname, '../');
 
/**
 * Checks if files are properly formatted using Prettier
 * Exits with code 1 if formatting is needed, 0 if all files are formatted
 */
function checkFormat() {
  // Get file paths from command line arguments (passed by lint-staged)
  const files = process.argv.slice(2);
 
  if (files.length === 0) {
    // No files to check
    return;
  }
 
  try {
    // Run prettier --check on the files
    // This will throw if files need formatting
    const quotedFiles = files.map((f) => `"${f}"`).join(' ');
    const command = `yarn prettier --check ${quotedFiles}`;
    execSync(command, {
      cwd: root,
      stdio: 'inherit',
      shell: true,
    });
    // All files are properly formatted
    console.log(chalk.green('āœ“ Format check passed'));
  } catch {
    // Prettier found files that need formatting (expected behavior)
    // The error is intentional - prettier exits with non-zero when files need formatting
    // We don't need the error object, just the exit code
    console.error(chalk.red('\nāœ– Prettier found files that need formatting'));
    console.error(chalk.dim(`  Run 'yarn format:fix' to fix formatting issues\n`));
    process.exit(1);
  }
}
 
checkFormat();