Added commanline switch =json
This commit is contained in:
parent
7043cf4ae5
commit
c72cf1339a
22
README.md
22
README.md
|
@ -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:
|
You can access any named exports:
|
||||||
```bash
|
```bash
|
||||||
esm myModule:user_config
|
esm myModule:user_config
|
||||||
> { "some": "json" }
|
> { some: 'json' }
|
||||||
```
|
```
|
||||||
|
|
||||||
### Use exported functions
|
### Use exported functions
|
||||||
|
@ -143,6 +143,26 @@ import { myAwesomeFunction3 } from 'myLib'
|
||||||
myAwesomeFunction3({ 0: "first", 3: "forth", foo: true })
|
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
|
## Usage with managers
|
||||||
|
|
||||||
### forever
|
### forever
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const { resolve } = require('path')
|
const { resolve } = require('path')
|
||||||
|
const switches = { json: false }, switchReg = /^=/
|
||||||
let params = process.argv.slice(2)
|
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 moduleStr = params.shift()
|
||||||
const [ esmodule, exported ] = (moduleStr || '').split(':')
|
const [ esmodule, exported ] = (moduleStr || '').split(':')
|
||||||
|
@ -32,6 +35,11 @@ let returned =
|
||||||
typeof toCall == 'function'
|
typeof toCall == 'function'
|
||||||
? toCall( args )
|
? toCall( args )
|
||||||
: toCall
|
: 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 )
|
//console.log( 'type' , typeof returned, returned instanceof Promise )
|
||||||
returned instanceof Promise
|
returned instanceof Promise
|
||||||
? returned.then( ret=> console.log(ret||''), e=>console.error(e) )
|
? returned.then( ret=> console.log(ret||''), e=>console.error(e) )
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "esm-cli",
|
"name": "esm-cli",
|
||||||
"version": "1.0.1",
|
"version": "1.1.0",
|
||||||
"description": "ECMAScript modules launcher",
|
"description": "ECMAScript modules launcher",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=6"
|
"node": ">=6"
|
||||||
|
|
Loading…
Reference in New Issue