Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@
"test": "vitest run --coverage",
"build": "tsdown",
"lint-package": "publint",
"lint": "oxlint; oxfmt --check"
"lint": "oxlint; oxfmt --check",
"check": "tsc --noEmit"
},
"dependencies": {
"@projectwallace/css-analyzer": "^9.3.0"
"@projectwallace/css-analyzer": "^9.6.0"
},
"devDependencies": {
"@types/node": "^22.0.0",
"@vitest/coverage-v8": "^4.1.0",
"oxfmt": "^0.43.0",
"oxlint": "^1.51.0",
"oxlint": "^1.58.0",
"publint": "^0.3.18",
"tsdown": "^0.21.4",
"typescript": "^6.0.2",
Expand Down
7 changes: 7 additions & 0 deletions src/__fixtures__/small.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@
"content": 1,
"zoom": 1
},
"shorthands": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0,
"ratio": 0
},
"uniquenessRatio": 1,
"prefixed": {
"total": 0,
Expand Down
23 changes: 10 additions & 13 deletions src/components.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { analyze } from '@projectwallace/css-analyzer'
import { to_filesize, to_number, to_percentage, pad_end, pad_start } from './formatters.js'
import type { Colors } from './types.js'

type Stats = ReturnType<typeof analyze>
import type { Colors, CssAnalysis } from './types.js'

const columns = [19, 12, 12, 12]
const width = columns.reduce((total, num) => (total += num), 0) + columns.length

export function Analytics(stats: Stats, style: Colors): string {
export function Analytics(stats: CssAnalysis, style: Colors): string {
function Row(...tds: unknown[]): string {
return tds
.map((td, index) => {
Expand All @@ -23,7 +20,7 @@ export function Analytics(stats: Stats, style: Colors): string {
return style.dim(''.padEnd(width, '─'))
}

function Summary(stats: Stats): string {
function Summary(stats: CssAnalysis): string {
return [
Hr(),
['Lines of Code', 'Filesize', 'Rules', 'Selectors', 'Declarations'].join(style.dim(' │ ')),
Expand All @@ -40,7 +37,7 @@ export function Analytics(stats: Stats, style: Colors): string {
].join('\n')
}

function Stylesheet(stylesheet: Stats['stylesheet']): string {
function Stylesheet(stylesheet: CssAnalysis['stylesheet']): string {
return [
Row(
'Comments',
Expand All @@ -55,7 +52,7 @@ export function Analytics(stats: Stats, style: Colors): string {
].join('\n')
}

function Rules(rules: Stats['rules']): string {
function Rules(rules: CssAnalysis['rules']): string {
let empty_count = to_number(rules.empty.total)
return [
Row(
Expand Down Expand Up @@ -85,7 +82,7 @@ export function Analytics(stats: Stats, style: Colors): string {
].join('\n')
}

function Selectors(selectors: Stats['selectors']): string {
function Selectors(selectors: CssAnalysis['selectors']): string {
return [
Row(
style.underline('Selectors'),
Expand Down Expand Up @@ -134,7 +131,7 @@ export function Analytics(stats: Stats, style: Colors): string {
].join('\n')
}

function AtRules(atrules: Stats['atrules']): string {
function AtRules(atrules: CssAnalysis['atrules']): string {
const { media, supports, fontface, import: imports, keyframes, container, property } = atrules

return [
Expand Down Expand Up @@ -189,7 +186,7 @@ export function Analytics(stats: Stats, style: Colors): string {
].join('\n')
}

function Declarations(declarations: Stats['declarations']): string {
function Declarations(declarations: CssAnalysis['declarations']): string {
return [
Row(
style.underline('Declarations'),
Expand All @@ -207,7 +204,7 @@ export function Analytics(stats: Stats, style: Colors): string {
].join('\n')
}

function Properties(properties: Stats['properties']): string {
function Properties(properties: CssAnalysis['properties']): string {
return [
Row(
style.underline('Properties'),
Expand Down Expand Up @@ -242,7 +239,7 @@ export function Analytics(stats: Stats, style: Colors): string {
].join('\n')
}

function Values(values: Stats['values']): string {
function Values(values: CssAnalysis['values']): string {
function ValueRow(
title: string,
total: number,
Expand Down
9 changes: 6 additions & 3 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseArgs } from 'node:util'
import { help } from './help.js'
import { Analytics } from './components.js'
export type { Colors } from './types.js'
import type { Colors } from './types.js'
import type { Colors, CssAnalysis } from './types.js'

type ProgramOptions = {
args: string[]
Expand Down Expand Up @@ -49,13 +49,16 @@ export async function Program({
return help(terminal_colors)
}

const stats = analyze(css)
const stats = analyze(css, { useLocations: false })
delete (stats as Record<string, unknown>).__meta__

// Format as JSON if user asked for it
if (values.json) {
return JSON.stringify(stats)
}

return Analytics(stats, terminal_colors)
// The analyze function has overloads and TypeScript's ReturnType only captures the last overload's return type,
// making the two variants structurally incompatible for a direct cast.
// The as unknown as CssAnalysis double assertion is the standard escape hatch for this.
return Analytics(stats as unknown as CssAnalysis, terminal_colors)
}
3 changes: 2 additions & 1 deletion src/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest'
import { readFile } from 'node:fs/promises'
import { analyze } from '@projectwallace/css-analyzer'
import { Analytics } from './components.js'
import { CssAnalysis } from './types.js'
// import { writeFileSync } from 'node:fs'

const terminal_colors = {
Expand All @@ -24,7 +25,7 @@ describe('Smoke Tests', () => {
readFile(`./src/__fixtures__/${fileName}.css`, 'utf-8'),
readFile(`./src/__fixtures__/${fileName}.txt`, 'utf-8'),
])
const stats = analyze(css)
const stats = analyze(css) as unknown as CssAnalysis
const actual = Analytics(stats, terminal_colors)
// writeFileSync(`./src/__fixtures__/${fileName}.txt`, actual)
expect(actual).toEqual(expected)
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { analyze } from '@projectwallace/css-analyzer'

export type CssAnalysis = ReturnType<typeof analyze>

export type Colors = {
bold: (str: string) => string
dim: (str: string) => string
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true,
"outDir": "./dist"
"outDir": "./dist",
"rootDir": "src",
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts", "node_modules"]
"exclude": ["node_modules"]
}