From 6af6042d63fb1f36b0a1552a2e5a9a93c84d6b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edixon=20Pi=C3=B1a?= Date: Sun, 8 Jan 2023 23:10:08 -0400 Subject: [PATCH] add library husky, dotenv and minor changes - 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 --- .env.template | 3 +++ .gitignore | 8 ++++++++ .vscode/settings.json | 3 ++- deno.jsonc | 6 ++++-- deno.lock | 12 ++++++++++++ src/@types/index.d.ts | 8 ++++++++ src/deps.ts | 2 ++ src/main.ts | 2 ++ src/scraping/jobs.ts | 13 ++----------- src/services/Scraper.service.ts | 11 ++++------- src/services/Server.service.ts | 6 ++++-- tools/prepare-husky.ts | 5 +++++ 12 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 .env.template create mode 100644 src/@types/index.d.ts create mode 100644 tools/prepare-husky.ts diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..5af7c27 --- /dev/null +++ b/.env.template @@ -0,0 +1,3 @@ +HOST= +PORT= +URL_GETONBRD= diff --git a/.gitignore b/.gitignore index e69de29..49be96b 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,8 @@ +# FOLDERS +.husky/ +.vscode/ +!.vscode/settings.json +!.vscode/extensions.json + +# FILES +.env diff --git a/.vscode/settings.json b/.vscode/settings.json index fb58d0d..d52f972 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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 } diff --git a/deno.jsonc b/deno.jsonc index c1441f8..ae6bea7 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -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": { diff --git a/deno.lock b/deno.lock index 115d771..8b87491 100644 --- a/deno.lock +++ b/deno.lock @@ -67,6 +67,9 @@ "https://deno.land/std@0.170.0/node/internal_binding/types.ts": "69000b1b92e0ca999c737f5add38827742b3ca3fe37a2389c80290de0ae6ef01", "https://deno.land/std@0.170.0/node/internal_binding/util.ts": "faf5146c3cc3b2d6c26026a818b4a16e91488ab26e63c069f36ba3c3ae24c97b", "https://deno.land/std@0.170.0/node/internal_binding/uv.ts": "27922aaec43de314afd99dfca1ce8f4d51ced9f5195e4917b54d387766404f61", + "https://deno.land/std@0.171.0/collections/filter_values.ts": "5b9feaf17b9a6e5ffccdd36cf6f38fa4ffa94cff2602d381c2ad0c2a97929652", + "https://deno.land/std@0.171.0/collections/without_all.ts": "a89f5da0b5830defed4f59666e188df411d8fece35a5f6ca69be6ca71a95c185", + "https://deno.land/std@0.171.0/dotenv/mod.ts": "6e1dbc4737e3f62a5c1f3d2a26a0e5626fb168a198375bef574fedbe86a79f20", "https://deno.land/x/oak@v11.1.0/application.ts": "0a728331822b8e27727ceec48a8499657c3163fc48fd379a8a9578a75f3cdb62", "https://deno.land/x/oak@v11.1.0/body.ts": "78402a4936accb80372c4944384658105604352d6783de7ff1e36b8832dd93c9", "https://deno.land/x/oak@v11.1.0/buf_reader.ts": "7cf96aa0ac670b75098113cf88a291a68332cc45efa8a9698f064ac5b8098a0f", @@ -155,5 +158,14 @@ "https://esm.sh/v102/nth-check@2.1.1/deno/nth-check.js": "a7aa78a04c30f4cddecb2f157b46b3b996e7589f3c817fcdcf5bcc403649c67e", "https://esm.sh/v102/parse5-htmlparser2-tree-adapter@7.0.0/deno/parse5-htmlparser2-tree-adapter.js": "4e88e69baf55e64eb7ef949f745ebaa2c8321954ea738e12dc8507d51635c2b6", "https://esm.sh/v102/parse5@7.1.2/deno/parse5.js": "32d7bfec42e23826218ea371f638487fdc5be1e9a6ba973366421aad3c2633f9" + }, + "npm": { + "specifiers": { "husky": "husky@8.0.3" }, + "packages": { + "husky@8.0.3": { + "integrity": "sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==", + "dependencies": {} + } + } } } diff --git a/src/@types/index.d.ts b/src/@types/index.d.ts new file mode 100644 index 0000000..9ecd51c --- /dev/null +++ b/src/@types/index.d.ts @@ -0,0 +1,8 @@ +type TJob = { + title: string; + role: string; + time: string; + companyName: string; + location: string; + url: string; +}; diff --git a/src/deps.ts b/src/deps.ts index dbcd0d4..386b599 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -1,3 +1,5 @@ +export * from 'npm:husky'; export * from 'https://deno.land/x/oak@v11.1.0/mod.ts'; export * from 'https://esm.sh/cheerio@1.0.0-rc.12'; +export { load as LoadEnv } from 'https://deno.land/std/dotenv/mod.ts'; // export * from 'https://deno.land/x/deno_swagger_doc@releavev2.0.1/mod.ts'; diff --git a/src/main.ts b/src/main.ts index 843c50c..c7096c4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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(); diff --git a/src/scraping/jobs.ts b/src/scraping/jobs.ts index 097ddc0..95deb1c 100644 --- a/src/scraping/jobs.ts +++ b/src/scraping/jobs.ts @@ -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 { - const scraper = new ScraperService('https://www.getonbrd.com'); + const scraper = new ScraperService(); const $ = await scraper.execute('/empleos/programacion'); const jobsResultList = $( @@ -26,7 +17,7 @@ export async function scrapeJobs(): Promise { const getDataJobs = ( jobsResultList: Cheerio, $: CheerioAPI, -): Job[] => { +): TJob[] => { const jobs = $(jobsResultList).map((_i: number, el: Element) => { let elCheerio = $(el); elCheerio = elCheerio.children('a'); diff --git a/src/services/Scraper.service.ts b/src/services/Scraper.service.ts index 325bad5..3b10ba6 100644 --- a/src/services/Scraper.service.ts +++ b/src/services/Scraper.service.ts @@ -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 { + 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 $; diff --git a/src/services/Server.service.ts b/src/services/Server.service.ts index ac1cdb7..fce62a7 100644 --- a/src/services/Server.service.ts +++ b/src/services/Server.service.ts @@ -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}`); } } diff --git a/tools/prepare-husky.ts b/tools/prepare-husky.ts new file mode 100644 index 0000000..3be2769 --- /dev/null +++ b/tools/prepare-husky.ts @@ -0,0 +1,5 @@ +import { install, set } from '../src/deps.ts'; + +install(); + +set('.husky/pre-commit', 'deno task update-lock && deno fmt && deno lint');