[![npm version](https://badge.fury.io/js/%40niweera%2Fnode-asterer.svg)](https://badge.fury.io/js/%40niweera%2Fnode-asterer)
[![NPM Downloads](https://img.shields.io/npm/dt/@niweera/node-asterer)](https://www.npmjs.com/package/@niweera/node-asterer)
![GitHub](https://img.shields.io/github/license/Niweera/node-asterer)
[![codecov](https://codecov.io/gh/Niweera/node-asterer/branch/main/graph/badge.svg?token=1NQCW1J9UK)](https://codecov.io/gh/Niweera/node-asterer)
[![Build Status](https://travis-ci.com/Niweera/node-asterer.svg?branch=main)](https://travis-ci.com/Niweera/node-asterer)
[![Known Vulnerabilities](https://snyk.io/test/github/Niweera/node-asterer/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Niweera/node-asterer?targetFile=package.json)
[![Dependency Status](https://david-dm.org/Niweera/node-asterer.svg)](https://david-dm.org/Niweera/node-asterer)
[![devDependencies Status](https://david-dm.org/Niweera/node-asterer/dev-status.svg)](https://david-dm.org/Niweera/node-asterer?type=dev)
[![Total alerts](https://img.shields.io/lgtm/alerts/g/Niweera/node-asterer.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Niweera/node-asterer/alerts/)
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Niweera/node-asterer.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Niweera/node-asterer/context:javascript)

# [Node-ASTerer](https://www.npmjs.com/package/@niweera/node-asterer)

Node-ASTerer is a CLI tool to generate the [AST](https://www.digitalocean.com/community/tutorials/js-traversing-ast) of a given JavaScript file, and it will save it in a *.json file.

Node-ASTerer uses [AcornJS](https://github.com/acornjs/acorn) under the hood to generate the AST.

## Usage

```bash
$ npm install -g @niweera/node-asterer
$ node-asterer -i /path/to/javascript/file.js -o /output/path/of/file.json
```
## Advanced Usage 

You can provide a `Acorn` configuration file to customize the AST generation with `Acorn`. 
Follow [Acorn documentation](https://www.npmjs.com/package/acorn#interface) to provide the configuration options.

```bash
$ node-asterer -i /path/to/javascript/file.js -o /output/path/of/file.json -c /path/to/config/file.json
```

The `Acorn` configuration JSON file should look like this.

```json
{
  "ecmaVersion": 2020,
  "locations": true,
  "sourceType": "script",
  "allowReserved": true,
  "allowHashBang": true
}
```

If a custom `Acorn` configuration file is not provided, AST generation will be done using the `Acorn` default configuration.

## Example 

The following is the input JavaScript file `sample.js`.

```javascript
const example = "example";

```

The following is the output JSON file `output.json`.

```json
{
    "type": "Program",
    "start": 0,
    "end": 27,
    "loc": {
        "start": {
            "line": 1,
            "column": 0
        },
        "end": {
            "line": 2,
            "column": 0
        }
    },
    "body": [
        {
            "type": "VariableDeclaration",
            "start": 0,
            "end": 26,
            "loc": {
                "start": {
                    "line": 1,
                    "column": 0
                },
                "end": {
                    "line": 1,
                    "column": 26
                }
            },
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "start": 6,
                    "end": 25,
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 6
                        },
                        "end": {
                            "line": 1,
                            "column": 25
                        }
                    },
                    "id": {
                        "type": "Identifier",
                        "start": 6,
                        "end": 13,
                        "loc": {
                            "start": {
                                "line": 1,
                                "column": 6
                            },
                            "end": {
                                "line": 1,
                                "column": 13
                            }
                        },
                        "name": "example"
                    },
                    "init": {
                        "type": "Literal",
                        "start": 16,
                        "end": 25,
                        "loc": {
                            "start": {
                                "line": 1,
                                "column": 16
                            },
                            "end": {
                                "line": 1,
                                "column": 25
                            }
                        },
                        "value": "example",
                        "raw": "\"example\""
                    }
                }
            ],
            "kind": "const"
        }
    ],
    "sourceType": "script"
}
```
