Skip to content

Commit 73e3e1d

Browse files
authored
feat: add tempdir and tempfile utils (#803)
* test: add ts smoke tests * test: fix ts smoke test * fix(types): add @webpod/ingrid to dts bundle * ci: print TS version * feat: introduce `tempdir` and `tempfile` utils
1 parent 7384185 commit 73e3e1d

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

src/cli.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ const argv = minimist(process.argv.slice(2), {
5959
})
6060

6161
;(async function main() {
62-
const globals = './globals.js'
63-
await import(globals)
62+
await import('./globals.js')
6463
if (argv.verbose) $.verbose = true
6564
if (argv.quiet) $.verbose = false
6665
if (argv.shell) $.shell = argv.shell

src/globals.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ declare global {
4141
var quote: typeof _.quote
4242
var quotePowerShell: typeof _.quotePowerShell
4343
var retry: typeof _.retry
44-
var usePowerShell: typeof _.usePowerShell
45-
var usePwsh: typeof _.usePwsh
46-
var useBash: typeof _.useBash
4744
var sleep: typeof _.sleep
4845
var spinner: typeof _.spinner
4946
var stdin: typeof _.stdin
47+
var tempdir: typeof _.tempdir
48+
var tempfile: typeof _.tempfile
49+
var tmpdir: typeof _.tempdir
50+
var tmpfile: typeof _.tempfile
51+
var usePowerShell: typeof _.usePowerShell
52+
var usePwsh: typeof _.usePwsh
53+
var useBash: typeof _.useBash
5054
var which: typeof _.which
5155
var within: typeof _.within
5256
var YAML: typeof _.YAML

src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ export {
2727
glob as globby,
2828
} from './vendor.js'
2929

30-
export { type Duration, quote, quotePowerShell } from './util.js'
30+
export {
31+
type Duration,
32+
quote,
33+
quotePowerShell,
34+
tempdir,
35+
tempdir as tmpdir,
36+
tempfile,
37+
tempfile as tmpfile,
38+
} from './util.js'
3139

3240
/**
3341
* @deprecated Use $`cmd`.nothrow() instead.

src/util.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,27 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { chalk, parseLine } from './vendor.js'
15+
import os from 'node:os'
16+
import path from 'node:path'
17+
import { chalk, parseLine, fs } from './vendor.js'
18+
19+
export function tempdir(prefix = `zx-${randomId()}`) {
20+
const dirpath = path.join(os.tmpdir(), prefix)
21+
fs.mkdirSync(dirpath, { recursive: true })
22+
23+
return dirpath
24+
}
25+
26+
export function tempfile(name?: string, data?: string | Buffer) {
27+
const filepath = name
28+
? path.join(tempdir(), name)
29+
: path.join(os.tmpdir(), `zx-${randomId()}`)
30+
31+
if (data === undefined) fs.closeSync(fs.openSync(filepath, 'w'))
32+
else fs.writeFileSync(filepath, data)
33+
34+
return filepath
35+
}
1636

1737
export function noop() {}
1838

test/index.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ import {
5151
expBackoff,
5252
spinner,
5353
path,
54+
tempdir,
55+
tempfile,
56+
tmpdir,
57+
tmpfile,
5458
} from '../build/index.js'
5559

5660
describe('index', () => {
@@ -100,5 +104,9 @@ describe('index', () => {
100104
// utils
101105
assert(quote)
102106
assert(quotePowerShell)
107+
assert(tempdir)
108+
assert(tmpdir)
109+
assert(tmpfile)
110+
assert(tempfile)
103111
})
104112
})

test/util.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import assert from 'node:assert'
16+
import fs from 'node:fs'
1617
import { test, describe } from 'node:test'
1718
import {
1819
exitCodeInfo,
@@ -26,6 +27,8 @@ import {
2627
randomId,
2728
normalizeMultilinePieces,
2829
getCallerLocationFromString,
30+
tempdir,
31+
tempfile,
2932
} from '../build/util.js'
3033

3134
describe('util', () => {
@@ -148,3 +151,17 @@ test(`getCallerLocationFromString-JSC`, () => {
148151
`
149152
assert.match(getCallerLocationFromString(stack), /^.*:11:5.*$/)
150153
})
154+
155+
test('tempdir() creates temporary folders', () => {
156+
assert.match(tempdir(), /\/zx-/)
157+
assert.match(tempdir('foo'), /\/foo$/)
158+
})
159+
160+
test('tempfile() creates temporary files', () => {
161+
assert.match(tempfile(), /\/zx-.+/)
162+
assert.match(tempfile('foo.txt'), /\/zx-.+\/foo\.txt$/)
163+
164+
const tf = tempfile('bar.txt', 'bar')
165+
assert.match(tf, /\/zx-.+\/bar\.txt$/)
166+
assert.equal(fs.readFileSync(tf, 'utf-8'), 'bar')
167+
})

0 commit comments

Comments
 (0)