Skip to content

Commit

Permalink
fix: improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 31, 2022
1 parent 07fa04f commit 7a7a9ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CasePolice
# 🚨 CasePolice

[![NPM version](https://img.shields.io/npm/v/case-police?color=a1b858&label=)](https://www.npmjs.com/package/case-police)

Expand All @@ -24,11 +24,10 @@ It will scan all your source files and fix the cases of [known names](./dict.jso

Only the word including both uppercase and lowercase will be fixed. (e.g. `Github` -> `GitHub`; `github` and `GITHUB` will be left untouched).

### Using in CI
### Use in CI

Simply add `case-police` (without `--fix`) to your workflow and it will exit with a non-zero code for your CI to catch it.


## CLI Options

| Options | Description |
Expand Down
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import isText from 'is-text-path'
import pLimit from 'p-limit'
import minimist from 'minimist'
import dictionary from '../dict.json'
import { version } from '../package.json'
import { buildRegex, replace } from './utils'

async function run() {
Expand Down Expand Up @@ -51,9 +52,9 @@ async function run() {

const limit = pLimit(5)
console.log()
console.log(c.inverse(c.red('C')) + c.blue(c.underline('ase')) + c.inverse(c.red('P')) + c.blue(c.underline('olice')))
console.log(c.inverse(c.red(' Case ')) + c.inverse(c.blue(' Police ')) + c.dim(` v${version}`))
console.log()
console.log(c.dim('checking ') + c.yellow(files.length) + c.dim(' files\n'))
console.log(c.blue(files.length) + c.dim(' files found for checking\n'))
const wrote: string[] = []
await Promise.all(files.map(file => limit(async() => {
const code = await fs.readFile(file, 'utf-8')
Expand All @@ -69,14 +70,12 @@ async function run() {
console.log(c.green('All good, well done!'))
}
else {
if (argv.fix)
console.log(c.green('\nfiles fixed:'))
else
console.log(c.yellow('\nfiles needs to be fixed:'))
console.log(c.dim(wrote.map(i => ` - ${i}`).join('\n')))

if (!argv.fix) {
console.log(c.dim('\nrun ') + c.magenta(c.bold('npx case-police --fix')) + c.dim(' to fix\n'))
if (argv.fix) {
console.log(c.green('\nfiles fixed'))
}
else {
console.log(c.dim(`\n${wrote.length} files contain case errors`))
console.log(c.dim('run ') + c.magenta(c.bold('npx case-police --fix')) + c.dim(' to fix\n'))
process.exitCode = 1
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export function replace(
if (!value || value === key)
return _
changed = true
console.log(`${c.dim(`${id}:${index}`)} \t${c.yellow(key)} -> ${c.green(value)}`)
const lines = code.slice(0, index).split('\n')
const line = code.slice(0, index).split('\n').length
const col = (lines[lines.length - 1].length || 0) + 1
console.log(`${c.yellow(key)} ${c.dim('→')} ${c.green(value)} \t ${c.dim(`./${id}:${line}:${col}`)}`)
return value
})
if (changed)
Expand Down

0 comments on commit 7a7a9ff

Please sign in to comment.