Skip to content

Commit

Permalink
add library husky, dotenv and minor changes
Browse files Browse the repository at this point in the history
- add new task in deno.jsonc
- add new dependencies in deps.ts
- use environment in services Scraper and Server
- add new script in /tools
- create types declarations in /@types
- add new files: .gitignore, .env.template
  • Loading branch information
EdixonAlberto committed Jan 9, 2023
1 parent f96eeab commit 6af6042
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HOST=
PORT=
URL_GETONBRD=
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# FOLDERS
.husky/
.vscode/
!.vscode/settings.json
!.vscode/extensions.json

# FILES
.env
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"deno.enable": true,
"deno.unstable": false,
"deno.config": "./deno.jsonc",
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"deno.config": "./deno.jsonc",
"thunder-client.saveToWorkspace": true,
"discord.enabled": true
}
6 changes: 4 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"tasks": {
"dev": "deno run --watch --allow-net src/main.ts",
"scrape": "deno run --allow-net --allow-write ./src/scraping/jobs.ts",
"update-lock": "deno cache --lock=deno.lock --lock-write src/deps.ts"
"update-lock": "deno cache --lock=deno.lock --lock-write src/deps.ts",
"prepare-husky": "deno run --allow-env --allow-read --allow-write --allow-run ./tools/prepare-husky.ts"
},
"fmt": {
"files": {
"include": [
"src/"
"src/",
"test/"
]
},
"options": {
Expand Down
12 changes: 12 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type TJob = {
title: string;
role: string;
time: string;
companyName: string;
location: string;
url: string;
};
2 changes: 2 additions & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from 'npm:husky';
export * from 'https://deno.land/x/[email protected]/mod.ts';
export * from 'https://esm.sh/[email protected]';
export { load as LoadEnv } from 'https://deno.land/std/dotenv/mod.ts';
// export * from 'https://deno.land/x/[email protected]/mod.ts';
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Server } from './services/Server.service.ts';
import { LoadEnv } from './deps.ts';

try {
await LoadEnv();
const server = new Server();

server.run();
Expand Down
13 changes: 2 additions & 11 deletions src/scraping/jobs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { ScraperService } from '../services/Scraper.service.ts';
import { Cheerio, CheerioAPI, Element } from '../deps.ts';

interface Job {
title: string;
role: string;
time: string;
companyName: string;
location: string;
url: string;
}

export async function scrapeJobs(): Promise<void> {
const scraper = new ScraperService('https://www.getonbrd.com');
const scraper = new ScraperService();
const $ = await scraper.execute('/empleos/programacion');

const jobsResultList = $(
Expand All @@ -26,7 +17,7 @@ export async function scrapeJobs(): Promise<void> {
const getDataJobs = (
jobsResultList: Cheerio<Element>,
$: CheerioAPI,
): Job[] => {
): TJob[] => {
const jobs = $(jobsResultList).map((_i: number, el: Element) => {
let elCheerio = $(el);
elCheerio = elCheerio.children('a');
Expand Down
11 changes: 4 additions & 7 deletions src/services/Scraper.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { CheerioAPI, load } from '../deps.ts';

export class ScraperService {
private baseUrl: string;

constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}

public async execute(path: string): Promise<CheerioAPI> {
const baseUrl: string | undefined = Deno.env.get('URL_GETONBRD');
if (!baseUrl) throw new Error('Evironment "URL_GETONBRD" not found');

const pathname: string = path.startsWith('/') ? path : `/${path}`;
const response = await fetch(this.baseUrl + pathname);
const response = await fetch(baseUrl + pathname);
const html: string = await response.text();
const $ = load(html);
return $;
Expand Down
6 changes: 4 additions & 2 deletions src/services/Server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export class Server {
}

public run(): void {
const port = 8000;
this.app.listen({ hostname: '0.0.0.0', port });
const hostname: string = Deno.env.get('HOST') || 'localhost';
const port: number = Number(Deno.env.get('PORT')) || 8000;

this.app.listen({ hostname, port });
console.log(`Sever listening on port ${port}`);
}
}
5 changes: 5 additions & 0 deletions tools/prepare-husky.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { install, set } from '../src/deps.ts';

install();

set('.husky/pre-commit', 'deno task update-lock && deno fmt && deno lint');

0 comments on commit 6af6042

Please sign in to comment.