-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor improvements on the usage of fs utilities - unify all the fs fu…
- Loading branch information
1 parent
f2d513e
commit 7575946
Showing
6 changed files
with
154 additions
and
33 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,41 @@ | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const { promisify } = require('util'); | ||
const mkdirp = require('mkdirp'); | ||
|
||
// Promisify common fs functions. | ||
const stat = promisify(fs.stat); | ||
const readFile = promisify(fs.readFile); | ||
const writeFile = promisify(fs.writeFile); | ||
const readdir = promisify(fs.readdir); | ||
|
||
/** | ||
* Creates a temporary directory and returns it path. | ||
* | ||
* @returns {Promise<string>} | ||
*/ | ||
function createTemp() { | ||
return promisify(fs.mkdtemp)(`${os.tmpdir()}${path.sep}`); | ||
} | ||
|
||
/** | ||
* Ensures the given path exists. | ||
* - If the path already exist, it's fine - it does nothing. | ||
* - If the path doesn't exist, it will create it. | ||
* | ||
* @param {string} path | ||
* @returns {Promise} | ||
*/ | ||
function ensureDirectoryExists(dir) { | ||
return stat(dir).catch(() => mkdirp(dir)); | ||
} | ||
|
||
module.exports = { | ||
stat, | ||
readdir, | ||
readFile, | ||
writeFile, | ||
createTemp, | ||
ensureDirectoryExists, | ||
}; |
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,97 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const { expect } = require('chai'); | ||
const { | ||
stat, | ||
readdir, | ||
readFile, | ||
writeFile, | ||
createTemp, | ||
ensureDirectoryExists, | ||
} = require('../../../lib/util/fs'); | ||
|
||
describe('FS functions', () => { | ||
describe('stat', () => { | ||
it('should return stats for a given path.', async () => { | ||
const directoryThatExists = await createTemp(); | ||
|
||
const result = await stat(directoryThatExists); | ||
|
||
expect(result).to.be.an.instanceOf(fs.Stats); | ||
}); | ||
|
||
it('should throw an error if the path does not exist.', async () => { | ||
const directoryThatExists = await createTemp(); | ||
const unexistingPath = path.join(directoryThatExists, 'abc/xyz/123'); | ||
|
||
expect(stat(unexistingPath)).to.eventually.be.rejected; | ||
}); | ||
}); | ||
|
||
describe('readFile', async () => { | ||
it('should be able to read from the given file path.', async () => { | ||
const tmpPath = await createTemp(); | ||
const filePath = path.join(tmpPath, `${Date.now()}1.txt`); | ||
|
||
fs.writeFileSync(filePath, 'Hello World!'); | ||
|
||
const contents = await readFile(filePath); | ||
|
||
expect(contents.toString()).to.equal('Hello World!'); | ||
}); | ||
}); | ||
|
||
describe('writeFile', async () => { | ||
it('should be able to write to the given file path.', async () => { | ||
const tmpPath = await createTemp(); | ||
const filePath = path.join(tmpPath, `${Date.now()}2.txt`); | ||
|
||
await writeFile(filePath, 'Foo Bar'); | ||
|
||
expect(fs.readFileSync(filePath).toString()).to.equal('Foo Bar'); | ||
}); | ||
}); | ||
|
||
describe('readdir', async () => { | ||
it('should read a directory and return a list of files under it.', async () => { | ||
const directory = await createTemp(); | ||
|
||
// Create files under the directory. | ||
fs.writeFileSync(path.join(directory, 'testfile1.txt'), 'testfile1'); | ||
fs.writeFileSync(path.join(directory, 'testfile2.txt'), 'testfile2'); | ||
fs.writeFileSync(path.join(directory, 'testfile3.txt'), 'testfile3'); | ||
fs.writeFileSync(path.join(directory, 'testfile4.txt'), 'testfile4'); | ||
|
||
const result = await readdir(directory); | ||
|
||
expect(result).to.deep.equal([ | ||
'testfile1.txt', | ||
'testfile2.txt', | ||
'testfile3.txt', | ||
'testfile4.txt', | ||
]); | ||
}); | ||
}); | ||
|
||
describe('ensureDirectoryExists', () => { | ||
it('should resolve successfully if the directory already exists.', async () => { | ||
const directoryThatExists = await createTemp(); | ||
|
||
const result = await ensureDirectoryExists(directoryThatExists); | ||
|
||
expect(result).to.be.an.instanceOf(fs.Stats); | ||
}); | ||
|
||
it('should create a directory (recursively) if it does not exist.', async () => { | ||
const directoryThatExists = await createTemp(); | ||
const newPath = path.join(directoryThatExists, 'abc/xyz/123'); | ||
|
||
expect(fs.existsSync(newPath)).to.equal(false); | ||
|
||
await ensureDirectoryExists(newPath); | ||
|
||
expect(fs.existsSync(newPath)).to.equal(true); | ||
}); | ||
}); | ||
}); |