Skip to content

Commit

Permalink
Enhance cd to accept ProcessOutput (#642)
Browse files Browse the repository at this point in the history
* Enhance cd to accept ProcessOutput

Satisfies #641

* Wrap cd test in within to avoid interference
  • Loading branch information
quexxon authored Jun 12, 2023
1 parent d9f8a75 commit aef8209
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ cd('/tmp')
await $`pwd` // => /tmp
```

Like `echo`, in addition to `string` arguments, `cd` accepts and trims
trailing newlines from `ProcessOutput` enabling common idioms like:

```js
cd(await $`mktemp -d`)
```

### `fetch()`

A wrapper around the [node-fetch](https://www.npmjs.com/package/node-fetch)
Expand Down
6 changes: 5 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,11 @@ function syncCwd() {
if ($[processCwd] != process.cwd()) process.chdir($[processCwd])
}

export function cd(dir: string) {
export function cd(dir: string | ProcessOutput) {
if (dir instanceof ProcessOutput) {
dir = dir.toString().replace(/\n+$/, '')
}

$.log({ kind: 'cd', dir })
process.chdir(dir)
$[processCwd] = process.cwd()
Expand Down
8 changes: 8 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ test('cd() fails on entering not existing dir', async () => {
assert.throws(() => cd('/tmp/abra-kadabra'))
})

test('cd() accepts ProcessOutput in addition to string', async () => {
within(async () => {
const tmpDir = await $`mktemp -d`
cd(tmpDir)
assert.match(process.cwd(), tmpDir.toString().trimEnd())
})
})

test('kill() method works', async () => {
let p = $`sleep 9999`.nothrow()
setTimeout(() => {
Expand Down

0 comments on commit aef8209

Please sign in to comment.