sx

Javascript's command-line swiss army knife

Usage no npm install needed!

<script type="module">
  import sx from 'https://cdn.skypack.dev/sx';
</script>

README

Sx

Build Status via Travis CI

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).
  • An interactive REPL

Command-line tool

Usage

$ sx [options] "commands"

Options

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')"

### sx --dialect [dialect] | -d [dialect]

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:

Standard Javascript (js): Common ECMAScript 5.

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.


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.


## Interactive REPL

As an extra, sx comes with an interactive REPL called isx. It allows to build interactive programs by offering a set of REPL commands.

Usage

$ isx <session-filename.js (default: .session.js)> 

In-REPL Commands

  • :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