Skip to content

Commit

Permalink
Merge pull request #3 from linkdd/exec-no-capture
Browse files Browse the repository at this point in the history
✨ Add `exec()` method which does not capture output
  • Loading branch information
linkdd committed Jul 24, 2023
2 parents 0e487f3 + 176b104 commit 995f9b5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ const cmd = command('cat')
const res = await cmd.run()
```

We can also avoid capturing the output, and raise on errors:

```typescript
await command('echo', 'hello world').exec()
```

Parameters of both `run()` and `exec()` methods can be overridden:

```typescript
class CommandBuilder {
// ...
async run(options?: {
capture: { stdout: boolean, stderr: boolean },
raiseOnError: boolean,
}) { /* ... */ }

async exec(options?: {
capture: { stdout: boolean, stderr: boolean },
raiseOnError: boolean,
}) { /* ... */ }
}
```

The defaults are:

- `run()`: `{ capture: { stdout: true, stderr: true }, raiseOnError: false }`
- `exec()`: `{ capture: { stdout: false, stderr: false }, raiseOnError: true }`

## :page_facing_up: License

This project is released under the terms of the [MIT License](./LICENSE.txt).
54 changes: 48 additions & 6 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ export class CommandBuilder {
return new CommandBuilder({ type: 'writeStdin', cmd: this.node, data })
}

async run() {
private async eval(options: {
capture: { stdout: boolean, stderr: boolean },
raiseOnError: boolean,
}) {
const stdio = await evalCommand(this.node)
stdio.stdin.end()

const stdout = await readStream(stdio.stdout)
const stderr = await readStream(stdio.stderr)
const stdout = (
options.capture.stdout
? await readStream(stdio.stdout)
: (stdio.stdout.pipe(process.stdout), Buffer.from(''))
)
const stderr = (
options.capture.stderr
? await readStream(stdio.stderr)
: (stdio.stderr.pipe(process.stderr), Buffer.from(''))
)

let exitCode = null

Expand All @@ -66,11 +77,16 @@ export class CommandBuilder {
exitCode = 0
}
catch (err) {
if (err instanceof errors.NonZeroExitCode) {
exitCode = err.code
if (err instanceof errors.TShellOutError && !options.raiseOnError) {
if (err instanceof errors.NonZeroExitCode) {
exitCode = err.code
}
else {
console.error(err)
}
}
else {
console.error(err)
throw err
}
}
finally {
Expand All @@ -89,6 +105,32 @@ export class CommandBuilder {
stderr,
}
}

async run(options?: {
capture: { stdout: boolean, stderr: boolean },
raiseOnError: boolean,
}) {
return await this.eval({
capture: {
stdout: options?.capture?.stdout ?? true,
stderr: options?.capture?.stderr ?? true,
},
raiseOnError: options?.raiseOnError ?? false
})
}

async exec(options?: {
capture: { stdout: boolean, stderr: boolean },
raiseOnError: boolean,
}) {
return await this.eval({
capture: {
stdout: options?.capture?.stdout ?? false,
stderr: options?.capture?.stderr ?? false,
},
raiseOnError: options?.raiseOnError ?? true
})
}
}

export const command = (executable: string, ...args: string[]) =>
Expand Down

0 comments on commit 995f9b5

Please sign in to comment.