Skip to content

Commit

Permalink
feat: allow filtering properties, close #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 4, 2017
1 parent 428d61b commit d801415
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ execaWrap('failing-command', ['its', 'arguments'])
})
```

### Filtering properties

If you are not interested in every returned property, you can filter and get only some
properties. For example, let us grab `command` and `stdout`

```js
execaWrap('ls', ['src'], {filter: ['cmd', 'stdout']})
// command: ls src
// stdout
// ------
```

For a single filter, just use a string or single item array

```js
execaWrap('ls', ['src'], {filter: 'cmd'})
// command: ls src
```

### Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017
Expand Down
21 changes: 21 additions & 0 deletions __snapshots__/execa-wrap-spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
exports['fields filter 1'] = `
command: ls src
stdout:
-------
execa-wrap-spec.js
index.js
windows-spec.js
-------
`

exports['ls src 1'] = `
command: ls src
Expand Down Expand Up @@ -39,3 +50,13 @@ exports['boo src 1'] = `
-------
`

exports['single field filter 1'] = `
stdout:
-------
execa-wrap-spec.js
index.js
windows-spec.js
-------
`
11 changes: 11 additions & 0 deletions src/execa-wrap-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@ if (os.platform() !== 'win32') {
it('failing', () => {
return execaWrap('boo', ['src']).then(snapshot.bind(null, 'boo src'))
})

it('can filter single field', () => {
return execaWrap('ls', ['src'], { filter: 'stdout' }).then(
snapshot.bind(null, 'single field filter')
)
})
it('can filter several fields', () => {
return execaWrap('ls', ['src'], { filter: ['cmd', 'stdout'] }).then(
snapshot.bind(null, 'fields filter')
)
})
})
}
63 changes: 56 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,42 @@ const stripFields = R.evolve({
stderr: stripAnsi
})

const makeText = execResult => {
const stdout = indentString(stripLeadingIndent(execResult.stdout), 2).trim()
const stderr = indentString(stripLeadingIndent(execResult.stderr), 2).trim()
const makeFilteredText = result => {
// indent with 2 spaces every line
let message = ''
if (result.cmd) {
message += ` command: ${result.cmd}\n`
}
if (result.code) {
message += ` code: ${result.code}\n`
}
if (result.failed) {
message += ` failed: ${result.failed}\n`
}
if (result.killed) {
message += ` killed: ${result.killed}\n`
}
if (result.signal) {
message += ` signal: ${result.signal}\n`
}
if (result.stdout) {
message += ` stdout:
-------
${result.stdout}
-------
`
}
if (result.stderr) {
message += ` stderr:
-------
${result.stderr}
-------
`
}
return message
}

const makeAllText = execResult => {
return `
command: ${execResult.cmd}
code: ${execResult.code}
Expand All @@ -23,20 +55,37 @@ const makeText = execResult => {
stdout:
-------
${stdout}
${execResult.stdout}
-------
stderr:
-------
${stderr}
${execResult.stderr}
-------
`
}

function execWrapper (cmd, args) {
const makeText = (options = {}) => execResult => {
let { filter } = options

const result = R.clone(execResult)
result.stdout = indentString(stripLeadingIndent(result.stdout), 2).trim()
result.stderr = indentString(stripLeadingIndent(result.stderr), 2).trim()

if (typeof filter === 'string') {
filter = [filter]
}
if (Array.isArray(filter) && filter.length) {
return makeFilteredText(R.pick(filter, result))
}

return makeAllText(result)
}

function execWrapper (cmd, args, options = {}) {
const child = execa(cmd, args)
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
return child.then(stripFields, stripFields).then(makeText)
return child.then(stripFields, stripFields).then(makeText(options))
}

module.exports = execWrapper

0 comments on commit d801415

Please sign in to comment.