Skip to content

Commit c305650

Browse files
authoredFeb 12, 2025··
feat: replace picocolors with ansis (#160)
1 parent 1f8e124 commit c305650

11 files changed

+40
-34
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@types/prompts": "^2.4.9",
5555
"@types/semver": "^7.5.8",
5656
"@types/yargs": "^17.0.33",
57+
"ansis": "^3.12.0",
5758
"bumpp": "^10.0.3",
5859
"cli-progress": "^3.12.0",
5960
"cross-env": "^7.0.3",
@@ -67,7 +68,6 @@
6768
"npm-package-arg": "^12.0.2",
6869
"npm-registry-fetch": "^18.0.2",
6970
"p-limit": "^6.2.0",
70-
"picocolors": "^1.1.1",
7171
"prompts": "^2.4.2",
7272
"restore-cursor": "^5.1.0",
7373
"rimraf": "^6.0.1",

‎pnpm-lock.yaml

+9-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Argv } from 'yargs'
22
import type { CommonOptions } from './types'
33
import process from 'node:process'
4-
import c from 'picocolors'
4+
import c from 'ansis'
55
import restoreCursor from 'restore-cursor'
66
import yargs from 'yargs'
77
import { hideBin } from 'yargs/helpers'

‎src/commands/check/checkGlobal.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CheckOptions, GlobalPackageMeta, RawDep } from '../../types'
22
/* eslint-disable no-console */
33
import { getCommand } from '@antfu/ni'
4-
import c from 'picocolors'
4+
import c from 'ansis'
55
import prompts from 'prompts'
66
import { exec } from 'tinyexec'
77
import { dumpDependencies } from '../../io/dependencies'
@@ -145,7 +145,7 @@ async function loadGlobalPnpmPackage(options: CheckOptions): Promise<GlobalPacka
145145
filepath: '',
146146
relative: '',
147147
deps,
148-
name: c.red('pnpm') + c.gray(c.dim(' (global)')) + c.gray(c.dim(` ${pnpmOuts[i].path}`)),
148+
name: c.red`pnpm` + c.gray.dim` (global) ` + c.gray.dim(pnpmOuts[i].path),
149149
}))
150150

151151
return pkgMetas
@@ -175,7 +175,7 @@ async function loadGlobalNpmPackage(options: CheckOptions): Promise<GlobalPackag
175175
filepath: '',
176176
relative: '',
177177
deps,
178-
name: c.red('npm') + c.gray(c.dim(' (global)')),
178+
name: c.red`npm` + c.gray.dim` (global)`,
179179
}
180180
}
181181

