snang
run js from the command line
Last updated 10 months ago by brekk .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install snang 
SYNC missed versions from official npm registry.

▄████████ ███▄▄▄▄ ▄████████ ███▄▄▄▄ ▄██████▄
███ ███ ███▀▀▀██▄ ███ ███ ███▀▀▀██▄ ███ ███ ███ █▀ ███ ███ ███ ███ ███ ███ ███ █▀
███ ███ ███ ███ ███ ███ ███ ▄███
▀███████████ ███ ███ ▀███████████ ███ ███ ▀▀███ ████▄
███ ███ ███ ███ ███ ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ ███ ███ ███ ███ ▄████████▀ ▀█ █▀ ███ █▀ ▀█ █▀ ████████▀

snang

pipe a file or stdin into snang and use JS to transform it

globals

  1. "x" refers to the current buffer contents
  2. Both "ramda" and "entrust" have been injected into the sandbox, so any method of theirs will work
  3. "exec" allows you to run child_process.execSync on the given string

examples

  • read name from package.json snang -r package.json -i "x.name"
  • list devDependencies in package.json which have eslint in the key, return as string snang -r package.json -iP "prop('devDependencies') | keys | filter(matches('eslint')) | join(' ')"
  • read a file, transform it, then invoke it cat src/fixture.txt | snang -P "split('\n') | map(y => y.length ? y : ' ') | join('') | exec"

cli flags

  • --help -h help text / you are here

read / write

  • --read -r read a file in (otherwise read from stdin)
  • --write -w write a file out (otherwise write to stdout)

json format

  • --json -i read in json
  • --jsonOut -o write out json

composition

  • --pipe -p wrap a series of commands with pipe
  • --shell -P implies --pipe and allows for easier syntax
  • --compose -c wrap a series of commands with compose

print

  • --print -l instead of running the commands, print them
  • --prettyPrint -L instead of running the commands, pretty print them (-R implies -r)

export file

  • --exportFile -x print all commands as an exportable file
  • --exportES6 -X print all commands as an es6 formatted exportable file

API

Table of Contents

yargsParserOptions

Options hash to be passed to yargs-parser.

Type: Object

Properties

runInContext

Given an array of [sandbox, expression] Objects, process commands.

Parameters

  • config Object Configuration from yargs-parser.
  • context Array An array of context Objects.
    • context.0 Object A sandbox Object.
    • context.1 string A string expression.

Returns string Out string or json.

converter

Core chunk => evaluated expression function.

Parameters

  • config Object Config from yargs.
  • expressions [string] List of expression strings.
  • chunk (Buffer | string) Chunk from stream.

Returns (Object | string) Converted chunk.

errors

Errors object, used both for filtering and constant definition.

Type: Object

Properties

  • chunkArgument string A chunk argument error.
  • useObjectFlag string A replacement error for the chunk argument error.

handleErrors

Pure error handler which attempts to address known common errors.

Parameters

  • warn Function Function to receive errors.

Examples

handleErrors(console.warn)

Returns Function onError handler.

onError

The inner error handler.

Parameters

parseCommands

Parse used commands from a given list of expressions.

getExpression

Convert a list of expressions into a string to be evaluated in a virtual machine context.

Parameters

  • config Object Config from yargs-parser.
  • expressions [string] Input expressions.
  • sandbox Object Sandbox object for evaluating the eventual expression against.

Returns string The expression to evaluate.

createComposedExpression

Parameters

  • pretty boolean Pretty print the expression?
  • expressions [string] An array of expressions.
  • name string Name of the function to compose.

Returns string Composed / piped string.

makeSandbox

Create a sandbox object to be used within the vm container.

Parameters

  • x string String which represents stdin | file stream.

Examples

makeSandbox("cool")

Returns Object Sandbox

toString

Returns string

concatConditionally

Binary concat method which is used to define both prepend and append methods.

Parameters

  • order boolean Prepend or append?
  • circumstance boolean Condition?
  • str string The string to append or prepend.
  • x string The string to be appended or prepended to.

Returns string

tackOn

TackOn(y => y * 5, 10) === [10, 50].

Parameters

  • fn Function A function to effect change.
  • x any Something else.

Returns Array X followed by a transformed x.

Current Tags

  • 0.1.0-beta                                ...           beta (7 months ago)
  • 0.0.6                                ...           latest (10 months ago)

7 Versions

  • 0.1.0-beta                                ...           7 months ago
  • 0.0.6                                ...           10 months ago
  • 0.0.5                                ...           10 months ago
  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 7
Dependencies (9)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |