github.com/teddydd/sh@v2.6.4+incompatible/_js/README.md (about)

     1  ## mvdan-sh
     2  
     3  This package is a JavaScript version of a shell package written in Go, available
     4  at https://github.com/mvdan/sh.
     5  
     6  It is transpiled from Go to JS using a GopherJS fork, available at
     7  https://github.com/myitcv/gopherjs.
     8  
     9  ### Sample usage
    10  
    11  ```
    12  const sh = require('mvdan-sh')
    13  const syntax = sh.syntax
    14  
    15  var parser = syntax.NewParser()
    16  var printer = syntax.NewPrinter()
    17  
    18  var src = "echo 'foo'"
    19  var f = parser.Parse(src, "src.sh")
    20  
    21  // print out the syntax tree
    22  syntax.DebugPrint(f)
    23  console.log()
    24  
    25  // replace all single quoted string values
    26  syntax.Walk(f, function(node) {
    27          if (syntax.NodeType(node) == "SglQuoted") {
    28                  node.Value = "bar"
    29          }
    30          return true
    31  })
    32  
    33  // print the code back out
    34  console.log(printer.Print(f)) // echo 'bar'
    35  ```
    36  
    37  You can find more samples in
    38  [testmain.js](https://github.com/mvdan/sh/blob/master/_js/testmain.js).
    39  
    40  ### Available APIs
    41  
    42  The APIs listed below are wrapped to be usable in JavaScript. Follow the links
    43  to read their documentation.
    44  
    45  * [syntax.NewParser](https://godoc.org/mvdan.cc/sh/syntax#NewParser)
    46    - [Parser.Parse](https://godoc.org/mvdan.cc/sh/syntax#Parser.Parse)
    47    - [Parser.Interactive](https://godoc.org/mvdan.cc/sh/syntax#Parser.Interactive)
    48    - [Parser.Incomplete](https://godoc.org/mvdan.cc/sh/syntax#Parser.Incomplete)
    49  * [syntax.DebugPrint](https://godoc.org/mvdan.cc/sh/syntax#DebugPrint)
    50  * [syntax.Walk](https://godoc.org/mvdan.cc/sh/syntax#Walk)
    51  * [syntax.NewPrinter](https://godoc.org/mvdan.cc/sh/syntax#NewPrinter)
    52    - [Printer.Print](https://godoc.org/mvdan.cc/sh/syntax#Printer.Print)
    53  
    54  Constructor options like
    55  [syntax.KeepComments](https://godoc.org/mvdan.cc/sh/syntax#KeepComments) are
    56  also available.
    57  
    58  The original `io.Reader` parameters can take a string or a
    59  [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable)
    60  object. `io.Writer` parameters are replaced by string returns.
    61  
    62  The nodes you will find in the syntax tree are all equivalent to the nodes you
    63  will see on the Go API. To get the type of a node, use `syntax.NodeType` as the
    64  example above shows. Some of the most common node types include:
    65  
    66  * [syntax.File](https://godoc.org/mvdan.cc/sh/syntax#File)
    67  * [syntax.Stmt](https://godoc.org/mvdan.cc/sh/syntax#Stmt)
    68  * [syntax.CallExpr](https://godoc.org/mvdan.cc/sh/syntax#CallExpr)
    69  * [syntax.Word](https://godoc.org/mvdan.cc/sh/syntax#Word)
    70  * [syntax.Lit](https://godoc.org/mvdan.cc/sh/syntax#Lit)
    71  
    72  The five above will show up in your syntax tree if you parse a `echo foo`
    73  command, which you can see if you use `syntax.DebugPrint` to inspect the syntax
    74  tree.
    75  
    76  ### Building
    77  
    78  You will need:
    79  
    80  * Latest Go 1.11.x
    81  * Latest `gopherjs` from @myitcv's fork: https://github.com/myitcv/gopherjs
    82  * NodeJS, to run the `testmain.js` test suite
    83  
    84  Then, simply run `./build`. The result will be `index.js`, which isn't minified.
    85  At the time of writing, `index.js` weighs 1.7MiB in plaintext, and 220KiB when
    86  minified and gzipped.