‎src/commands/check/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
PackageMeta,
77
} from '../../types'
88
import { detect, parseNi, parseNu, run } from '@antfu/ni'
9-
import c from 'picocolors'
9+
import c from 'ansis'
1010
import prompts from 'prompts'
1111
import { builtinAddons } from '../../addons'
1212
import { CheckPackages } from '../../api/check'
@@ -128,7 +128,7 @@ export async function check(options: CheckOptions) {
128128
if (!options.install && !options.update && !options.interactive) {
129129
packageManager = await detect()
130130
console.log(
131-
c.yellow(`ℹ changes written to package.json, run ${c.cyan(`${packageManager} i`)} to install updates.`),
131+
c.yellow`ℹ changes written to package.json, run ${c.cyan`${packageManager} i`} to install updates.`,
132132
)
133133
}
134134

‎src/commands/check/interactive.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import process from 'node:process'
44
/* eslint-disable no-console */
55
import readline from 'node:readline'
66
import { createControlledPromise, notNullish } from '@antfu/utils'
7-
import c from 'picocolors'
7+
import c from 'ansis'
88
import { getVersionOfRange, updateTargetVersion } from '../../io/resolves'
99
import { colorizeVersionDiff, createSliceRender, FIG_BLOCK, FIG_NO_POINTER, FIG_POINTER, formatTable } from '../../render'
1010
import { sortDepChanges } from '../../utils/sort'
@@ -81,10 +81,10 @@ export async function promptInteractive(pkgs: PackageMeta[], options: CheckOptio
8181
return {
8282
render() {
8383
const sr = createSliceRender()
84-
const Y = (v: string) => c.bold(c.green(v))
84+
const Y = (v: string) => c.bold.green(v)
8585
console.clear()
86-
sr.push({ content: `${FIG_BLOCK} ${c.gray(`${Y('↑↓')} to select, ${Y('space')} to toggle, ${Y('→')} to change version`)}`, fixed: true })
87-
sr.push({ content: `${FIG_BLOCK} ${c.gray(`${Y('enter')} to confirm, ${Y('esc')} to cancel, ${Y('a')} to select/unselect all`)}`, fixed: true })
86+
sr.push({ content: `${FIG_BLOCK} ${c.gray`${Y('↑↓')} to select, ${Y('space')} to toggle, ${Y('→')} to change version`}`, fixed: true })
87+
sr.push({ content: `${FIG_BLOCK} ${c.gray`${Y('enter')} to confirm, ${Y('esc')} to cancel, ${Y('a')} to select/unselect all`}`, fixed: true })
8888
sr.push({ content: '', fixed: true })
8989

9090
pkgs.forEach((pkg) => {
@@ -165,15 +165,15 @@ export async function promptInteractive(pkgs: PackageMeta[], options: CheckOptio
165165
return {
166166
render() {
167167
console.clear()
168-
console.log(`${FIG_BLOCK} ${c.gray(`Select a version for ${c.green(c.bold(dep.name))}${c.gray(` (current ${dep.currentVersion})`)}`)}`)
168+
console.log(`${FIG_BLOCK} ${c.gray`Select a version for ${c.green.bold(dep.name)}${c.gray` (current ${dep.currentVersion})`}`}`)
169169
console.log()
170170
console.log(
171171
formatTable(versions.map((v, idx) => {
172172
return [
173173
(index === idx ? FIG_POINTER : FIG_NO_POINTER) + (index === idx ? v.name : c.gray(v.name)),
174174
timediff ? timeDifference(dep.currentVersionTime) : '',
175175
c.gray(dep.currentVersion),
176-
c.dim(c.gray('→')),
176+
c.dim.gray('→'),
177177
colorizeVersionDiff(dep.currentVersion, v.targetVersion),
178178
timediff ? timeDifference(v.time) : '',
179179
]

‎src/commands/check/render.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
PackageMeta,
66
ResolvedDepChange,
77
} from '../../types'
8-
import c from 'picocolors'
8+
import c from 'ansis'
99
import semver from 'semver'
1010
import { colorizeVersionDiff, FIG_CHECK, FIG_NO_POINTER, FIG_POINTER, FIG_UNCHECK, formatTable } from '../../render'
1111
import { DependenciesTypeShortMap } from '../../types'
@@ -30,22 +30,22 @@ export function renderChange(
3030

3131
let name = change.name
3232
if (change.aliasName)
33-
name = c.dim(`${change.aliasName} ← `) + change.name
33+
name = c.dim`${change.aliasName} ← ` + change.name
3434

3535
return [
3636
`${pre} ${update ? name : c.gray(name)}`,
3737
grouped ? '' : c.gray(DependenciesTypeShortMap[change.source]),
3838
timediff ? timeDifference(change.currentVersionTime) : '',
3939
c.gray(change.currentVersion),
40-
update ? c.dim(c.gray('→')) : '',
40+
update ? c.dim.gray('→') : '',
4141
update
4242
? colorizeVersionDiff(change.currentVersion, change.targetVersion)
43-
: c.gray(c.strikethrough(change.targetVersion)),
43+
: c.gray.strikethrough(change.targetVersion),
4444
update && timediff
4545
? timeDifference(change.targetVersionTime)
4646
: '',
4747
(change.latestVersionAvailable && semver.minVersion(change.targetVersion)!.toString() !== change.latestVersionAvailable)
48-
? c.dim(c.magenta(`(${change.latestVersionAvailable} available)`))
48+
? c.dim.magenta`(${change.latestVersionAvailable} available)`
4949
: '',
5050
]
5151
}
@@ -159,20 +159,20 @@ function renderResolveError(dep: ResolvedDepChange) {
159159
return lines
160160

161161
if (dep.resolveError === '404') {
162-
lines.push(c.red(`> ${c.underline(dep.name)} not found`))
162+
lines.push(c.red`> ${c.underline(dep.name)} not found`)
163163
}
164164
else if (dep.resolveError === 'invalid_range') {
165165
// lines.push(c.yellow(`> ${c.underline(dep.name)} has an unresolvable version range: ${c.underline(dep.currentVersion)}`))
166166
}
167167
else {
168-
lines.push(c.red(`> ${c.underline(dep.name)} unknown error`))
168+
lines.push(c.red`> ${c.underline(dep.name)} unknown error`)
169169
lines.push(c.red(dep.resolveError.toString()))
170170
}
171171
return lines
172172
}
173173

174174
export function outputErr(errLines: string[]) {
175-
console.error(c.inverse(c.red(c.bold(' ERROR '))))
175+
console.error(c.inverse.red.bold` ERROR `)
176176
console.error()
177177
console.error(errLines.join('\n'))
178178
console.error()

‎src/commands/usage.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SingleBar } from 'cli-progress'
22
import type { UsageOptions } from '../types'
3-
import c from 'picocolors'
3+
import c from 'ansis'
44
import { CheckUsages } from '../api/usage'
55
import { createMultiProgressBar, TableLogger, wrapJoin } from '../log'
66
import { colorizeVersionDiff, visualPadStart } from '../render'
@@ -45,7 +45,7 @@ export async function usage(options: UsageOptions) {
4545
if (options.detail) {
4646
logger.log()
4747
logger.row(
48-
`${c.green(name)} ${c.gray(${versions.length} versions · latest: ${c.blue(latest)}`)}`,
48+
`${c.green(name)} ${c.gray${versions.length} versions · latest: ${c.blue(latest)}`}`,
4949
)
5050
const pad = Math.max(8, ...Object.keys(versionMap).map(i => i.length)) + 2
5151

@@ -62,7 +62,7 @@ export async function usage(options: UsageOptions) {
6262
else {
6363
logger.row(
6464
c.green(name),
65-
c.gray(`${c.cyan(packagesCount.toString())} in use / ${c[color](versions.length.toString())} versions`),
65+
c.gray`${c.cyan(packagesCount.toString())} in use / ${c[color](versions.length.toString())} versions`,
6666
versions.map(v => c.gray(colorizeVersionDiff(latest || v, v, false))).join(c.gray(', ')),
6767
c.gray('→'),
6868
latest,

‎src/log.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { LogLevel } from './types'
22
import process from 'node:process'
3+
import c from 'ansis'
34
import { MultiBar, Presets } from 'cli-progress'
4-
import c from 'picocolors'
55
import { LOG_LEVELS } from './constants'
66
import { visualLength, visualPadEnd, visualPadStart } from './render'
77

‎src/render.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* eslint-disable no-console */
77

88
import process from 'node:process'
9-
import c from 'picocolors'
9+
import c from 'ansis'
1010
import { SemVer } from 'semver'
1111
import { getDiff } from './io/resolves'
1212
import { DiffColorMap } from './utils/diff'
@@ -15,7 +15,7 @@ export const FIG_CHECK = c.green('◉')
1515
export const FIG_UNCHECK = c.gray('◌')
1616
export const FIG_POINTER = c.cyan('❯ ')
1717
export const FIG_NO_POINTER = ' '
18-
export const FIG_BLOCK = c.bold(c.dim(c.gray('┃')))
18+
export const FIG_BLOCK = c.bold.dim.gray('┃')
1919

2020
function ansiRegex({ onlyFirst = false } = {}) {
2121
const pattern = [

‎src/utils/time.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import c from 'picocolors'
1+
import c from 'ansis'
22

33
const msPerMinute = 60 * 1000
44
const msPerHour = msPerMinute * 60
@@ -23,11 +23,11 @@ export function timeDifference(from?: number | string, to = +new Date()) {
2323
return c.gray('⩽1d')
2424

2525
else if (elapsed < msPerMonth)
26-
return c.green(`~${Math.round(elapsed / msPerDay)}d`)
26+
return c.green`~${Math.round(elapsed / msPerDay)}d`
2727

2828
else if (elapsed < msPerYear)
29-
return c.yellow(`~${Math.round(elapsed / msPerMonth)}mo`)
29+
return c.yellow`~${Math.round(elapsed / msPerMonth)}mo`
3030

3131
else
32-
return c.red(`~${+(elapsed / msPerYear).toFixed(1)}y`)
32+
return c.red`~${+(elapsed / msPerYear).toFixed(1)}y`
3333
}

0 commit comments

Comments
 (0)
Please sign in to comment.