Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jjangga0214/hasura-cli

Hasura CLI as an npm package
https://github.com/jjangga0214/hasura-cli

Last synced: about 2 months ago
JSON representation

Hasura CLI as an npm package

Awesome Lists containing this project

README

        

# ![hasura-cli](./docs/assets/hasura-cli.svg)

An npm package that automatically installs and wraps **Hasura CLI** binary in isolated manner

[![npm version](https://img.shields.io/npm/v/hasura-cli?style=flat-square&labelColor=black&label=version)](https://www.npmjs.com/package/hasura-cli)
[![npm beta version](https://img.shields.io/npm/v/hasura-cli/beta?style=flat-square&labelColor=black&color=ffd900&label=beta)](https://www.npmjs.com/package/hasura-cli)
[![npm alpha version](https://img.shields.io/npm/v/hasura-cli/alpha?style=flat-square&labelColor=black&color=fedcba&&label=alpha)](https://www.npmjs.com/package/hasura-cli)
[![npm download](https://img.shields.io/npm/dm/hasura-cli?style=flat-square&labelColor=black&label=npm%20download)](https://www.npmjs.com/package/hasura-cli)

The package is automatically published (always up-to-date) when a new version is released from Hasura's side.

[![license](https://img.shields.io/badge/license-MIT-ff4081.svg?style=flat-square&labelColor=black)](./LICENSE)
[![test](https://img.shields.io/badge/test-jest-7c4dff.svg?style=flat-square&labelColor=black)](./jest.config.js)
[![code style:airbnb](https://img.shields.io/badge/code_style-airbnb-448aff.svg?style=flat-square&labelColor=black)](https://github.com/airbnb/javascript)
[![code style:prettier](https://img.shields.io/badge/code_style-prettier-18ffff.svg?style=flat-square&labelColor=black)](https://prettier.io/)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-ffab00.svg?style=flat-square&labelColor=black)](https://conventionalcommits.org)
[![Commitizen friendly](https://img.shields.io/badge/Commitizen-cz_conventional_changelog-dd2c00.svg?style=flat-square&labelColor=black)](http://commitizen.github.io/cz-cli/)
![pr welcome](https://img.shields.io/badge/PRs-welcome-09FF33.svg?style=flat-square&labelColor=black)

## Why?

The [**Original Hasura CLI**](https://github.com/hasura/graphql-engine/tree/master/cli), which is not this package, is a compiled binary originally written in go. But just installing it on your system could cause some problems.

1. Difficult to use different hasura versions on multiple projects.
2. Inconvenient to ensure every colleagues having same version installed.
3. Manual installation not specified as npm devDependency.

**hasura-cli** solves them. It automatically downloads the CLI and exposes the command `hasura`. Downloaded CLI would be isolated, making it only dedicated to the "project" that installed it. Of course, you can install it as global package as well.

## Installation

You can simply install hasura-cli through npm, yarn or pnpm. Note that this package follows a version of the [**Original Hasura CLI**](https://github.com/hasura/graphql-engine/tree/master/cli). If you want to check its releases, go [here](https://github.com/hasura/graphql-engine/releases).

Currently, there are 3 npm tags (npm tags are different from versions), `latest`, `beta` and `alpha`. `latest` tag refers to Hasura's latest stable version(e.g. [![npm version](https://img.shields.io/npm/v/hasura-cli?style=flat-square&labelColor=black&label=version)](https://www.npmjs.com/package/hasura-cli)), while `beta` and `alpha`, respectively beta version(e.g. [![npm beta version](https://img.shields.io/npm/v/hasura-cli/beta?style=flat-square&labelColor=black&color=ffd900&label=beta)](https://www.npmjs.com/package/hasura-cli)) and alpha version(e.g. [![npm alpha version](https://img.shields.io/npm/v/hasura-cli/alpha?style=flat-square&labelColor=black&color=fedcba&&label=alpha)](https://www.npmjs.com/package/hasura-cli)).

Of course, you can install it globally,

```bash
npm install --global hasura-cli[@tag|@version]
```

or in a project.

```bash
# latest version from latest tag (Same as hasura-cli@latest)
npm install --save-dev hasura-cli

# specific version
npm install --save-dev [email protected]

# latest version from beta tag
npm install --save-dev hasura-cli@beta

# latest version from alpha tag
npm install --save-dev hasura-cli@alpha
```

Then you will be able to run hasura command.

For example,

```bash
# print hasura version
npx hasura version
```

Or configure npm scripts on package.json in the way you want.
(tip. provide env vars like `$HASURA_GRAPHQL_ENDPOINT` or `$HASURA_GRAPHQL_ADMIN_SECRET`)

```json
{
"scripts": {
"hasura": "hasura --project hasura --skip-update-check",
"hasura:console": "npm run hasura console",
"hasura:apply": "npm run hasura migrate apply"
}
}
```

## Support

Generally, it works on 64 bits architecture of any Linux, macOS, and Windows with node@>=16.

## Development (Contribution)

### Quick PR for new version

It's simple. Just update the `version` in [package.json](./package.json), then make a Pull Request. That's it!

```jsonc
{
"name": "hasura-cli",
"version": "1.3.0", // Patch this to "1.3.1-beta.1", for example.
"license": "MIT"
// ...
}
```

### Note

Please read [NOTE.md](./docs/NOTE.md), before getting started.

### Environment variables

Environment variables are intended to be only used on development environment.

First, create `.env` file, and configure it as you want.

```bash
cp .env.example .env
```

You can simply populate the variables by executing `pnpm dev` or `pnpm dev:no-respawn`. Otherwise, you have to manually feed them (e.g. `dotenv -- `). That's because this project doesn't use [`dotenv`](https://github.com/motdotla/dotenv), but [`dotenv-cli`](https://github.com/entropitor/dotenv-cli). So, the application does not read `.env` by itself.

#### `HASURA_CLI_INSTALL` (boolean)

Whether `src/index.ts` would install the cli. You can set it `false` to prevent unwanted downloads.

#### `HASURA_CLI_DEST_DIR` (string)

A directory where Hasura CLI should be installed.

#### `HASURA_CLI_DEST_FILENAME` (string)

A file name of Hasura CLI.

### Getting started

Install dependencies. Lifecycle script `postinstall` is only for clients who want to install the binary. So, ignore it with `--ignore-scripts` option. It should also be used on CI.

```bash
pnpm install --ignore-scripts
```

On development, you can run

```bash
pnpm dev
# or
pnpm dev:no-respawn
# or
pnpm dev:build
```

`pnpm dev` watches source code and restarts a process when a file changes. It does not write compiled js to the file system. [ts-node-dev](https://github.com/whitecolor/ts-node-dev) watches, compiles and restarts.

`pnpm dev:no-respawn` does the same thing except it does not restart.

`pnpm dev:build` logically does the identical job at the high viewpoint. But it compiles (`tsc -w`) ts, writes js on file system, and runs (`nodemon`) js. [concurrently](https://github.com/kimmobrunfeldt/concurrently) runs `tsc` and `nodemon` simultaneously.

To manually test compiled js, you can run

```bash
pnpm build # compiles ts to js
pnpm start # runs dist/index.js
```

### Other scripts

```bash
pnpm test # runs all tests (against "*.test.ts")
pnpm test:coverage # runs all tests and measures coverage
pnpm lint . # lint
```

### How does this work?

Here is a brief file system tree.

```
hasura-cli
├── dist // to be generated by build process (e.g. `pnpm build`), and ignored by git
├── hasura
├── package.json
└── src
   ├── asset.ts
   ├── index.ts
   └── install.ts
```

package.json exposes the command `hasura` as a symlink to the flie `hasura`. Only the directory `dist` and file `hasura` are packed as a package.

```json
{
"bin": {
"hasura": "./hasura"
},
"files": ["dist", "hasura"]
}
```

However, when publishing the package, the file `hasura` is just a dummy 'text' file, not a binary file. The file will be replaced with a binary only when a client installs the package on Linux or macOS. On Windows, unlike Linux or macOS, the file `hasura` is to be removed, and a new file `hasura.exe` will be created. `postinstall` lifecycle hook executes `dist/index.js`, which would install the platform-specific binary.

[The binaries](https://github.com/hasura/graphql-engine/releases) are hosted on GitHub as release assets. `src/asset.ts` exposes functions of _"getting GitHub asset URL"_ and _"downloading the asset from the URL"_. `src/install.ts` exposes a function of _"composing them and handling how the installation should be processed"_. `src/index.ts` uses the function to install the asset with some additional control.

## License

[MIT License](LICENSE). Copyright © 2019, GIL B. Chan <[[email protected]](mailto:[email protected])>