Implement Prettier code formatting across the codebase with proper configuration and integration into development workflow.
- Current codebase lacks consistent code formatting standards leading to inconsistent style across files
- Add Prettier configuration with project-specific rules for consistent code formatting
- Integrate Prettier into development workflow with pre-commit hooks and CI/CD pipeline
- Format existing codebase to match new standards while preserving functionality
Configure Prettier with project-specific formatting rules and install necessary dependencies.
Add Prettier and related packages to the project with appropriate version constraints.
- Install prettier as a dev dependency using
npm install --save-dev prettier - Install prettier plugins if needed (e.g.,
@prettier/plugin-php,prettier-plugin-java) based on project languages - Update package.json to include prettier in devDependencies section
- Verify installation by running
npx prettier --version
Establish formatting rules and ignore patterns for the project.
- Create .prettierrc.json file in project root with formatting rules (tab width, semicolons, quotes, etc.)
- Create .prettierignore file to exclude build artifacts, node_modules, and generated files
- Add common ignore patterns like
dist/,build/,*.min.js,coverage/ - Test configuration by running
npx prettier --check .on a sample file
Set up development environment for automatic formatting on save.
- Create .vscode/settings.json with Prettier as default formatter and format-on-save enabled
- Add .editorconfig file if not present to ensure consistent editor behavior
- Document Prettier setup instructions in README.md or CONTRIBUTING.md
- Include instructions for other popular editors (IntelliJ, Sublime, etc.)
Integrate Prettier into development and CI/CD workflows to enforce consistent formatting.
Create convenient commands for developers to format and check code.
- Add
"format"script to package.json that runsprettier --write . - Add
"format:check"script that runsprettier --check . - Add
"format:staged"script for formatting only staged files usingprettier --write $(git diff --cached --name-only --diff-filter=ACMR) - Test scripts by running
npm run format:checkto verify they work correctly
Ensure code is formatted before commits using Git hooks.
- Install husky for Git hooks management:
npm install --save-dev husky - Install lint-staged for running commands on staged files:
npm install --save-dev lint-staged - Initialize husky with
npx husky installand add to package.json prepare script - Create .husky/pre-commit hook that runs
npx lint-staged - Configure lint-staged in package.json to run prettier on staged files
Add formatting checks to continuous integration to prevent unformatted code from being merged.
- Update CI configuration file (.github/workflows/, .gitlab-ci.yml, etc.) to include formatting check step
- Add step that runs
npm run format:checkafter dependency installation - Configure step to fail the build if formatting issues are found
- Add formatting check as a required status check for pull requests
Apply Prettier formatting to existing codebase while maintaining functionality.
Apply consistent formatting to all existing code files.
- Run
npm run formatto format all files in the codebase - Review changes using
git diffto ensure only formatting changes were made - Verify no functional changes by running existing test suite
- Commit formatting changes with descriptive message like "Apply Prettier formatting to entire codebase"
Ensure team members understand the new formatting standards and workflow.
- Update CONTRIBUTING.md with Prettier setup and usage instructions
- Add section about formatting requirements for pull requests
- Document how to resolve formatting conflicts and common issues
- Include troubleshooting guide for common Prettier configuration problems
- Verify Prettier installation by running
npx prettier --versionand confirming version output - Test formatting by creating a poorly formatted test file and running
npm run format, then verify it gets properly formatted - Test pre-commit hook by staging an unformatted file and attempting to commit, verify it gets formatted automatically
- Verify CI/CD integration by creating a pull request with unformatted code and confirming the build fails
- Test editor integration by saving a file in VS Code and confirming it formats automatically
- Run full test suite after formatting changes to ensure no functionality was broken
- Verify ignore patterns work by checking that files in .prettierignore are not formatted