• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const { withTempDir } = require('@npmcli/fs')
4const fs = require('fs/promises')
5const path = require('path')
6
7module.exports.mkdir = mktmpdir
8
9async function mktmpdir (cache, opts = {}) {
10  const { tmpPrefix } = opts
11  const tmpDir = path.join(cache, 'tmp')
12  await fs.mkdir(tmpDir, { recursive: true, owner: 'inherit' })
13  // do not use path.join(), it drops the trailing / if tmpPrefix is unset
14  const target = `${tmpDir}${path.sep}${tmpPrefix || ''}`
15  return fs.mkdtemp(target, { owner: 'inherit' })
16}
17
18module.exports.withTmp = withTmp
19
20function withTmp (cache, opts, cb) {
21  if (!cb) {
22    cb = opts
23    opts = {}
24  }
25  return withTempDir(path.join(cache, 'tmp'), cb, opts)
26}
27