Skip to content

Commit

Permalink
Add .fxrc support (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Nov 10, 2018
1 parent d86b06c commit b374176
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -121,6 +121,28 @@ $ npm install -g lodash
$ cat package.json | fx 'require("lodash").keys(this.dependencies)'
```

### Using .fxrc

Create _.fxrc_ file in `$HOME` directory, and require any packages or define global functions.

For example, access all lodash methods without `_` prefix. Put in your `.fxrc` file:

```js
Object.assign(global, require('lodash'))
```

And now you will be able to call all lodash methods. For example, see who's been committing to react recently:

```
curl 'https://api.github.com/repos/facebook/react/commits?per_page=100' \
| fx 'mapValues(groupBy(this, "commit.committer.name"), size)'
```

> To be able require global modules make sure you have correct `NODE_PATH` env variable.
> ```bash
> export NODE_PATH=/usr/local/lib/node_modules
> ```
### Formatting

If you need something different then JSON (for example arguments for xargs) do not return anything from reducer.
Expand Down
17 changes: 12 additions & 5 deletions index.js
@@ -1,6 +1,17 @@
#!/usr/bin/env node
'use strict'
const os = require('os')
const path = require('path')
const pretty = require('@medv/prettyjson')
const {stdin, stdout, stderr} = process

try {
require(path.join(os.homedir(), '.fxrc'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
}
}

const usage = `
Usage
Expand Down Expand Up @@ -28,8 +39,6 @@ const usage = `
`

function main(input) {
const {stdout, stderr} = process

if (input === '') {
stderr.write(usage)
process.exit(2)
Expand Down Expand Up @@ -88,15 +97,13 @@ function reduce(json, code) {
}

function run() {
const stdin = process.stdin
stdin.setEncoding('utf8')

let buff = ''

if (stdin.isTTY) {
main('')
}

let buff = ''
stdin.on('readable', () => {
let chunk

Expand Down

0 comments on commit b374176

Please sign in to comment.