Skip to content

Commit 65eba95

Browse files
bartvenemanclaude
andauthored
breaking! feat: add CLI interface for format-css package (#170)
## Summary This PR adds a command-line interface (CLI) to the format-css package, allowing users to format CSS files directly from the terminal. ## Key Changes - **New CLI module** (`src/cli/cli.ts`): Implements a complete command-line interface with support for: - Formatting CSS files passed as arguments - Reading CSS from stdin for pipe operations - `--minify` flag to minify CSS output - `--tab-size=<n>` option to customize indentation - `--help` / `-h` flag with formatted usage documentation - Proper error handling and exit codes - **Build configuration** (`tsdown.config.ts`): Updated to build both the library and CLI: - Converted from single entry point to multi-entry configuration - Added separate build target for CLI with Node.js platform - Configured CLI to reference the main package as external to avoid duplication - **Package metadata** (`package.json`): Added `bin` field to register the `format-css` command, making it available as an executable when installed globally or locally ## Implementation Details - The CLI intelligently detects input source: file arguments, stdin pipe, or displays help if neither is provided - Uses Node.js built-in utilities (`parseArgs`, `styleText`) for argument parsing and formatted output - Validates `--tab-size` parameter to ensure it's a positive integer - Properly handles async stdin reading for piped input - Includes comprehensive help text with usage examples https://claude.ai/code/session_01QXPADk12xcj6p5nGFNJHsh Breaking because the supported node version went from >=18.0.0 to >=20.12.0 closes #123 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9be1afb commit 65eba95

18 files changed

Lines changed: 389 additions & 345 deletions

README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ Need more examples?
6565
1. Multiline tokens like **Selectors, Values, etc.** are rendered on a single line
6666
1. Unknown syntax is rendered as-is, with multi-line formatting kept intact
6767

68-
## Minify CSS
68+
## Options
69+
70+
### Minify CSS
6971

7072
This package also exposes a minifier function since minifying CSS follows many of the same rules as formatting.
7173

@@ -79,7 +81,7 @@ let minified = minify('a {}')
7981
let formatted_mini = format('a {}', { minify: true })
8082
```
8183

82-
## Tab size
84+
### Tab size
8385

8486
For cases where you cannot control the tab size with CSS there is an option to override the default tabbed indentation with N spaces.
8587

@@ -91,8 +93,36 @@ let formatted = format('a { color: red; }', {
9193
})
9294
```
9395

96+
## CLI
97+
98+
This library also ships a CLI tools that's a small wrapper around the library.
99+
100+
```
101+
USAGE
102+
format-css [options] [file...]
103+
cat styles.css | format-css [options]
104+
105+
OPTIONS
106+
--minify Minify the CSS output
107+
--tab-size=<n> Use N spaces for indentation instead of tabs
108+
--help, -h Show this help
109+
110+
EXAMPLES
111+
# Format a file
112+
format-css styles.css
113+
114+
# Format with 2-space indentation
115+
format-css styles.css --tab-size=2
116+
117+
# Minify
118+
format-css styles.css --minify
119+
120+
# Via pipe
121+
cat styles.css | format-css
122+
```
123+
94124
## Related projects
95125

96-
- [Format CSS online](https://www.projectwallace.com/prettify-css?utm_source=github&utm_medium=wallace_format_css_related_projects) - See this formatter in action online!
97-
- [Minify CSS online](https://www.projectwallace.com/minify-css?utm_source=github&utm_medium=wallace_format_css_related_projects) - See this minifier in action online!
98-
- [CSS Analyzer](https://github.com/projectwallace/css-analyzer) - The best CSS analyzer that powers all analysis on [projectwallace.com](https://www.projectwallace.com?utm_source=github&utm_medium=wallace_format_css_related_projects)
126+
- [Format CSS online](https://www.projectwallace.com/prettify-css) - See this formatter in action online!
127+
- [Minify CSS online](https://www.projectwallace.com/minify-css) - See this minifier in action online!
128+
- [CSS Analyzer](https://github.com/projectwallace/css-analyzer) - The best CSS analyzer that powers all analysis on [projectwallace.com](https://www.projectwallace.com)

0 commit comments

Comments
 (0)