diff --git a/README.md b/README.md index 2835a12..0840c1f 100644 --- a/README.md +++ b/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: ```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 diff --git a/bin/esm-cli.js b/bin/esm-cli.js index 5766cca..21c91be 100755 --- a/bin/esm-cli.js +++ b/bin/esm-cli.js @@ -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) ) diff --git a/package.json b/package.json index 5e59099..facb4cd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esm-cli", - "version": "1.0.1", + "version": "1.1.0", "description": "ECMAScript modules launcher", "engines": { "node": ">=6"