A CLI tool to format styled-components
template literals in your codebase. It ensures that your CSS-in-JS remains clean, consistent, and easy to read, supporting both styled-components
and css
tagged template literals.
- Automatic Formatting: Formats
styled-components
andcss
tagged template literals in.ts
,.tsx
, and.js
files. - Diff View: Displays changes as a diff for review.
- Auto-fix Mode: Apply fixes directly to your files with the
--fix
option. - Indentation Control: Customize the indentation style (tabs or spaces, default: 2 spaces).
- Language Support: Works seamlessly with TypeScript and JavaScript projects.
Install globally to use the CLI tool anywhere:
npm install -g styled-formatter
Add styled-formatter
as a development dependency in your project:
npm install --save-dev styled-formatter
styled-formatter
provides a simple and intuitive command-line interface for checking and fixing formatting issues.
To check for formatting issues:
styled-formatter "src/**/*.ts"
Apply fixes directly to the files:
styled-formatter "src/**/*.ts" --fix
Set custom indentation styles. By default, the indentation is set to 2 spaces:
styled-formatter "src/**/*.ts" --indent tab
styled-formatter "src/**/*.ts" --indent 4 # 4 spaces
import styled from "styled-components";
const Button = styled.button`
color:red;
font-size:12px;
`;
const Wrapper = styled.div`
border: 1px solid black;
&:hover {
color:white;
}
`;
import styled from "styled-components";
const Button = styled.button`
color: red;
font-size: 12px;
`;
const Wrapper = styled.div`
border: 1px solid black;
&:hover {
color: white;
}
`;
Option | Description |
---|---|
--fix |
Automatically apply fixes to formatting issues. |
--indent <type> |
Specify indentation style (tab or spaces). Default: 2 . |
[files...] |
Glob pattern to specify files to format. |
No additional configuration files are required. The tool uses a predefined formatting style inspired by best practices and popular tools like Prettier.
Add a script to your package.json
:
"scripts": {
"format": "styled-formatter 'src/**/*.{ts,tsx}' --fix"
}
Run the formatter:
npm run format
You can integrate styled-formatter
with tools like husky
to enforce formatting during pre-commit:
npx husky add .husky/pre-commit "npm run format"
We welcome contributions! If you encounter issues or have ideas for new features, please open an issue or submit a pull request.
-
Clone the repository:
git clone https://github.com/yudppp/styled-formatter.git
-
Install dependencies:
npm install
-
Run tests to verify changes:
npm test
-
Create a pull request with your changes.
This project is licensed under the MIT License. See the LICENSE file for details.