Skip to content

Commit

Permalink
fix: auto exit if no errors after change
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenQingchuan committed Sep 13, 2022
1 parent 824bdf8 commit 65c32ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/index.ts
@@ -1,5 +1,4 @@
#!/usr/bin/env node

import path from 'node:path'
import chokidar from 'chokidar'
import cac from 'cac'
Expand Down Expand Up @@ -94,12 +93,19 @@ function showSelectFilePrompt(ctx: Context) {
})
return prompt
}
function guardErrsMapNotEmpty(rawErrsMap: RawErrsMap) {
const errsCount = getRawErrsSumCount(rawErrsMap)
if (errsCount === 0) {
console.log(`\n🎉 ${chalk.bold.greenBright('Found 0 Errors.')}\n`)
process.exit()
}
}

try {
await ensureTscVersion()
showAppHeader()

const cli = cac('tsc-err-dirs')
showAppHeader(cli)

const parsedEnvArgs = cli.parse()
const rootDirArg = parsedEnvArgs.args[0]
const rootAbsPath = getTargetDir(rootDirArg)
Expand All @@ -108,11 +114,8 @@ try {
}

// Generate a map to store errors info
const rawErrsMap = await getRawErrsMapFromTsCompile(rootAbsPath)
if (getRawErrsSumCount(rawErrsMap) === 0) {
console.log(`\n🎉 ${chalk.bold.greenBright('Found 0 Errors.')}\n`)
process.exit()
}
const _initRawErrsMap = await getRawErrsMapFromTsCompile(rootAbsPath)
guardErrsMapNotEmpty(_initRawErrsMap)

// Watch the `rootAbsPath` for any changes
const rootWatcher = await new Promise<chokidar.FSWatcher>((resolve) => {
Expand All @@ -132,7 +135,7 @@ try {
const ctx = {
root: rootAbsPath,
targetAbsPath: rootAbsPath,
rawErrsMap,
rawErrsMap: _initRawErrsMap,
}
// Bind watcher to the selector view
const selectFile = async (ctx: Context) => {
Expand Down Expand Up @@ -172,6 +175,7 @@ try {
console.log(error.message)
ctx.rawErrsMap.clear()
ctx.rawErrsMap = await getRawErrsMapFromTsCompile(rootAbsPath, true)
guardErrsMapNotEmpty(ctx.rawErrsMap)
continue
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/show-console-print.ts
@@ -1,7 +1,9 @@
import chalk from 'chalk'
import packageJSON from '../package.json'
import type { CAC } from 'cac'

export function showAppHeader() {
export function showAppHeader(cli: CAC) {
const version = packageJSON.version
console.log(
`\n${chalk.bold.blue(`
_____ _____ ____ _
Expand All @@ -13,6 +15,7 @@ export function showAppHeader() {
)}
`)}`
)
cli.version(version)
}
export function showFirstTscCompilePathInfo({
cmd,
Expand Down

0 comments on commit 65c32ad

Please sign in to comment.