Create your own Deno module + Github Action
Tags: deno, tutorial, githubaction, denomodule
Published: 2020-05-31 11:22:35

In my last article, I mentioned my first hands-on experience on `Deno`. Along with that, I have demonstrated how to create REST API by using `Deno` with `oak`.

If you have missed it, here is the link to that article.

{% link https://dev.to/elanandkumar/deno-is-here-have-you-explored-it-yet-3alo %}

This time, I was trying to explore creating own module in `Deno`. So, I'll try to explain how we can do it here and will provide the source code too.

Believe me, it is quite simple and there is no complexity in using it too.

Let's get started.

As we all know that we can import any module from URL directly in Deno, so let's first create one file which we can run with Deno.

As a best practice, we should name our main module as `mod.ts`.
> mod.ts

```typescript
function getHelloMsg(name: string): string {
  return `Hello ${name}`;
}
console.log(getHelloMsg('Deno'));
```
If you run the above file with Deno using the below command:
```bash
deno run mod.ts
```
and the terminal will show the following output
```bash
Hello Deno
```
Now to convert it to a module that others can use, let's slightly modify the `mod.ts`. The updated code snippet is given below:
```typescript
export function getHelloMsg(name: string): string {
  return `Hello ${name}`;
}
```
I have added the `export` keyword on line 1 before the function so that we can import it.

Now, let's create one file where we can test it.
> index.ts
```typescript
import { getHelloMsg } from './mod.ts';

const message = getHelloMsg('deno');
console.log(message);
```
Now, run it using the following command.
```bash
deno run index.ts
```
And the output will be similar to what we have seen before.
```bash
Hello Deno
```
But isn't it something that we do anyway? Yes, we do.

But when we push the `mod.ts` file in our GitHub, we can use it like below:
```typescript
import { getHelloMsg } from "https://raw.githubusercontent.com/YOUR_GIT/REPO_NAME/master/mod.ts";

const message = getHelloMsg('Deno');
console.log(message);
```
> Please note the first line which is highlighted above.

As you can see we are importing the module directly from the URL which is kind of cool.

We do not need any registry or any setup to publish it so that others can use it.
> The main highlight here is that now, we can put this module on GitHub and we can use it from the URL.

Next, let's set up our action file for GitHub. Follow the steps given below:
- Create a directory `.github`
- Create another directory inside `.github` named `workflows`
- Inside `workflows` create a file called `deno.yml`

Here is the sample file
> .github/workflows/deno.yml
```yml
on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        deno: [1.0.0]
    name: Test run with Deno ${{ matrix.deno }}
    steps:
      - uses: actions/checkout@master

      - name: Setup Deno module
      uses: denolib/setup-deno@master
      with:
        deno-version: ${{matrix.deno}}

      - name: Print deno version
      run: deno --version

      - name: Check format
      run: deno fmt --check

      - name: Run tests
      run: deno test
```
A couple of things are happening here but the important one is to note are the following:

- `on`: This tells when the action to be triggered. In our case, it will be triggered on `push` and `pull_request`
- `deno fmt`: This one ensures that the files are formatted correctly which means badly formatted code will fail the action
- `deno test`: This triggers the Unit tests

As you can see, these steps can ensure that module is in healthy condition till the action run on GitHub is green.

This way, whenever we will push any changes/updates to our module, the actions will trigger the build(_according to the action setup_).

A complete example with usage and action is available on my GitHub URL. Feel free to use it to create/scaffold your module for deno.

[Deno module starter kit](https://github.com/elanandkumar/deno-module-starter-kit)

Please follow the `README.md` on how to use it.

In a future article, I'll come up with how to publish your module to `deno.land`.

Till then.....

Happy Learning!