59b144c264
Add support for `--output-file` on ESLint cli, based on this discussion https://github.com/vercel/next.js/discussions/26179, and this closed PR https://github.com/vercel/next.js/pull/35154. With this flag, it is possible to save the output in a file and use it for any purpose. ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [x] Make sure the linting passes by running `yarn lint` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { promises as fs } from 'fs'
|
|
import path from 'path'
|
|
import * as Log from '../../build/output/log'
|
|
import isError from '../../lib/is-error'
|
|
|
|
/**
|
|
* Check if a given file path is a directory or not.
|
|
* @param {string} filePath The path to a file to check.
|
|
* @returns {Promise<boolean>} `true` if the path is a directory.
|
|
*/
|
|
async function isDirectory(filePath: string): Promise<boolean> {
|
|
try {
|
|
return (await fs.stat(filePath)).isDirectory()
|
|
} catch (error) {
|
|
if (
|
|
isError(error) &&
|
|
(error.code === 'ENOENT' || error.code === 'ENOTDIR')
|
|
) {
|
|
return false
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
/**
|
|
* Create a file with eslint output data
|
|
* @param {string} outputFile The name file that needs to be created
|
|
* @param {string} outputData The data that needs to be inserted into the file
|
|
*/
|
|
export async function writeOutputFile(outputFile: string, outputData: string) {
|
|
const filePath = path.resolve(process.cwd(), outputFile)
|
|
|
|
if (await isDirectory(filePath)) {
|
|
Log.error(
|
|
`Cannot write to output file path, it is a directory: ${filePath}`
|
|
)
|
|
} else {
|
|
try {
|
|
await fs.mkdir(path.dirname(filePath), { recursive: true })
|
|
await fs.writeFile(filePath, outputData)
|
|
Log.info(`The output file has been created: ${filePath}`)
|
|
} catch (err) {
|
|
Log.error(`There was a problem writing the output file: ${filePath}`)
|
|
console.error(err)
|
|
}
|
|
}
|
|
}
|