esm-cli/README.md

173 lines
4.3 KiB
Markdown
Raw Permalink Normal View History

2019-04-10 23:42:14 +02:00
# esm-cli
2019-05-28 00:37:40 +02:00
ECMAScript modules launcher in Node 6+
2019-04-11 00:55:07 +02:00
2019-05-28 00:37:40 +02:00
Lunch ES modules using command line interpreter, accessing a specific export function or data, and passing parameters.
2019-04-11 00:55:07 +02:00
## Install
2019-05-28 00:37:40 +02:00
Just install module as a global command with npm
2019-04-11 00:55:07 +02:00
`npm install -g esm-cli`
2019-05-28 00:37:40 +02:00
Or you can install it globaly directly from a git clone:
2019-04-11 00:55:07 +02:00
```
2019-05-28 00:37:40 +02:00
git clone https://git.p2p.legal/dig/esm-cli
2019-04-11 00:55:07 +02:00
cd esm-cli
npm install -g .
```
You should get acces to a global `esm` command.
## Usage
2019-05-28 00:37:40 +02:00
With esm-cli you can instanciate a full ES module file, executing it's content,
and retreive an exported variable or execute an exported function.
It understand returned promises or async functions.
2019-04-11 00:55:07 +02:00
`esm path/to/esm_file:export_name [params]`
2019-05-28 00:37:40 +02:00
In this exemple, the file `./path/to/esm_file.js` is executed and the exported function is executed passing the [parsed params](#parameters-parsing):
`await export_name( [params] )`
> Note: .js extension is not needed (nodejs resolver).
2019-04-11 00:55:07 +02:00
### Get exported variables
Let's write a simple ES module (myModule.js):
```javascript
export default 42
export var user_config = {
some: "json"
}
```
And let's use it in cli:
```bash
esm myModule
> 42
```
In a bash file you can invoke some text return using command subsitutions:
```bash
#!/usr/bin/env bash
es_var=$(esm myModule)
echo "EcmaScript said: $es_var"
```
2019-05-28 00:37:40 +02:00
If only a path to the file is provided, the default export is returned if any.
2019-04-11 00:55:07 +02:00
You can access any named exports:
```bash
esm myModule:user_config
2020-08-16 12:20:41 +02:00
> { some: 'json' }
2019-04-11 00:55:07 +02:00
```
### Use exported functions
When the exported "thing" is a function (async or not), it is executed passing command line parameters if given.
Imagine a simple ES module library (myLib.js):
```javascript
export default function hello()
{
console.log( 'Hello world !' )
}
export function greet( params ) {
return `Hello ${params[0]}`
}
```
Then you can write:
```bash
esm myLib
> Hello world !
```
```bash
esm myLib:greet Pierre
> Hello Pierre !!
```
### Promise/async support
If the returned result is a promise, because explicitly returned, or from an async function, esm-cli will take care of waiting it's resolution returning the resolved value (gently catching rejections showing nothing).
### Parameters parsing
Command line parameters are parsed into an Array before it is given to a called function.
This Array contains a list of strings corresponding to each string given without hyphen `-`:
(myLib.js)
```javascript
export function showParams( params )
{
console.log( params )
}
```
```bash
esm myLib:showParams param1 param2 param3
> [ "param1", "param2", "param3" ]
```
... and this array is augmented with properties corresponding to hyphen params, with value or "true" if not:
```bash
esm myLib:showParams param1 param2 param3 -s --t -foo --bar -baz:42 --val=some
> [ "param1", "param2", "param3", s: true, t: true, foo: true, bar: true, baz: 42, val: "some" ]
```
2019-05-28 01:00:24 +02:00
> Note: order don't matters...
2019-04-11 00:55:07 +02:00
You can then use ES destructuration to filter named paremeters:
```javascript
2019-05-28 01:00:24 +02:00
// Array destructuration gets simple params
export function myAwesomeFunction( [ first, second, , forth ] )
2019-04-11 00:55:07 +02:00
{
console.log( first, second, forth )
}
2019-05-28 01:00:24 +02:00
// Object destructuration gets named params
export function myAwesomeFunction2( { foo, val, baz } )
2019-04-11 00:55:07 +02:00
{
console.log( foo, val, baz )
}
2019-05-28 01:00:24 +02:00
// Object destructuration for both, using index name
export function myAwesomeFunction3( { 0:param1, 3:param4, foo, val, baz } )
2019-04-19 17:44:43 +02:00
{
console.log( param1, param4, foo, val, baz )
}
2019-04-19 17:49:36 +02:00
```
2019-05-28 01:00:24 +02:00
To call this kind of function in js and create an augmented param array:
2019-04-19 17:49:36 +02:00
```javascript
2019-05-28 01:00:24 +02:00
import { myAwesomeFunction3 } from 'myLib'
2019-04-19 17:49:36 +02:00
myAwesomeFunction3({ 0: "first", 3: "forth", foo: true })
```
2020-08-16 12:20:41 +02:00
### 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"}
```
2020-08-16 11:35:11 +02:00
## Usage with managers
### forever
````bash
forver -c esm myScript --param option
```