99f2aa11e78baca89f1ba01503a5cd5b13dc66dbee789a394cdfa3140a849294d3e6dd875bc518630453dd0068250909288c798a5a43a1ab0b713e9eed749e 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # @npmcli/fs
  2. polyfills, and extensions, of the core `fs` module.
  3. ## Features
  4. - all exposed functions return promises
  5. - `fs.rm` polyfill for node versions < 14.14.0
  6. - `fs.mkdir` polyfill adding support for the `recursive` and `force` options in node versions < 10.12.0
  7. - `fs.copyFile` extended to accept an `owner` option
  8. - `fs.mkdir` extended to accept an `owner` option
  9. - `fs.mkdtemp` extended to accept an `owner` option
  10. - `fs.writeFile` extended to accept an `owner` option
  11. - `fs.withTempDir` added
  12. - `fs.cp` polyfill for node < 16.7.0
  13. ## The `owner` option
  14. The `copyFile`, `mkdir`, `mkdtemp`, `writeFile`, and `withTempDir` functions
  15. all accept a new `owner` property in their options. It can be used in two ways:
  16. - `{ owner: { uid: 100, gid: 100 } }` - set the `uid` and `gid` explicitly
  17. - `{ owner: 100 }` - use one value, will set both `uid` and `gid` the same
  18. The special string `'inherit'` may be passed instead of a number, which will
  19. cause this module to automatically determine the correct `uid` and/or `gid`
  20. from the nearest existing parent directory of the target.
  21. ## `fs.withTempDir(root, fn, options) -> Promise`
  22. ### Parameters
  23. - `root`: the directory in which to create the temporary directory
  24. - `fn`: a function that will be called with the path to the temporary directory
  25. - `options`
  26. - `tmpPrefix`: a prefix to be used in the generated directory name
  27. ### Usage
  28. The `withTempDir` function creates a temporary directory, runs the provided
  29. function (`fn`), then removes the temporary directory and resolves or rejects
  30. based on the result of `fn`.
  31. ```js
  32. const fs = require('@npmcli/fs')
  33. const os = require('os')
  34. // this function will be called with the full path to the temporary directory
  35. // it is called with `await` behind the scenes, so can be async if desired.
  36. const myFunction = async (tempPath) => {
  37. return 'done!'
  38. }
  39. const main = async () => {
  40. const result = await fs.withTempDir(os.tmpdir(), myFunction)
  41. // result === 'done!'
  42. }
  43. main()
  44. ```