-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathrenderer.ts
140 lines (114 loc) · 3.65 KB
/
renderer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import fs from 'node:fs'
import path from 'node:path'
import { stripVTControlCharacters } from 'node:util'
import pc from 'picocolors'
import { resolve } from '../utils/resolve'
import { formatNanoseconds } from './format-ns'
export const UI = {
indent: 2,
}
export function header() {
return `${pc.italic(pc.bold(pc.blue('\u2248')))} tailwindcss ${pc.blue(`v${getVersion()}`)}`
}
export function highlight(file: string) {
return `${pc.dim(pc.blue('`'))}${pc.blue(file)}${pc.dim(pc.blue('`'))}`
}
/**
* Convert an `absolute` path to a `relative` path from the current working
* directory.
*/
export function relative(
to: string,
from = process.cwd(),
{ preferAbsoluteIfShorter = true } = {},
) {
let result = path.relative(from, to)
if (!result.startsWith('..')) {
result = `.${path.sep}${result}`
}
if (preferAbsoluteIfShorter && result.length > to.length) {
return to
}
return result
}
/**
* Wrap `text` into multiple lines based on the `width`.
*/
export function wordWrap(text: string, width: number): string[] {
// Handle text with newlines by maintaining the newlines, then splitting
// each line separately.
if (text.includes('\n')) {
return text.split('\n').flatMap((line) => wordWrap(line, width))
}
let words = text.split(' ')
let lines = []
let line = ''
let lineLength = 0
for (let word of words) {
let wordLength = stripVTControlCharacters(word).length
if (lineLength + wordLength + 1 > width) {
lines.push(line)
line = ''
lineLength = 0
}
line += (lineLength ? ' ' : '') + word
lineLength += wordLength + (lineLength ? 1 : 0)
}
if (lineLength) {
lines.push(line)
}
return lines
}
/**
* Format a duration in nanoseconds to a more human readable format.
*/
export function formatDuration(ns: bigint) {
let formatted = formatNanoseconds(ns)
if (ns <= 50 * 1e6) return pc.green(formatted)
if (ns <= 300 * 1e6) return pc.blue(formatted)
if (ns <= 1000 * 1e6) return pc.yellow(formatted)
return pc.red(formatted)
}
export function indent(value: string, offset = 0) {
return `${' '.repeat(offset + UI.indent)}${value}`
}
function log(message: string, { art = pc.gray('\u2502'), prefix = '', print = eprintln }) {
let prefixLength = prefix.length
let padding = ' '
let paddingLength = padding.length
let artLength = stripVTControlCharacters(art).length
let availableWidth = process.stderr.columns
let totalWidth = availableWidth - prefixLength - paddingLength * 2 - artLength
wordWrap(message, totalWidth).map((line, idx) => {
return print(
`${art}${padding}${idx === 0 ? prefix : ' '.repeat(prefixLength)}${line}${padding}`,
)
})
print()
}
export function success(message: string, { prefix = '', print = eprintln } = {}) {
log(message, { art: pc.green('\u2502'), prefix, print })
}
export function info(message: string, { prefix = '', print = eprintln } = {}) {
log(message, { art: pc.blue('\u2502'), prefix, print })
}
export function error(message: string, { prefix = '', print = eprintln } = {}) {
log(message, { art: pc.red('\u2502'), prefix, print })
}
export function warn(message: string, { prefix = '', print = eprintln } = {}) {
log(message, { art: pc.yellow('\u2502'), prefix, print })
}
// Rust inspired functions to print to the console:
export function eprintln(value = '') {
process.stderr.write(`${value}\n`)
}
export function println(value = '') {
process.stdout.write(`${value}\n`)
}
function getVersion(): string {
if (typeof globalThis.__tw_version === 'string') {
return globalThis.__tw_version
}
let { version } = JSON.parse(fs.readFileSync(resolve('tailwindcss/package.json'), 'utf-8'))
return version
}