sx
is a command line utility which executes javascript code, mostly to process input and/or generate output.
Pull requests are very welcome!
$ npm install -g sx
- 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).
- An interactive REPL
$ sx [options] "commands"
--pretty, -p
--json, -j
--line, -x
--list, -l
--async, -a
--filter, -f
--string, -s
--infile, -i [file]
--outfile, -o [file]
--file, -F [file]
--dialect, -d [dialect]
Pretty prints produced output, in case it's an object or an array.
Examples
$ sx -p "{hello:'world'}"
{
"hello": "world"
}
Accepts input as JSON, useful for chaining sx calls.
Examples
$ curl -sL http://freegeoip.net/json | sx -jx x.city
Berlin
Starts accepting input trough stdin, and exposes each line as x
.
Examples
$ ps | sx -x 'x.match(/\d+/)[0]'
337
345
4118
79235
97048
Treats all input passed through stdin as an array of lines, and exposes it as l
.
Examples
grep "console.log" * | sx -l l.length
5
Expects an asynchronous result, code must pass result to print callback exposed as a
.
Examples
$ /dev/urandom | sx -a "process.stdin.on('data', a); process.stdin.resume()"
...
Uses provided code as a predicate to test input.
Examples
$ ls | sx -fx "path.extname(x) === '.js'"
jayscript.js
Calls current object's toString method.
Examples
$ sx -s http.createServer
// vim: filetype=javascript
function (requestListener) {
return new Server(requestListener);
}
Uses provided file as input instead of stdin.
Examples
$ sx -xlfi ~/.zshrc x.match\(/export PATH/\)
node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Uses provided file as output instead of stdout.
NOTE: Contents of output file will be wipped out preventively.
Examples
$ tail -f /var/log/system.log | sx -xo local.log "console.log(x); x"
...
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
$ sx -bF Makefile "b.replace(/win32/i, 'darwin')"
Use another input dialect instead of javascript.
Javascript dialects are basically different languages which follow the style and the semantics of javascript.
This library includes the following dialects:
function pow(a, b){
for (var r = a, n = 0; n < b; n++) {
r = r * a
}
return r
}
function head(arr){
return arr.slice(0, 1);
}
Go-Script (gs): A clone of Go syntax without the type stuff.
func pow(a, b){
for r := a, n := 0; n < b; n++ {
r = r * a
}
return r
}
func head(arr){
return arr[0:1]
}
More information (will be available soon) on (node-jsjs)[http://github.com/aynik/jsjs] parser repository.
Accepts JSON input, treating it as a list and exposing each item.
Examples
$ sx '_.range(8)' | sx -jxlf x%2==0
0
2
4
6
$ npm install -g express
$ sx 'express.call().use(express.static("./")).listen(3000); "http://localhost:3000"'
http://localhost:3000
$ npm install -g request
$ sx -a 'request.call(0, "http://google.com", function(err, resp, body){ a(body) })'
...
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 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.
As an extra, sx
comes with an interactive REPL called isx
. It allows to build interactive programs by offering a set of REPL commands.
$ isx <session-filename.js (default: .session.js)>
- :load <session-filename.js> loads a worker and a new session into it's buffer
- :reload reloads the worker and feeds in current buffer
- :test sets the repl in test mode (no code is saved into the buffer)
- :build sets the repl in build mode (written code will be save into buffer)
- :write writes the current buffer to it's session file
- :drop N-N drops lines from the buffer at the given range
- :move N-N N moves selected lines at the given range to a destination line number in the buffer
- :print prints the current buffer
- :quit exits the repl