11import { analyze } from '@projectwallace/css-analyzer'
2+ import { parseArgs } from 'node:util'
23import { help } from './help.js'
34import { Analytics } from './components.js'
45
56export async function Program ( { args, read_file, terminal_colors, stdin } ) {
6- const format_as_json = '--json'
7- const help_args = [ '-h' , '--help' ]
7+ const options = {
8+ json : {
9+ type : 'boolean' ,
10+ short : 'j'
11+ } ,
12+ help : {
13+ type : 'boolean' ,
14+ short : 'h'
15+ }
16+ }
17+
18+ const { values, positionals } = parseArgs ( {
19+ args,
20+ options,
21+ allowPositionals : true ,
22+ strict : false
23+ } )
824
9- // Show help if the user explicitly asked for it
10- if ( args . some ( arg => help_args . includes ( arg ) )
11- // Show help if there's no input and no arguments provided
12- || args . length === 0 && stdin === '' ) {
25+ // Show help if the user explicitly asked for it or if no arguments were provided
26+ if ( values . help || args . length === 0 && stdin === '' ) {
1327 return help ( terminal_colors )
1428 }
1529
16- // path is the first param that doesn't start with -- and isn't one
17- // of the existing flags
18- const path_param = args . find ( arg => {
19- if ( arg == format_as_json ) return false
20- if ( help_args . includes ( arg ) ) return false
21- if ( arg . startsWith ( '--' ) ) return false
22- return true
23- } )
30+ // Use the first positional argument as the file path
31+ const path_param = positionals [ 0 ]
2432 const css = path_param ? await read_file ( path_param ) : stdin
2533
2634 if ( ! css ) {
@@ -31,7 +39,7 @@ export async function Program({ args, read_file, terminal_colors, stdin }) {
3139 delete stats . __meta__
3240
3341 // Format as JSON if user asked for it
34- if ( args . some ( arg => arg === format_as_json ) ) {
42+ if ( values . json ) {
3543 return JSON . stringify ( stats )
3644 }
3745
0 commit comments