Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/program.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { analyze } from '@projectwallace/css-analyzer'
import { parseArgs } from 'node:util'
import { help } from './help.js'
import { Analytics } from './components.js'

export async function Program({ args, read_file, terminal_colors, stdin }) {
const format_as_json = '--json'
const help_args = ['-h', '--help']
const options = {
json: {
type: 'boolean',
short: 'j'
},
help: {
type: 'boolean',
short: 'h'
}
}

const { values, positionals } = parseArgs({
args,
options,
allowPositionals: true,
strict: false
})

// Show help if the user explicitly asked for it
if (args.some(arg => help_args.includes(arg))
// Show help if there's no input and no arguments provided
|| args.length === 0 && stdin === '') {
// Show help if the user explicitly asked for it or if no arguments were provided
if (values.help || args.length === 0 && stdin === '') {
return help(terminal_colors)
}

// path is the first param that doesn't start with -- and isn't one
// of the existing flags
const path_param = args.find(arg => {
if (arg == format_as_json) return false
if (help_args.includes(arg)) return false
if (arg.startsWith('--')) return false
return true
})
// Use the first positional argument as the file path
const path_param = positionals[0]
const css = path_param ? await read_file(path_param) : stdin

if (!css) {
Expand All @@ -31,7 +39,7 @@ export async function Program({ args, read_file, terminal_colors, stdin }) {
delete stats.__meta__

// Format as JSON if user asked for it
if (args.some(arg => arg === format_as_json)) {
if (values.json) {
return JSON.stringify(stats)
}

Expand Down