1 | import fs from 'node:fs/promises'
|
2 |
|
3 | import debug from 'debug'
|
4 |
|
5 | const debugLog = debug('lint-staged:file')
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export const readFile = async (filename, ignoreENOENT = true) => {
|
14 | debugLog('Reading file `%s`', filename)
|
15 | try {
|
16 | return await fs.readFile(filename)
|
17 | } catch (error) {
|
18 | if (ignoreENOENT && error.code === 'ENOENT') {
|
19 | debugLog("File `%s` doesn't exist, ignoring...", filename)
|
20 | return null
|
21 | } else {
|
22 | throw error
|
23 | }
|
24 | }
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export const unlink = async (filename, ignoreENOENT = true) => {
|
33 | debugLog('Removing file `%s`', filename)
|
34 | try {
|
35 | await fs.unlink(filename)
|
36 | } catch (error) {
|
37 | if (ignoreENOENT && error.code === 'ENOENT') {
|
38 | debugLog("File `%s` doesn't exist, ignoring...", filename)
|
39 | } else {
|
40 | throw error
|
41 | }
|
42 | }
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | export const writeFile = async (filename, buffer) => {
|
51 | debugLog('Writing file `%s`', filename)
|
52 | await fs.writeFile(filename, buffer)
|
53 | }
|