Stop using "npm publish"
Tags: npm, node, javascript
Published: 2021-09-19 09:57:31

Publishing your package to npm is not limited to a single command line "npm publish", there are other steps you have to do before release your product to other developers. But is there a way to optimize that in one single command line? Yes!

[Aux4](https://aux4.io) is an open source CLI (command line interface) generator friendly to use in your project. It generates a CLI from a simple JSON file. 

The JSON structure is simple, it's an object with a list of profiles. Each profile has a name and a list of commands. The main profile is where aux4 will start listing your commands.

## Install aux4
Aux4 is a npm package, you can easily install by:

```bash
npm install -g aux4
```

## Create the .aux4 file
You can create an `.aux4` file in the root of your project.

```json
{
  "profiles": [
    {
      "name": "main",
      "commands": [
        {
          "value": "release",
          "execute": [
            "echo 'npm publishing'"
          ]
        }
      ]
    }
  ]
}
```

## Execute
To execute just use `aux4` command from the root folder of your project or any sub-folder. The output will be `npm publishing`.

```bash
aux4 release

npm publishing
```

## Add the real steps
Let's say the first step you want to do is execute your tests, change the version in the `package.json` file, after that build your package, create a tag on git, and finally publish to npm, and. pushing your changes to the repository. Here are the steps:

1. test
1. define npm version
1. build
1. git tag
1. npm publish
1. git push

In this post I am just demonstrating what you can do, but have to adapt to your project's reality and perform the steps you need.

```json
{
  "profiles": [
    {
      "name": "main",
      "commands": [
        {
          "value": "release",
          "execute": [
            "npm test",
            "npm version patch",
            "npm run build",
            "json:cat package.json",
            "set:versionNumber=${response.version}",
            "git tag -a ${versionNumber} -m '${versionNumber}'",
            "git push --follow-tags",
            "npm publish ./build",
            "rm -rf build",
            "git push",
            "echo ${versionNumber} released successfully"
          ]
        }
      ]
    }
  ]
}
```

### What are those lines?

I am going to describe what's every line to make it more clear.

#### npm test
execute the tests.

#### npm version patch
increment the patch of the current version defined in your `package.json` file. e.g.: if your current version is `1.0.1` it will increment to `1.0.2`.

#### npm run build
it will build your project if you have it defined in the scripts. You can do that in different ways, this is just a demonstration.

#### json:cat package.json
`cat package.json` will print the content of the file to the output of the console. The prefix `json:` convert the JSON string to a JSON object.

#### set:versionNumber=${response.version}
In **aux4** `${response}` is the output of the previous line, in this case, how in the previous line we converted the JSON to an object, we can access its properties.
Here it's setting a variable `versionNumber` with the version of the package. The structure is `set:variable=value`.

#### git tag -a ${versionNumber} -m '${versionNumber}'
Create a [tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) in the repository with the same version. 

#### git push --follow-tags
Push only the tags to your git repository.

#### npm publish ./build
Publish the package to npm.

#### rm -rf build (optional)
Deletes the `build` folder. It's not needed, but it might be useful.

#### git push
Pushes your changes to the git repository.

#### echo ${versionNumber} released successfully
Displays `1.0.2 released successfully` to the output. 

## Add documentation to your command
Aux4 allows to document your commands, so the other people using that can easily understand what's the purpose of your commands. To do that you just need to add a help section to your command.

```json
{
  "profiles": [
    {
      "name": "main",
      "commands": [
        {
          "value": "release",
          "execute": [
            "npm test",
            "npm version patch",
            "npm run build",
            "json:cat package.json",
            "set:versionNumber=${response.version}",
            "git tag -a ${versionNumber} -m '${versionNumber}'",
            "git push --follow-tags",
            "npm publish ./build",
            "rm -rf build",
            "git push",
            "echo ${versionNumber} released successfully"
          ],
          "help": {
            "description": "publish a new version of my package to npm"
          }
        }
      ]
    }
  ]
}
```

The documentation is displayed when you execute the command `aux4`.

```bash
aux4

     aux4   aux4 utilities
  release   publish a new version of my package to npm
```

## Add a parameter
You can add a parameter in case you don't want to release a `patch` every time. So in the parameter you can specify which type of version you are releasing.

```json
{
  "profiles": [
    {
      "name": "main",
      "commands": [
        {
          "value": "release",
          "execute": [
            "npm test",
            "npm version ${version}",
            "npm run build",
            "json:cat package.json",
            "set:versionNumber=${response.version}",
            "git tag -a ${versionNumber} -m '${versionNumber}'",
            "git push --follow-tags",
            "npm publish ./build",
            "rm -rf build",
            "git push",
            "echo ${versionNumber} released successfully"
          ],
          "help": {
            "description": "publish a new version of my package to npm"
            "variables": [
              {
                "name": "version",
                "text": "type of version release. e.g.: major, minor, patch",
                "default": "patch"
              }
            ]
          }
        }
      ]
    }
  ]
}
```

Variables have three attributes: 

* `name`: the variable name
* `text`: the documentation of the variable
* `default`: the default value in case the variable is not defined.

#### npm version ${version}
Using the variable to specify the type of version.

### Documentation

```bash
aux4

     aux4   aux4 utilities
  release   publish a new version of my package to npm
              - version [patch] type of version release. e.g.: major, minor, patch
```

### Execution

```bash
aux4 release --version minor

1.1.0 released successfully
```

## Conclusion
aux4 is a great tool to optimize your time, simplify and document your scripts and make it easier to share with your team.
The main benefit of using aux4 is allowing all your team to not forget any important step while performing some task by sharing your custom project tool with your team.

## Comment
Your opinion matters, what do you think about aux4? Are you going to use it in your project? Please share your thoughts in the comment section.