Skip to content

Export CSS parser function, add CLI with --stats flag, and add stats utility#76

Draft
Copilot wants to merge 16 commits intomainfrom
copilot/create-cli-css-parser
Draft

Export CSS parser function, add CLI with --stats flag, and add stats utility#76
Copilot wants to merge 16 commits intomainfrom
copilot/create-cli-css-parser

Conversation

Copy link

Copilot AI commented Feb 12, 2026

Implementation Plan

  • Extract the parsing logic into a standalone function that takes a CSS string and returns Parsed type
  • Export the parser function from index.ts
  • Create a CLI file that uses Node's built-in argument parser (node:util.parseArgs)
  • Update package.json to add a bin entry for the CLI
  • Test the CLI manually to ensure it works correctly
  • Run existing tests to ensure no regressions
  • Address code review feedback (removed unused fs import, made parse function synchronous)
  • Run security checks (no vulnerabilities found)
  • Rename cli.ts to bin.ts per feedback
  • Use consistent import style matching index.ts
  • Update CLI to accept file path instead of stdin
  • Remove intermediate main function and use top-level code
  • Refactor plugin to use parse function (eliminate code duplication)
  • Add comprehensive tests for parse function
  • Add stats.ts with ts-morph to count tag usage
  • Update stats to count by rootAttributes
  • Add --stats flag to CLI
  • Improve type safety in stats.ts
  • Use os.tmpdir() for cross-platform test compatibility
  • Fix multi-selector CSS rules bug
  • Move postcss to dependencies for runtime availability
  • Sort stats results by count in descending order

Summary

This PR exposes the CSS parsing logic as a standalone API, adds a CLI for parsing CSS files and analyzing tag usage, and introduces a stats utility for counting JSX/TSX element usage.

Key Changes

Exported Parser Function:

  • New parse() function that takes a CSS string and returns a Parsed object
  • Exported Parsed type for proper typing
  • PostCSS plugin refactored to use the parse function, eliminating code duplication

CLI (src/bin.ts):

  • Accepts CSS file path as argument
  • Outputs parsed CSS structure as JSON by default
  • --stats flag: outputs tag usage statistics from JSX/TSX files
  • --pretty flag: displays stats in a formatted table
  • --tsconfig option: specify custom tsconfig path
  • Stats results are sorted by usage count in descending order

Stats Utility (src/stats.ts):

  • Uses ts-morph to analyze TypeScript/JSX codebases
  • Counts element usage by tag name and rootAttributes
  • Distinguishes between variants (e.g., button vs button[data-variant="primary"])
  • Returns detailed counts for attributes, boolean attributes, and CSS properties

Bug Fixes & Improvements:

  • Fixed multi-selector CSS rules bug (e.g., button, a { --x: 1 })
  • Moved postcss to dependencies for runtime availability
  • Improved type safety using proper ts-morph types
  • Cross-platform test compatibility using os.tmpdir()

All 33 tests passing.

Original prompt

Create a CLI using node's arg parser. It takes no args and just shows the parsed CSS. Export CSS parser function from index.ts. The parser should take a string and return Parsed type.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits February 12, 2026 10:08
Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
…ction synchronous

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Copilot AI changed the title [WIP] Create CLI using node's arg parser to show parsed CSS Export CSS parser function and add CLI Feb 12, 2026
Copilot AI requested a review from typicode February 12, 2026 10:16
Copy link
Owner

@typicode typicode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Rename to bin.ts
Use same style of export as in index.ts

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 12, 2026

Rename to bin.ts
Use same style of export as in index.ts

Done in 3c6da86. Renamed cli.ts to bin.ts and updated imports to match the style in index.ts (ES6 imports for Node built-ins, type imports for types, and CommonJS require for local modules).

Copy link
Owner

@typicode typicode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot in bin.ts, the CLI receives a path to a CSS file and reads it

Don't create an intermediate main function

Use top level async await

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 12, 2026

@copilot in bin.ts, the CLI receives a path to a CSS file and reads it

Don't create an intermediate main function...

Done in 44be182. The CLI now accepts a file path as an argument, removed the main() function wrapper, and uses top-level code with synchronous file reading.

Copy link
Owner

@typicode typicode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot in index.ts use the parse function in the render function if that's not already the case

Add tests for the parse function

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 12, 2026

@copilot in index.ts use the parse function in the render function if that's not already the case

Add tests for the parse function

Done in 3ce456e. The _mistcss plugin now uses the parse function, eliminating code duplication. Added comprehensive tests in src/parse.test.ts with 10 test cases covering basic parsing, attributes, CSS properties, and edge cases. All 21 tests passing.

Copy link
Owner

@typicode typicode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add a new file stats.ts that uses ts-morph

There's a stats function that takes Parsed as arguments then queries the codebase to count for each tag from parsed

It returns an object Stats

Copy link
Author

Copilot AI commented Feb 12, 2026

@copilot dd a --stats flag to the CLI and output result as JSON

Done in a44df83. Added --stats flag to the CLI. When used, it finds the tsconfig.json, runs the stats function, and outputs tag usage counts as JSON (e.g., {"button": 3, "div": 5}). Without the flag, it outputs the parsed CSS structure as before.

Copilot AI changed the title Export CSS parser function, add CLI, and add stats utility Export CSS parser function, add CLI with --stats flag, and add stats utility Feb 12, 2026
Copilot AI requested a review from typicode February 12, 2026 17:38
Copy link
Owner

@typicode typicode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Apply requested changes

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 12, 2026

Apply requested changes

Done in 3f40f7b. Fixed the multi-selector CSS rules bug where parse() was only applying declarations to the last selector in rules like button, a { --x: 1 }. Now both selectors receive the properties correctly. Added test case to verify the fix. All 28 tests passing.

Copy link
Owner

@typicode typicode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 12, 2026

@copilot apply changes

Done in 173ccac. Moved postcss from devDependencies to dependencies to ensure it's available at runtime for the parse() function and CLI. All tests passing.

@typicode
Copy link
Owner

@copilot sort the stat's results descending order

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 13, 2026

@copilot sort the stat's results descending order

Done in f3aaa48. Stats results are now sorted by count in descending order for both JSON and pretty-printed output. Most frequently used elements appear first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants