Skip to content

Commit 64ac888

Browse files
committed
add error, info, success, and warn helpers
1 parent 64850e0 commit 64ac888

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

packages/@tailwindcss-upgrade/src/index.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import fastGlob from 'fast-glob'
44
import { execSync } from 'node:child_process'
55
import path from 'node:path'
6-
import pc from 'picocolors'
76
import { help } from './commands/help'
87
import { migrate } from './migrate'
98
import { args, type Arg } from './utils/args'
10-
import { eprintln, header, highlight, wordWrap } from './utils/renderer'
9+
import { eprintln, error, header, highlight, info, success } from './utils/renderer'
1110

1211
const options = {
1312
'--help': { type: 'boolean', description: 'Display usage information', alias: '-h' },
@@ -31,15 +30,8 @@ async function run() {
3130
if (!flags['--force']) {
3231
let stdout = execSync('git status --porcelain', { encoding: 'utf-8' })
3332
if (stdout.trim()) {
34-
wordWrap(
35-
'Git directory is not clean. Please stash or commit your changes before migrating.',
36-
process.stderr.columns - 5 - 4,
37-
).map((line) => eprintln(`${pc.red('\u2502')} ${line}`))
38-
wordWrap(
39-
`You may use the ${highlight('--force')} flag to override this safety check.`,
40-
process.stderr.columns - 2 - 4,
41-
).map((line) => eprintln(`${pc.red('\u2502')} ${line}`))
42-
eprintln()
33+
error('Git directory is not clean. Please stash or commit your changes before migrating.')
34+
info(`You may use the ${highlight('--force')} flag to override this safety check.`)
4335
process.exit(1)
4436
}
4537
}
@@ -49,11 +41,9 @@ async function run() {
4941

5042
// Discover CSS files in case no files were provided
5143
if (files.length === 0) {
52-
wordWrap(
44+
info(
5345
'No files provided. Searching for CSS files in the current directory and its subdirectories…',
54-
process.stderr.columns - 5 - 4,
55-
).map((line) => eprintln(`${pc.blue('\u2502')} ${line}`))
56-
eprintln()
46+
)
5747

5848
files = await fastGlob(['**/*.css'], {
5949
absolute: true,
@@ -70,17 +60,9 @@ async function run() {
7060
// Figure out if we made any changes
7161
let stdout = execSync('git status --porcelain', { encoding: 'utf-8' })
7262
if (stdout.trim()) {
73-
wordWrap(
74-
'Migration complete. Verify the changes and commit them to your repository.',
75-
process.stderr.columns - 5 - 4,
76-
).map((line) => eprintln(`${pc.green('\u2502')} ${line}`))
77-
eprintln()
63+
success('Migration complete. Verify the changes and commit them to your repository.')
7864
} else {
79-
wordWrap(
80-
'Migration complete. No changes were made to your repository.',
81-
process.stderr.columns - 5 - 4,
82-
).map((line) => eprintln(`${pc.green('\u2502')} ${line}`))
83-
eprintln()
65+
success('Migration complete. No changes were made to your repository.')
8466
}
8567
}
8668

packages/@tailwindcss-upgrade/src/utils/renderer.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ export function indent(value: string, offset = 0) {
8383
return `${' '.repeat(offset + UI.indent)}${value}`
8484
}
8585

86+
export function success(message: string, print = eprintln) {
87+
wordWrap(message, process.stderr.columns - 3).map((line) => {
88+
return print(`${pc.green('\u2502')} ${line}`)
89+
})
90+
print()
91+
}
92+
93+
export function info(message: string, print = eprintln) {
94+
wordWrap(message, process.stderr.columns - 3).map((line) => {
95+
return print(`${pc.blue('\u2502')} ${line}`)
96+
})
97+
print()
98+
}
99+
100+
export function error(message: string, print = eprintln) {
101+
wordWrap(message, process.stderr.columns - 3).map((line) => {
102+
return print(`${pc.red('\u2502')} ${line}`)
103+
})
104+
print()
105+
}
106+
107+
export function warn(message: string, print = eprintln) {
108+
wordWrap(message, process.stderr.columns - 3).map((line) => {
109+
return print(`${pc.yellow('\u2502')} ${line}`)
110+
})
111+
print()
112+
}
113+
86114
// Rust inspired functions to print to the console:
87115

88116
export function eprintln(value = '') {

0 commit comments

Comments
 (0)