runit-on-cli

Run node modules directly on CLI

Usage no npm install needed!

<script type="module">
  import runitOnCli from 'https://cdn.skypack.dev/runit-on-cli';
</script>

README

runit-on-cli

Run node modules directly on CLI

CircleCI

Install

npm install -g runit-on-cli

API

$ runit-on-cli <module-name> <named-export> [-c] [-f [functionName]] [-n [version]] [-p [parameters...]] [-s] [-t [transformFunction]] [-u [subModule]]
  • -c, --call-module-as-function: call the exported module as a function intead of object
  • -f, --function-name [functionName]: call a specific function from exported module
  • -n, --npm-module-version [version]: run a specific version of the npm module
  • -p, --params [parameters...]: list of params that will be passed to the module/function call
  • -s, --silent: print only the module output, without progress or logs
  • -t, --transform-output [transformFunction]: define a function to modify module/function return
  • -u, --sub-module [subModule]: import a submodule, such as 'crypto-js/sha256'

Examples

Running a module with default export, passing one string parameter:

$ runit-on-cli jwt-decode -p \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\"

Image

The above example using silent mode (-s option) will output only the return content:

{ sub: '1234567890', name: 'John Doe', iat: 1516239022 }

Running a module with a named export, passing two parameters, an object and an array:

$ runit-on-cli -s lodash orderBy -p '[{name:"banana"},{name:"apple"}]' '["name"]'
[ { name: 'apple' }, { name: 'banana' } ]

Passing a parameter from a file content:

$ runit-on-cli -s lodash orderBy -p "$(cat ./users.json)" '["first"]'
[
  { first: 'ane', last: 'mcfly' },
  { first: 'john', last: 'mayer' },
  { first: 'mary', last: 'jane' }
]

A more complex example using lodash map:

$ runit-on-cli -s lodash map -p "$(cat ./users.json)" 'e => {e.full = e.first + " " + e.last; return e;}'
[
  { first: 'mary', last: 'jane', full: 'mary jane' },
  { first: 'john', last: 'mayer', full: 'john mayer' },
  { first: 'ane', last: 'mcfly', full: 'ane mcfly' }
]

Running async functions and transform the output to return a specific property:

$ runit-on-cli -s axios get -p \'http://universities.hipolabs.com\' -t 'output.data'
{
    data: {
        author: { name: 'hipo', website: 'http://hipolabs.com' },
        github: 'https://github.com/Hipo/university-domains-list',
        example: 'http://universities.hipolabs.com/search?name=middle&country=Turkey'
    }
}

A more complex example of using -t option:

$ runit-on-cli -s moment -t 'moment().add(7, "days").format("YYYY/MM/DD")'
2021/08/01

Using the -t option is possible to use the node environment, actually.

Using the node environment by -t option:

$ runit-on-cli -s axios get -p \'http://universities.hipolabs.com\' -t 'Object.keys(output.data)'
[ 'author', 'github', 'example' ]

Call a specific function from exported module:

$ runit-on-cli chalk -f blue -p \"Hello World\"

Image

The same result is got by using -t option: runit-on-cli chalk -p \"Hello World\" -t 'chalk.blue(output)'

Another example of calling a specific function from exported module:

$ runit-on-cli -s faker -f phone.phoneNumber
550-681-2495

Running a module with a named export, without parameters:

$ runit-on-cli -s uuid v4
2a7bb8f8-ac20-46ea-a0eb-ea58df26d48e

Running the exported module as a function intead of object, and call a specific function:

$ runit-on-cli moment -c -f add -p 7 \'days\'

Image

The -c options is necessary for this case because moment export is a function: moment().add(7,'days')

Running a submodule:

$ runit-on-cli crypto-js -u sha256 -p \'test\'

Image

License

MIT © Danilo Sampaio