Added commanline switch =json

This commit is contained in:
dig 2020-08-16 12:20:41 +02:00
parent 7043cf4ae5
commit c72cf1339a
3 changed files with 30 additions and 2 deletions

View File

@ -60,7 +60,7 @@ If only a path to the file is provided, the default export is returned if any.
You can access any named exports:
```bash
esm myModule:user_config
> { "some": "json" }
> { some: 'json' }
```
### Use exported functions
@ -143,6 +143,26 @@ import { myAwesomeFunction3 } from 'myLib'
myAwesomeFunction3({ 0: "first", 3: "forth", foo: true })
```
### Choose the type of result rendered
As default esm-cli uses `console.log` to render the resulting object to output.
It's great to get primitives correctly rendered like strings without `"`.
But you may sometimes need to get a JSON string representation of object, like
when working with stings quoted ans escaped, or to get a complete JSON object.
To do so, you can use the commandline switch `=json` that can be placed in any order and is not given to the module as param:
```
> esm myLib
> Hello world!
> esm myLib =json
> "Hello world!"
esm myModule:user_config
> { some: 'json' }
> esm myModule:user_config =json
> {"some":"json"}
```
## Usage with managers
### forever

View File

@ -1,7 +1,10 @@
#!/usr/bin/env node
const { resolve } = require('path')
const switches = { json: false }, switchReg = /^=/
let params = process.argv.slice(2)
params = params.map( p=> switchReg.test(p) ? (switches[p.slice(1)] = true, null) : p )
.filter( o=>o )
const moduleStr = params.shift()
const [ esmodule, exported ] = (moduleStr || '').split(':')
@ -32,6 +35,11 @@ let returned =
typeof toCall == 'function'
? toCall( args )
: toCall
const render = obj=> switches.json
? process.stdout.write( obj&& JSON.stringify(obj,null,'\t') || '' )
: console.log( obj || '' )
//console.log( 'type' , typeof returned, returned instanceof Promise )
returned instanceof Promise
? returned.then( ret=> console.log(ret||''), e=>console.error(e) )

View File

@ -1,6 +1,6 @@
{
"name": "esm-cli",
"version": "1.0.1",
"version": "1.1.0",
"description": "ECMAScript modules launcher",
"engines": {
"node": ">=6"