finki-main
main
|
Last change
on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
1009 bytes
|
| Line | |
|---|
| 1 | const { join, sep } = require('path')
|
|---|
| 2 |
|
|---|
| 3 | const getOptions = require('./common/get-options.js')
|
|---|
| 4 | const mkdir = require('./mkdir/index.js')
|
|---|
| 5 | const mkdtemp = require('./mkdtemp.js')
|
|---|
| 6 | const rm = require('./rm/index.js')
|
|---|
| 7 |
|
|---|
| 8 | // create a temp directory, ensure its permissions match its parent, then call
|
|---|
| 9 | // the supplied function passing it the path to the directory. clean up after
|
|---|
| 10 | // the function finishes, whether it throws or not
|
|---|
| 11 | const withTempDir = async (root, fn, opts) => {
|
|---|
| 12 | const options = getOptions(opts, {
|
|---|
| 13 | copy: ['tmpPrefix'],
|
|---|
| 14 | })
|
|---|
| 15 | // create the directory, and fix its ownership
|
|---|
| 16 | await mkdir(root, { recursive: true, owner: 'inherit' })
|
|---|
| 17 |
|
|---|
| 18 | const target = await mkdtemp(join(`${root}${sep}`, options.tmpPrefix || ''), { owner: 'inherit' })
|
|---|
| 19 | let err
|
|---|
| 20 | let result
|
|---|
| 21 |
|
|---|
| 22 | try {
|
|---|
| 23 | result = await fn(target)
|
|---|
| 24 | } catch (_err) {
|
|---|
| 25 | err = _err
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | try {
|
|---|
| 29 | await rm(target, { force: true, recursive: true })
|
|---|
| 30 | } catch (err) {}
|
|---|
| 31 |
|
|---|
| 32 | if (err) {
|
|---|
| 33 | throw err
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | return result
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | module.exports = withTempDir
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.