Skip to content

Commit

Permalink
add error, info, success, and warn helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Sep 16, 2024
1 parent 64850e0 commit 64ac888
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
32 changes: 7 additions & 25 deletions packages/@tailwindcss-upgrade/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import fastGlob from 'fast-glob'
import { execSync } from 'node:child_process'
import path from 'node:path'
import pc from 'picocolors'
import { help } from './commands/help'
import { migrate } from './migrate'
import { args, type Arg } from './utils/args'
import { eprintln, header, highlight, wordWrap } from './utils/renderer'
import { eprintln, error, header, highlight, info, success } from './utils/renderer'

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

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

files = await fastGlob(['**/*.css'], {
absolute: true,
Expand All @@ -70,17 +60,9 @@ async function run() {
// Figure out if we made any changes
let stdout = execSync('git status --porcelain', { encoding: 'utf-8' })
if (stdout.trim()) {
wordWrap(
'Migration complete. Verify the changes and commit them to your repository.',
process.stderr.columns - 5 - 4,
).map((line) => eprintln(`${pc.green('\u2502')} ${line}`))
eprintln()
success('Migration complete. Verify the changes and commit them to your repository.')
} else {
wordWrap(
'Migration complete. No changes were made to your repository.',
process.stderr.columns - 5 - 4,
).map((line) => eprintln(`${pc.green('\u2502')} ${line}`))
eprintln()
success('Migration complete. No changes were made to your repository.')
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/@tailwindcss-upgrade/src/utils/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ export function indent(value: string, offset = 0) {
return `${' '.repeat(offset + UI.indent)}${value}`
}

export function success(message: string, print = eprintln) {
wordWrap(message, process.stderr.columns - 3).map((line) => {
return print(`${pc.green('\u2502')} ${line}`)
})
print()
}

export function info(message: string, print = eprintln) {
wordWrap(message, process.stderr.columns - 3).map((line) => {
return print(`${pc.blue('\u2502')} ${line}`)
})
print()
}

export function error(message: string, print = eprintln) {
wordWrap(message, process.stderr.columns - 3).map((line) => {
return print(`${pc.red('\u2502')} ${line}`)
})
print()
}

export function warn(message: string, print = eprintln) {
wordWrap(message, process.stderr.columns - 3).map((line) => {
return print(`${pc.yellow('\u2502')} ${line}`)
})
print()
}

// Rust inspired functions to print to the console:

export function eprintln(value = '') {
Expand Down

0 comments on commit 64ac888

Please sign in to comment.