Sha256: 136d06d81a3c5b7dfccbe731d574b675b5fa57a1523bb169bde5727fe6e8b107

Contents?: true

Size: 1009 Bytes

Versions: 7

Compression:

Stored size: 1009 Bytes

Contents

const { join, sep } = require('path')

const getOptions = require('./common/get-options.js')
const mkdir = require('./mkdir/index.js')
const mkdtemp = require('./mkdtemp.js')
const rm = require('./rm/index.js')

// create a temp directory, ensure its permissions match its parent, then call
// the supplied function passing it the path to the directory. clean up after
// the function finishes, whether it throws or not
const withTempDir = async (root, fn, opts) => {
  const options = getOptions(opts, {
    copy: ['tmpPrefix'],
  })
  // create the directory, and fix its ownership
  await mkdir(root, { recursive: true, owner: 'inherit' })

  const target = await mkdtemp(join(`${root}${sep}`, options.tmpPrefix || ''), { owner: 'inherit' })
  let err
  let result

  try {
    result = await fn(target)
  } catch (_err) {
    err = _err
  }

  try {
    await rm(target, { force: true, recursive: true })
  } catch (err) {}

  if (err) {
    throw err
  }

  return result
}

module.exports = withTempDir

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
disco_app-0.16.1 test/dummy/node_modules/@npmcli/fs/lib/with-temp-dir.js
disco_app-0.15.2 test/dummy/node_modules/@npmcli/fs/lib/with-temp-dir.js
disco_app-0.18.4 test/dummy/node_modules/@npmcli/fs/lib/with-temp-dir.js
disco_app-0.18.1 test/dummy/node_modules/@npmcli/fs/lib/with-temp-dir.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/@npmcli/fs/lib/with-temp-dir.js
disco_app-0.14.0 test/dummy/node_modules/@npmcli/fs/lib/with-temp-dir.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/@npmcli/fs/lib/with-temp-dir.js