rsnext/packages/next/lib/verifyAndLint.ts
Houssein Djirdeh 436e74bb04
ESLint: More updates and bug fixes (#25952)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2021-06-10 11:02:50 +02:00

51 lines
1.4 KiB
TypeScript

import chalk from 'chalk'
import { Worker } from 'jest-worker'
import { existsSync } from 'fs'
import { join } from 'path'
import { ESLINT_DEFAULT_DIRS } from './constants'
export async function verifyAndLint(
dir: string,
configLintDirs: string[] | undefined,
numWorkers: number | undefined,
enableWorkerThreads: boolean | undefined
): Promise<void> {
try {
const lintWorkers = new Worker(require.resolve('./eslint/runLintCheck'), {
numWorkers,
enableWorkerThreads,
}) as Worker & {
runLintCheck: typeof import('./eslint/runLintCheck').runLintCheck
}
lintWorkers.getStdout().pipe(process.stdout)
lintWorkers.getStderr().pipe(process.stderr)
const lintDirs = (configLintDirs ?? ESLINT_DEFAULT_DIRS).reduce(
(res: string[], d: string) => {
const currDir = join(dir, d)
if (!existsSync(currDir)) return res
res.push(currDir)
return res
},
[]
)
const lintResults = await lintWorkers.runLintCheck(dir, lintDirs, true)
if (lintResults) {
console.log(lintResults)
}
lintWorkers.end()
} catch (err) {
if (err.type === 'CompileError') {
console.error(chalk.red('\nFailed to compile.'))
console.error(err.message)
process.exit(1)
} else if (err.type === 'FatalError') {
console.error(err.message)
process.exit(1)
}
throw err
}
}