Skip to content

Commit aef8209

Browse files
authored
Enhance cd to accept ProcessOutput (google#642)
* Enhance cd to accept ProcessOutput Satisfies google#641 * Wrap cd test in within to avoid interference
1 parent d9f8a75 commit aef8209

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ cd('/tmp')
169169
await $`pwd` // => /tmp
170170
```
171171

172+
Like `echo`, in addition to `string` arguments, `cd` accepts and trims
173+
trailing newlines from `ProcessOutput` enabling common idioms like:
174+
175+
```js
176+
cd(await $`mktemp -d`)
177+
```
178+
172179
### `fetch()`
173180

174181
A wrapper around the [node-fetch](https://www.npmjs.com/package/node-fetch)

src/core.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,11 @@ function syncCwd() {
441441
if ($[processCwd] != process.cwd()) process.chdir($[processCwd])
442442
}
443443

444-
export function cd(dir: string) {
444+
export function cd(dir: string | ProcessOutput) {
445+
if (dir instanceof ProcessOutput) {
446+
dir = dir.toString().replace(/\n+$/, '')
447+
}
448+
445449
$.log({ kind: 'cd', dir })
446450
process.chdir(dir)
447451
$[processCwd] = process.cwd()

test/core.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ test('cd() fails on entering not existing dir', async () => {
233233
assert.throws(() => cd('/tmp/abra-kadabra'))
234234
})
235235

236+
test('cd() accepts ProcessOutput in addition to string', async () => {
237+
within(async () => {
238+
const tmpDir = await $`mktemp -d`
239+
cd(tmpDir)
240+
assert.match(process.cwd(), tmpDir.toString().trimEnd())
241+
})
242+
})
243+
236244
test('kill() method works', async () => {
237245
let p = $`sleep 9999`.nothrow()
238246
setTimeout(() => {

0 commit comments

Comments
 (0)