-
-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make sure deleted files aren't restored due to git bugs (#778)
* fix: make sure deleted files aren't restored due to git bugs * refactor(file): use fs.promises * perf: run file removals in parallel * fix: filter out empty string for when there is no output * fix: correct file handling * test: add test for complex additions and deletions * fix: make cleaning untracked files resurrected due to git bug testable
- Loading branch information
Showing
9 changed files
with
161 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,75 @@ | ||
'use strict' | ||
|
||
const debug = require('debug')('lint-staged:file') | ||
const fs = require('fs') | ||
|
||
const fsPromises = fs.promises | ||
const fs = require('fs').promises | ||
|
||
/** | ||
* Check if a file exists. Returns the filepath if exists. | ||
* @param {string} filepath | ||
* Check if a file exists. Returns the filename if exists. | ||
* @param {String} filename | ||
* @returns {String|Boolean} | ||
*/ | ||
const exists = async filepath => { | ||
const exists = async filename => { | ||
try { | ||
await fsPromises.access(filepath) | ||
return filepath | ||
await fs.access(filename) | ||
return filename | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
/** | ||
* Read contents of a file to buffer | ||
* @param {String} filename | ||
* @returns {Promise<Buffer|Null>} | ||
* @param {Boolean} [ignoreENOENT=true] — Whether to throw if the file doesn't exist | ||
* @returns {Promise<Buffer>} | ||
*/ | ||
const readBufferFromFile = (filename, rejectENOENT = false) => | ||
new Promise(resolve => { | ||
debug('Reading buffer from file `%s`', filename) | ||
fs.readFile(filename, (error, buffer) => { | ||
if (!rejectENOENT && error && error.code === 'ENOENT') { | ||
debug("File `%s` doesn't exist, ignoring...", filename) | ||
return resolve(null) // no-op file doesn't exist | ||
} | ||
debug('Done reading buffer from file `%s`!', filename) | ||
resolve(buffer) | ||
}) | ||
}) | ||
const readFile = async (filename, ignoreENOENT = true) => { | ||
debug('Reading file `%s`', filename) | ||
try { | ||
return await fs.readFile(filename) | ||
} catch (error) { | ||
if (ignoreENOENT && error.code === 'ENOENT') { | ||
debug("File `%s` doesn't exist, ignoring...", filename) | ||
return null // no-op file doesn't exist | ||
} else { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Unlink a file if it exists | ||
* @param {*} filepath | ||
* @param {String} filename | ||
* @param {Boolean} [ignoreENOENT=true] — Whether to throw if the file doesn't exist | ||
*/ | ||
const unlink = async filepath => { | ||
if (filepath) { | ||
await fsPromises.access(filepath) | ||
await fsPromises.unlink(filepath) | ||
const unlink = async (filename, ignoreENOENT = true) => { | ||
if (filename) { | ||
debug('Unlinking file `%s`', filename) | ||
try { | ||
await fs.unlink(filename) | ||
} catch (error) { | ||
if (ignoreENOENT && error.code === 'ENOENT') { | ||
debug("File `%s` doesn't exist, ignoring...", filename) | ||
} else { | ||
throw error | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Write buffer to file | ||
* @param {String} filename | ||
* @param {Buffer} buffer | ||
* @returns {Promise<Void>} | ||
*/ | ||
const writeBufferToFile = (filename, buffer) => | ||
new Promise(resolve => { | ||
debug('Writing buffer to file `%s`', filename) | ||
fs.writeFile(filename, buffer, () => { | ||
debug('Done writing buffer to file `%s`!', filename) | ||
resolve() | ||
}) | ||
}) | ||
const writeFile = async (filename, buffer) => { | ||
debug('Writing file `%s`', filename) | ||
await fs.writeFile(filename, buffer) | ||
} | ||
|
||
module.exports = { | ||
exists, | ||
readBufferFromFile, | ||
readFile, | ||
unlink, | ||
writeBufferToFile | ||
writeFile | ||
} |
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
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,13 @@ | ||
import { unlink, readFile } from '../lib/file' | ||
|
||
describe('unlink', () => { | ||
it('should throw when second argument is false and file is not found', async () => { | ||
await expect(unlink('example', false)).rejects.toThrow('ENOENT') | ||
}) | ||
}) | ||
|
||
describe('readFile', () => { | ||
it('should throw when second argument is false and file is not found', async () => { | ||
await expect(readFile('example', false)).rejects.toThrow('ENOENT') | ||
}) | ||
}) |
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
Oops, something went wrong.