diff --git a/README.md b/README.md index a52ae289..449d7af0 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index eb780aeb..b62a2e73 100755 --- a/index.js +++ b/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 @@ -28,8 +39,6 @@ const usage = ` ` function main(input) { - const {stdout, stderr} = process - if (input === '') { stderr.write(usage) process.exit(2) @@ -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