Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ESLint] Adds
--output-file
flag (#36420)
Add support for `--output-file` on ESLint cli, based on this discussion #26179, and this closed PR #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>
- Loading branch information
1 parent
4199da0
commit 59b144c
Showing
5 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters