Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a42de2
commit acbf1a2
Showing
4 changed files
with
63 additions
and
5 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,45 @@ | ||
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 { | ||
Log.error(`There was a problem writing the output file: ${filePath}`) | ||
} | ||
} | ||
} |