Skip to content

Commit

Permalink
feat: introduce @npmcli/fs for tmp dir methods (#59)
Browse files Browse the repository at this point in the history
* feat: introduce @npmcli/fs for tmp dir methods
  • Loading branch information
nlf authored Aug 26, 2021
1 parent 88ddc1b commit 274d456
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
18 changes: 9 additions & 9 deletions lib/util/tmp.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict'

const util = require('util')
const fs = require('@npmcli/fs')

const fixOwner = require('./fix-owner')
const path = require('path')
const rimraf = util.promisify(require('rimraf'))
const uniqueFilename = require('unique-filename')
const { disposer } = require('./disposer')

module.exports.mkdir = mktmpdir

function mktmpdir (cache, opts = {}) {
const { tmpPrefix } = opts
const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), tmpPrefix)
return fixOwner.mkdirfix(cache, tmpTarget).then(() => {
return tmpTarget
})
const tmpDir = path.join(cache, 'tmp')
return fs.mkdir(tmpDir, { recursive: true, owner: 'inherit' })
.then(() => {
// do not use path.join(), it drops the trailing / if tmpPrefix is unset
const target = `${tmpDir}${path.sep}${tmpPrefix || ''}`
return fs.mkdtemp(target, { owner: 'inherit' })
})
}

module.exports.withTmp = withTmp
Expand All @@ -25,7 +25,7 @@ function withTmp (cache, opts, cb) {
cb = opts
opts = {}
}
return disposer(mktmpdir(cache, opts), rimraf, cb)
return fs.withTempDir(path.join(cache, 'tmp'), cb, opts)
}

module.exports.fix = fixtmpdir
Expand Down
34 changes: 30 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
],
"license": "ISC",
"dependencies": {
"@npmcli/fs": "^1.0.0",
"@npmcli/move-file": "^1.0.1",
"chownr": "^2.0.0",
"fs-minipass": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/util.tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ test('provides a utility that does resource disposal on tmp', (t) => {
})

test('withTmp should accept both opts and cb params', t => {
return tmp.withTmp(CACHE, { tmpPrefix: {} }, dir => {
return tmp.withTmp(CACHE, { tmpPrefix: 'foo' }, dir => {
t.ok(dir, 'dir should contain a valid response')
})
})
Expand Down

0 comments on commit 274d456

Please sign in to comment.