2021-12-21 16:13:45 +01:00
|
|
|
import chalk from 'next/dist/compiled/chalk'
|
2022-01-17 16:17:22 +01:00
|
|
|
import { Worker } from 'next/dist/compiled/jest-worker'
|
2021-06-09 00:46:00 +02:00
|
|
|
import { existsSync } from 'fs'
|
|
|
|
import { join } from 'path'
|
2021-06-10 11:02:50 +02:00
|
|
|
import { ESLINT_DEFAULT_DIRS } from './constants'
|
2021-06-15 03:31:40 +02:00
|
|
|
import { Telemetry } from '../telemetry/storage'
|
|
|
|
import { eventLintCheckCompleted } from '../telemetry/events'
|
|
|
|
import { CompileError } from './compile-error'
|
2021-09-16 18:06:57 +02:00
|
|
|
import isError from './is-error'
|
2021-04-30 13:09:07 +02:00
|
|
|
|
|
|
|
export async function verifyAndLint(
|
|
|
|
dir: string,
|
2021-08-23 19:56:21 +02:00
|
|
|
cacheLocation: string,
|
2021-06-09 00:46:00 +02:00
|
|
|
configLintDirs: string[] | undefined,
|
2021-04-30 13:09:07 +02:00
|
|
|
numWorkers: number | undefined,
|
2021-06-15 03:31:40 +02:00
|
|
|
enableWorkerThreads: boolean | undefined,
|
|
|
|
telemetry: Telemetry
|
2021-04-30 13:09:07 +02:00
|
|
|
): 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)
|
|
|
|
|
2021-06-10 11:02:50 +02:00
|
|
|
const lintDirs = (configLintDirs ?? ESLINT_DEFAULT_DIRS).reduce(
|
2021-06-09 00:46:00 +02:00
|
|
|
(res: string[], d: string) => {
|
|
|
|
const currDir = join(dir, d)
|
|
|
|
if (!existsSync(currDir)) return res
|
|
|
|
res.push(currDir)
|
|
|
|
return res
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2021-08-23 19:56:21 +02:00
|
|
|
const lintResults = await lintWorkers.runLintCheck(dir, lintDirs, true, {
|
|
|
|
cacheLocation,
|
|
|
|
})
|
2021-06-15 03:31:40 +02:00
|
|
|
const lintOutput =
|
|
|
|
typeof lintResults === 'string' ? lintResults : lintResults?.output
|
|
|
|
|
|
|
|
if (typeof lintResults !== 'string' && lintResults?.eventInfo) {
|
|
|
|
telemetry.record(
|
|
|
|
eventLintCheckCompleted({
|
|
|
|
...lintResults.eventInfo,
|
|
|
|
buildLint: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof lintResults !== 'string' && lintResults?.isError && lintOutput) {
|
|
|
|
await telemetry.flush()
|
|
|
|
throw new CompileError(lintOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lintOutput) {
|
|
|
|
console.log(lintOutput)
|
2021-04-30 13:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lintWorkers.end()
|
|
|
|
} catch (err) {
|
2021-09-16 18:06:57 +02:00
|
|
|
if (isError(err)) {
|
|
|
|
if (err.type === 'CompileError' || err instanceof 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)
|
|
|
|
}
|
2021-04-30 13:09:07 +02:00
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|