Skip to content

Commit

Permalink
fix: cli args & clean
Browse files Browse the repository at this point in the history
  • Loading branch information
0nza1101 committed May 13, 2022
1 parent 7cd0a60 commit 7ae9123
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
14 changes: 6 additions & 8 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { Options, wakeDyno } from "./mod.ts";

if (import.meta.main) {
console.log('Args', parse(Deno.args));
const args = parse(Deno.args);

if (args._.length > 0) {
if (Deno.args.length > 0) {
const args = parse(Deno.args);
const options: Options = {
interval: args.interval ?? 29,
...(args.stopStart && args.stopEnd) && {
...(args.stopStart || args.stopEnd) && {
stopTimes: {
start: args.start,
end: args.end
start: args.stopStart,
end: args.stopEnd
}
}
}
Expand All @@ -26,6 +24,6 @@ if (import.meta.main) {
wakeDyno(urls, options);
}
} else {
console.log('Usage:\n$ heroku-awakener --url <url> --interval <interval> --stopStart <start> --stopEnd <end>')
console.log('Usage:\n$ heroku-awakener --url <url> --interval <interval> --stopStart <start> --stopEnd <end>\n$ heroku-awakener --urls <urls> --interval <interval> --stopStart <start> --stopEnd <end>')
}
}
18 changes: 7 additions & 11 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import * as logger from "https://deno.land/[email protected]/log/mod.ts";
import dayjs from "https://cdn.skypack.dev/[email protected]";
import isBetween from "https://cdn.skypack.dev/[email protected]/plugin/isBetween.js";
import customParseFormat from "https://cdn.skypack.dev/[email protected]/plugin/customParseFormat.js";

dayjs.extend(customParseFormat);
dayjs.extend(isBetween);

export function mode() {
return 0;
}

interface StopTimes {
start: string;
end: string;
Expand All @@ -23,9 +20,8 @@ function isStopTime(stopTimes: StopTimes): boolean {
const { start, end } = stopTimes;
const format = 'HH:mm';

// Check if a start and end time are supplied.
if (!start || !end) {
console.log('WARNING: Both a start/end stop time must be defined.');
logger.warning('Both a start/end stop time must be defined.');
return true;
}

Expand All @@ -50,8 +46,8 @@ export function wakeDyno(url: string, options: Options = {}) {
wakeDyno(url, options);
} else {
fetch(url)
.then(() => logging && console.log('Successfully woke the dyno'))
.catch(() => logging && console.log('Error attempting to wake the dyno'))
.then(() => logging && logger.info('Successfully woke the dyno'))
.catch((error: Error) => logging && logger.error(`Attempting to wake the dyno : ${error.message}`))
.finally(() => wakeDyno(url, options));
}
}, milliseconds);
Expand All @@ -68,12 +64,12 @@ export function wakeDynos(urls: string[], options: Options) {

setTimeout(() => {
if (stopTimes && isStopTime(stopTimes)) {
wakeDynos(urls, options); // Recursively call function until not a stop time.
wakeDynos(urls, options);
} else {
const promises = urls.map((url) => fetch(url));
Promise.all(promises)
.then(() => logging && console.log('Successfully woke all dynos'))
.catch(() => logging && console.log('Error attempting to wake the dynos'))
.then(() => logging && logger.info('Successfully woke all dynos'))
.catch((error: Error) => logging && logger.error(`Attempting to wake the dyno : ${error.message}`))
.finally(() => wakeDynos(urls, options));
}
}, milliseconds);
Expand Down

0 comments on commit 7ae9123

Please sign in to comment.