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.
Deno is here. Todo App(React) + Deno Server (API) + Docker.
Anand Kumar ・ May 17 '20
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
function getHelloMsg(name: string): string {
return `Hello ${name}`;
}
console.log(getHelloMsg('Deno'));
If you run the above file with Deno using the below command:
deno run mod.ts
and the terminal will show the following output
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:
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
import { getHelloMsg } from './mod.ts';
const message = getHelloMsg('deno');
console.log(message);
Now, run it using the following command.
deno run index.ts
And the output will be similar to what we have seen before.
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:
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
namedworkflows
- Inside
workflows
create a file calleddeno.yml
Here is the sample file
.github/workflows/deno.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 onpush
andpull_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.
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!
Top comments (0)