codekingpro/portable-devtools
114k
1'use strict'
2
3const crypto = require('crypto')
4const { withTempDir } = require('@npmcli/fs')
5const fs = require('fs/promises')
6const path = require('path')
7
8module.exports.mkdir = mktmpdir
9
10module.exports.tmpName = function tmpName (cache, tmpPrefix) {
11 const id = crypto.randomUUID()
12 return path.join(cache, 'tmp', tmpPrefix ? `${tmpPrefix}-${id}` : id)
13}
14
15async function mktmpdir (cache, opts = {}) {
16 const { tmpPrefix } = opts
17 const tmpDir = path.join(cache, 'tmp')
18 await fs.mkdir(tmpDir, { recursive: true, owner: 'inherit' })
19 // do not use path.join(), it drops the trailing / if tmpPrefix is unset
20 const target = `${tmpDir}${path.sep}${tmpPrefix || ''}`
21 return fs.mkdtemp(target, { owner: 'inherit' })
22}
23
24module.exports.withTmp = withTmp
25
26function withTmp (cache, opts, cb) {
27 if (!cb) {
28 cb = opts
29 opts = {}
30 }
31 return withTempDir(path.join(cache, 'tmp'), cb, opts)
32}
33 