polyfills, and extensions, of the core fs
module.
fs.cp
polyfill for node < 16.7.0fs.withTempDir
added
root
: the directory in which to create the temporary directoryfn
: a function that will be called with the path to the temporary directoryoptions
tmpPrefix
: a prefix to be used in the generated directory name
The withTempDir
function creates a temporary directory, runs the provided
function (fn
), then removes the temporary directory and resolves or rejects
based on the result of fn
.
const fs = require('@npmcli/fs')
const os = require('os')
// this function will be called with the full path to the temporary directory
// it is called with `await` behind the scenes, so can be async if desired.
const myFunction = async (tempPath) => {
return 'done!'
}
const main = async () => {
const result = await fs.withTempDir(os.tmpdir(), myFunction)
// result === 'done!'
}
main()