styled-formatter
v1.1.5
Published
CLI for formatting styled-components template literals
Downloads
495
Readme
styled-formatter
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.
Features
- Automatic Formatting: Formats
styled-componentsandcsstagged template literals in.ts,.tsx, and.jsfiles. - Diff View: Displays changes as a diff for review.
- Auto-fix Mode: Apply fixes directly to your files with the
--fixoption. - Indentation Control: Customize the indentation style (tabs or spaces, default: 2 spaces).
- Language Support: Works seamlessly with TypeScript and JavaScript projects.
Installation
Global Installation
Install globally to use the CLI tool anywhere:
npm install -g styled-formatterLocal Installation
Add styled-formatter as a development dependency in your project:
npm install --save-dev styled-formatterUsage
styled-formatter provides a simple and intuitive command-line interface for checking and fixing formatting issues.
Basic Command
To check for formatting issues:
styled-formatter "src/**/*.ts"Auto-fix Formatting
Apply fixes directly to the files:
styled-formatter "src/**/*.ts" --fixSpecify Indentation
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 spacesExamples
Input
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;
}
`;Output (after formatting)
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;
}
`;CLI Options
| 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. |
Configuration
No additional configuration files are required. The tool uses a predefined formatting style inspired by best practices and popular tools like Prettier.
Integration with Other Tools
Using npm scripts
Add a script to your package.json:
"scripts": {
"format": "styled-formatter 'src/**/*.{ts,tsx}' --fix"
}Run the formatter:
npm run formatGit Hooks
You can integrate styled-formatter with tools like husky to enforce formatting during pre-commit:
npx husky add .husky/pre-commit "npm run format"Contributing
We welcome contributions! If you encounter issues or have ideas for new features, please open an issue or submit a pull request.
Steps to Contribute
Clone the repository:
git clone https://github.com/yudppp/styled-formatter.gitInstall dependencies:
npm installRun tests to verify changes:
npm testCreate a pull request with your changes.
License
This project is licensed under the MIT License. See the LICENSE file for details.
