Skip to content
This repository has been archived by the owner on Feb 12, 2025. It is now read-only.

Commit

Permalink
feat: replace picocolors with ansis
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Feb 11, 2025
1 parent 635aec0 commit 42e2322
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
"prepare": "simple-git-hooks"
},
"dependencies": {
"ansis": "^3.12.0",
"boxen": "^8.0.1",
"cac": "^6.7.14",
"cli-progress": "^3.12.0",
"node-modules-tools": "0.0.0",
"picocolors": "^1.1.1",
"picomatch": "^4.0.2"
},
"devDependencies": {
Expand Down
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

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

36 changes: 17 additions & 19 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type { PackageModuleType, ResolvedPackageNode } from 'node-modules-tools'
import process from 'node:process'
import ansi from 'ansis'
import boxen from 'boxen'
import { cac } from 'cac'
import { Presets, SingleBar } from 'cli-progress'
import { analyzePackage, listPackageDependencies } from 'node-modules-tools'
import pc from 'picocolors'
import { version } from '../package.json'
import { constructPatternFilter } from './utils'

const cli = cac('are-we-esm')

const colorMap: Record<PackageModuleType, (str: string) => string> = {
esm: pc.green,
dual: pc.cyan,
faux: pc.magenta,
cjs: pc.yellow,
esm: ansi.green,
dual: ansi.cyan,
faux: ansi.magenta,
cjs: ansi.yellow,
}

const types = ['esm', 'dual', 'faux', 'cjs'] as PackageModuleType[]
Expand All @@ -23,7 +23,7 @@ const nonEsmTypes = ['faux', 'cjs'] as PackageModuleType[]
function c(type: PackageModuleType, str: string, bold = false): string {
let colored = colorMap[type](str)
if (bold)
colored = pc.bold(colored)
colored = ansi.bold(colored)
return colored
}

Expand Down Expand Up @@ -66,7 +66,7 @@ cli
const bar = new SingleBar({
clearOnComplete: true,
hideCursor: true,
format: `{bar} {value}/{total} ${pc.gray('{name}')}`,
format: `{bar} {value}/{total} ${ansi.gray('{name}')}`,
linewrap: false,
barsize: 40,
}, Presets.shades_grey)
Expand Down Expand Up @@ -101,9 +101,9 @@ cli
const filtered = resolved.filter(x => x.module === type)
if (options.list && filtered.length && (options.all || nonEsmTypes.includes(type))) {
console.log()
console.log(pc.inverse(c(type, ` ${type.toUpperCase()} `, true)), c(type, `${filtered.length} packages:`))
console.log(ansi.inverse(c(type, ` ${type.toUpperCase()} `, true)), c(type, `${filtered.length} packages:`))
console.log()
console.log(filtered.map(x => ` ${c(type, x.name)}${pc.dim(`@${x.version}`)}`).join('\n'))
console.log(filtered.map(x => ` ${c(type, x.name)}${ansi.dim(`@${x.version}`)}`).join('\n'))
}
count[type] = filtered.length
}
Expand All @@ -128,28 +128,26 @@ cli

let pkgCountString = ''
if (total > 1) {
pkgCountString = ' '.repeat(5) + Object.entries(pkgCount).map(([type, count]) => c(type as PackageModuleType, String(count)).trim()).join(pc.gray(' | '))
pkgCountString = ' '.repeat(5) + Object.entries(pkgCount).map(([type, count]) => c(type as PackageModuleType, String(count)).trim()).join(ansi.gray(' | '))
}
console.log(`\n${c(type, pkg.name)}${pc.dim(`@${pkg.version}`)} ${pkgCountString}`)
console.log(`\n${c(type, pkg.name)}${ansi.dim(`@${pkg.version}`)} ${pkgCountString}`)
for (const dep of deps) {
if (!options.all && !nonEsmTypes.includes(dep.module))
continue
console.log(` ${pc.dim('|')} ${c(dep.module, dep.name)}${pc.dim(`@${dep.version}`)}`)
console.log(` ${ansi.dim('|')} ${c(dep.module, dep.name)}${ansi.dim(`@${dep.version}`)}`)
}
}
}

if (!options.all) {
console.log()
console.log(pc.gray(
`Listing non-ESM packages in flat tree, pass ${pc.cyan('--all --list')} to list all packages`,
))
console.log(ansi.gray`Listing non-ESM packages in flat tree, pass ${ansi.cyan('--all --list')} to list all packages`)
}

if (count.cjs || count.faux) {
console.log()
console.log(boxen(
` Run ${pc.cyan('pnpm why <package>@<version>')} to find out why you have a package `,
` Run ${ansi.cyan('pnpm why <package>@<version>')} to find out why you have a package `,
{
borderColor: 'gray',
borderStyle: 'singleDouble',
Expand All @@ -162,7 +160,7 @@ cli
const esmRatio = (count.dual + count.esm) / resolved.length
const summary = [
'',
`${pc.blue(pc.bold(String(resolved.length).padStart(8, ' ')))} total packages checked`,
`${ansi.blue.bold(String(resolved.length).padStart(8, ' '))} total packages checked`,
'',
]

Expand All @@ -173,14 +171,14 @@ cli

summary.push(
'',
`${pc.green(pc.bold(`${(esmRatio * 100).toFixed(1)}%`.padStart(8, ' ')))} of packages are ESM-compatible`,
`${ansi.green.bold`${(esmRatio * 100).toFixed(1)}%`.padStart(8, ' ')} of packages are ESM-compatible`,
'',
)

console.log(boxen(
summary.join('\n'),
{
title: `${pc.inverse(pc.bold(' Are We ESM? '))} ${pc.dim(`v${version}`)}`,
title: `${ansi.inverse.bold` Are We ESM? `} ${ansi.dim`v${version}`}`,
borderColor: 'gray',
borderStyle: 'round',
padding: {
Expand Down

0 comments on commit 42e2322

Please sign in to comment.