Sx
sx
is a command line utility which executes javascript code, mostly to process input and/or generate output.
Pull requests are very welcome!
Install
$ npm install -g sx
Features
- Various input modes, including list and JSON input parsing.
- Standard and file I/O supported.
- Many automations: module auto-loading, last value auto-return, auto-escaping for zsh and auto-completion both for zsh and bash.
- Poor man's beautifier (JSON.stringify).
Documentation
Usage
$ sx [options] "commands"
Options
--pretty, -p
--json, -j
--line, -x
--list, -l
--async, -a
--filter, -f
--string, -s
--infile, -i [file]
--outfile, -o [file]
--file, -F [file]
Combos
More examples
Extras
Options ### sx --pretty | -p
Pretty prints produced output, in case it's an object or an array.
Examples
An introductory hello world
$ sx -p "{hello:'world'}"
{
"hello": "world"
}
### sx --json | -j
Accepts input as JSON, useful for chaining sx calls.
Examples
Fetches geoip data and prints user's city
$ curl -sL http://freegeoip.net/json | sx -jx x.city
Berlin
### sx --line | -x
Starts accepting input trough stdin, and exposes each line as x
.
Examples
Prints all user's processes pids
$ ps | sx -x 'x.match(/\d+/)[0]'
337
345
4118
79235
97048
### sx --list | -l
Treats all input passed through stdin as an array of lines, and exposes it as l
.
Examples
Counts all matching occurrences
grep "console.log" * | sx -l l.length
5
### sx --async | -a
Expects an asynchronous result, code must pass result to print callback exposed as a
.
Examples
Echoes all incoming random bytes
$ /dev/urandom | sx -a "process.stdin.on('data', a); process.stdin.resume()"
...
### sx --filter | -f
Uses provided code as a predicate to test input.
Examples
Returns only javascript files
$ ls | sx -fx "path.extname(x) === '.js'"
jayscript.js
### sx --string | -s
Calls current object's toString method.
Examples
Inspects http.createServer
$ sx -s http.createServer
// vim: filetype=javascript
function (requestListener) {
return new Server(requestListener);
}
### sx --infile [file] | -i [file]
Uses provided file as input instead of stdin.
Examples
Filters matching lines from file
$ sx -xlfi ~/.zshrc x.match\(/export PATH/\)
node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
### sx --outfile [file] | -o [file]
Uses provided file as output instead of stdout.
NOTE: Contents of output file will be wipped out preventively.
Examples
Saves input while writing stdout
$ tail -f /var/log/system.log | sx -xo local.log "console.log(x); x"
...
### sx --file [file] | -F [file]
Uses provided file as input instead of stdin and at the same time as output instead of stdout, could be used to mutate contents of files, writing to them after the contents are on memory.
NOTE: Contents of output file will be wipped out preventively.
Examples
Replaces file contents in place
$ sx -bF Makefile "b.replace(/win32/i, 'darwin')"
Combos ### sx --json --line --list | -jxl
Accepts JSON input, treating it as a list and exposing each item.
Examples
Get all even numbers in a given range
$ sx '_.range(8)' | sx -jxlf x%2==0
0
2
4
6
More examples #### Express static server
$ npm install -g express
$ sx 'express.call().use(express.static("./")).listen(3000); "http://localhost:3000"'
http://localhost:3000
HTTP GET with request
$ npm install -g request
$ sx -a 'request.call(0, "http://google.com", function(err, resp, body){ a(body) })'
...
Extras ### Auto-completion
Auto-completion is provided as a script for bash and zsh.
Test in current zsh:
$ source <(sx --completion)
Install (bash):
$ echo "source <(sx --completion)" >> ~/.bashrc
Install (zsh):
$ echo "source <(sx --completion)" >> ~/.zshrc
Usage (core):
$ sx fs.<tab>
$ sx h<tab>
Usage (external module):
$ npm install -g lodash
$ sx _.<tab>
### Auto-escaping
Auto-escaping is provided as a function for zsh.
Install:
- First you'll have to find what are your zsh function paths.
$ echo $fpath
/usr/share/zsh/site-functions /usr/share/zsh/5.0.2/functions
Then chose the first of them, and install the script (you may need root permissions):
$ sudo sx --escaping > /usr/share/zsh/site-functions/sx-escape-magic
And add to your .zshrc
an autoload command:
$ echo "autoload -Uz sx-escape-magic" >> ~/.zshrc
$ echo "sx-escape-magic" >> ~/.zshrc
Now when you restart your shell you should be able to write javascript code and special characters will be automatically escaped.