2021-04-30 13:09:07 +02:00
|
|
|
import chalk from 'chalk'
|
|
|
|
import { Worker } from 'jest-worker'
|
2021-06-09 00:46:00 +02:00
|
|
|
import { existsSync } from 'fs'
|
|
|
|
import { join } from 'path'
|
2021-04-30 13:09:07 +02:00
|
|
|
|
|
|
|
export async function verifyAndLint(
|
|
|
|
dir: string,
|
2021-06-09 00:46:00 +02:00
|
|
|
configLintDirs: string[] | undefined,
|
2021-04-30 13:09:07 +02:00
|
|
|
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)
|
|
|
|
|
2021-06-09 00:46:00 +02:00
|
|
|
const lintDirs = (configLintDirs ?? ['pages', 'components', 'lib']).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)
|
2021-04-30 13:09:07 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|