File size: 1,396 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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.
 * Returns `true` if the path is a directory.
 */
function isDirectory(
  /**  The path to a file to check. */
  filePath: string
): Promise<boolean> {
  return fs
    .stat(filePath)
    .then((stat) => stat.isDirectory())
    .catch((error) => {
      if (
        isError(error) &&
        (error.code === 'ENOENT' || error.code === 'ENOTDIR')
      ) {
        return false
      }
      throw error
    })
}
/**
 * Create a file with eslint output data
 */
export async function writeOutputFile(
  /** The name file that needs to be created */
  outputFile: string,
  /** The data that needs to be inserted into the file */
  outputData: string
): Promise<void> {
  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)
    }
  }
}