Skip to content

Commit

Permalink
Prepared to handle and to execute .js files
Browse files Browse the repository at this point in the history
README.md improvements regarding usage via CLI
  • Loading branch information
Diego ZoracKy authored and Diego ZoracKy committed Jan 5, 2018
1 parent a3927f5 commit 273a2c4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
48 changes: 48 additions & 0 deletions README.md
Expand Up @@ -10,6 +10,8 @@ The main goal is to have any module prepared to be executed via CLI (installed g
To see why I believe you should plug it on your module, even if you don't need a CLI (it probably will serve someone on the community), read here: [Introducing MagiCLI: Automagically generates a command-line interface (CLI) for any module
](https://hackernoon.com/introducing-magicli-automagically-generates-a-command-line-interface-cli-for-any-module-49543e50f86d)

**It can be installed globally, in order to *execute* any module or .js file via CLI.**

## Goals

* Minimal setup (*one line*)
Expand All @@ -19,6 +21,7 @@ To see why I believe you should plug it on your module, even if you don't need a
* *Name*, *Description* and *Version* extracted from package.json
* Simple API to hook into the execution flow (*stdin*, *before*, *after*)
* Cover all possible cases of module.exports (*Function*, *Object* with nested properties, Destructuring parameters)
* Provide a CLI to be used to execute any given module or .js file via CLI

## Usage (the most simple and minimal way)

Expand Down Expand Up @@ -65,6 +68,51 @@ The program will be expecting options with the same name as the parameters decla

Important: MagiCLI requires the module in order to analyse it, and provide the command-line interface for it. Keep that in mind in case your module does something just by being required.

## Usage via CLI

In order to **execute** any module or .js file via CLI, install it globally:

```bash
$ npm install magicli -g
```

Then just pass in as the first argument, the path to a module or a .js file. Examples:
* `$ magicli . --help`
* `$ magicli ./path/to-some-module --help`
* `$ magicli ./path/to-a-file.js --help`

Or use it via **[npx](http://blog.npmjs.org/post/162869356040/introducing-npx-an-npm-package-runner)** without the need to install it.

Let's suppose that you have a simple .js file as this one:

```javascript
module.exports = {
sum: (n1, n2) => n1 + n2,
ec: {
ho: str => `${str} !!!`
}
}
```

Just execute **magicli** on it, as `$ magicli ./path/to-the-file-above.js --help` and you will get:

```bash
Commands:
sum
ec-ho
```

`$ magicli ./path/to-the-file-above.js sum --help` will give you:
```bash
Usage:
$ sum [options]

Options:
--n1
--n2
```
and `$ magicli ./path/to-the-file-above.js sum --n1=4 --n2=2` will result in `6`

### How it works

MagiCLI is capable of handling many styles of `exports`, like:
Expand Down
9 changes: 8 additions & 1 deletion lib/magicli.js
Expand Up @@ -25,8 +25,15 @@ function requireMainModule(currentPath) {
};
}

function requireJsFile(currentPath) {
return {
moduleRef: require(currentPath),
packageJson: {}
};
}

function magicli({ commands = {}, validateRequiredParameters = false, help = {}, version = {}, pipe = {}, enumerability = 'enumerable', subcommandDelimiter = '-', modulePath } = {}) {
const { moduleRef, packageJson } = requireMainModule(modulePath || require.main.filename);
const { moduleRef, packageJson } = modulePath && path.extname(modulePath) === '.js' ? requireJsFile(modulePath) : requireMainModule(modulePath || require.main.filename);
const moduleName = packageJson.name;
const moduleVersion = packageJson.version;
const moduleDescription = packageJson.description;
Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,11 +1,11 @@
{
"name": "magicli",
"version": "0.1.0",
"description": "Automagically generates command-line interfaces (CLI) for any module. Expected options and help sections are created automatically based on parameters names, with support to async.",
"version": "0.1.1",
"description": "Automagically generates command-line interfaces (CLI) for any module. Expected options and help sections are created automatically based on parameters names, with support to async. It can be installed globally, in order to *execute* any module, or .js file, via CLI.",
"main": "lib/magicli.js",
"bin": "bin/cli.js",
"scripts": {
"test": "mocha ./tests -b",
"test": "mocha ./tests",
"test-main": "mocha ./tests/main.test.js",
"test-cli": "mocha ./tests/cli.test.js"
},
Expand Down

0 comments on commit 273a2c4

Please sign in to comment.