Skip to content

Commit d969840

Browse files
authored
feat: accept mode option for tmpdir and tmpfile (#1063)
1 parent 022606a commit d969840

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

docs/api.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,9 @@ Temp file factory.
293293

294294
```js
295295
f1 = tmpfile() // /os/based/tmp/zx-1ra1iofojgg
296-
f2 = tmpfile('f.txt') // /os/based/tmp/zx-1ra1iofojgg/foo.txt
297-
f3 = tmpfile('f.txt', 'string or buffer')
296+
f2 = tmpfile('f2.txt') // /os/based/tmp/zx-1ra1iofojgg/foo.txt
297+
f3 = tmpfile('f3.txt', 'string or buffer')
298+
f4 = tmpfile('f4.sh', 'echo "foo"', 0o744) // executable
298299
```
299300

300301
## minimist

src/util.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,33 @@
1414

1515
import os from 'node:os'
1616
import path from 'node:path'
17-
import fs from 'node:fs'
17+
import fs, { type Mode } from 'node:fs'
1818
import { chalk, type RequestInfo, type RequestInit } from './vendor-core.js'
1919
import { inspect } from 'node:util'
2020

2121
export { isStringLiteral } from './vendor-core.js'
2222

23-
export function tempdir(prefix: string = `zx-${randomId()}`): string {
23+
export function tempdir(
24+
prefix: string = `zx-${randomId()}`,
25+
mode?: Mode
26+
): string {
2427
const dirpath = path.join(os.tmpdir(), prefix)
25-
fs.mkdirSync(dirpath, { recursive: true })
28+
fs.mkdirSync(dirpath, { recursive: true, mode })
2629

2730
return dirpath
2831
}
2932

30-
export function tempfile(name?: string, data?: string | Buffer): string {
33+
export function tempfile(
34+
name?: string,
35+
data?: string | Buffer,
36+
mode?: Mode
37+
): string {
3138
const filepath = name
3239
? path.join(tempdir(), name)
3340
: path.join(os.tmpdir(), `zx-${randomId()}`)
3441

35-
if (data === undefined) fs.closeSync(fs.openSync(filepath, 'w'))
36-
else fs.writeFileSync(filepath, data)
42+
if (data === undefined) fs.closeSync(fs.openSync(filepath, 'w', mode))
43+
else fs.writeFileSync(filepath, data, { mode })
3744

3845
return filepath
3946
}

test/cli.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ describe('cli', () => {
272272
const zxLocation = isWindows ? toPOSIXPath(zxPath) : zxPath
273273
const scriptCode = `#!/usr/bin/env ${zxLocation}\nconsole.log('The script from path runs.')`
274274
const scriptName = 'script-from-path'
275-
const scriptFile = tmpfile(scriptName, scriptCode)
275+
const scriptFile = tmpfile(scriptName, scriptCode, 0o744)
276276
const scriptDir = path.dirname(scriptFile)
277-
fs.chmodSync(scriptFile, 0o744)
278277

279278
const envPathSeparator = isWindows ? ';' : ':'
280279
process.env.PATH += envPathSeparator + scriptDir

0 commit comments

Comments
 (0)