Skip to content

Commit

Permalink
add log level, adjust workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
vorant94 committed Jul 24, 2024
1 parent 5c689e6 commit 7263592
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
name: Generate Calendar for Rav-Hen
name: Create Calendar for Rav-Hen

on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
type: choice
default: info
options:
- info
- debug
branchName:
description: 'Rav Hen branch to search cinemas for'
required: true
Expand All @@ -21,9 +28,12 @@ permissions:
pages: write
id-token: write

env:
NODE_ENV: production
LOG_LEVEL: ${{ inputs.logLevel }}

jobs:
generate:
create:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -32,7 +42,7 @@ jobs:

- run: npm run build --workspace @sofash/cli

- run: node packages/cli/dist/rav-hen/scripts/create-calendar.js -b ${{ inputs.branchName }} -d ${{ inputs.date }}
- run: npm run rav-hen:create --workspace @sofash/cli -- -b ${{ inputs.branchName }} -d ${{ inputs.date }}

- uses: actions/upload-pages-artifact@v3
with:
Expand All @@ -41,6 +51,6 @@ jobs:
deploy:
runs-on: ubuntu-latest
needs:
- generate
- create
steps:
- uses: actions/deploy-pages@v4
4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"clean": "shx rm -rf dist"
"clean": "shx rm -rf dist",
"rav-hen:create:dev": "node --env-file=.env dist/rav-hen/scripts/create-calendar.js",
"rav-hen:create": "node dist/rav-hen/scripts/create-calendar.js"
},
"dependencies": {
"consola": "^3.2.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/config/globals/env.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { z } from "zod";
import { logLevelSchema } from "../../logging/types/log-level.js";

const envSchema = z.object({
// biome-ignore lint/style/useNamingConvention: env variables have different convention
NODE_ENV: z.enum(["development", "production"]).default("development"),
// biome-ignore lint/style/useNamingConvention: env variables have different convention
LOG_LEVEL: logLevelSchema,
});
export type Env = z.infer<typeof envSchema>;

Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/ical/utils/create-ical-calendar.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from "node:fs/promises";
import consola from "consola";
import ical, {
type ICalCalendar,
type ICalCalendarJSONData,
} from "ical-generator";
import { env } from "../../config/globals/env.js";
import { logger } from "../../logging/globals/logger.js";
import { getPathsOfDataFiles } from "./get-paths-of-data-files.js";
import { getUrlsOfDataFiles } from "./get-urls-of-data-files.js";

Expand Down Expand Up @@ -34,11 +34,11 @@ async function getExistingICalCalendarFromLocalFilesystem(

try {
data = JSON.parse(await fs.readFile(jsonPath, { encoding: "utf-8" }));
consola.debug(
logger.debug(
"Found previous events file locally, will add new events to it",
);
} catch (_e) {
consola.debug("No previous events file found, will create a new one");
logger.debug("No previous events file found, will create a new one");
}

return data;
Expand All @@ -56,11 +56,11 @@ async function getExistingICalCalendarFromRemoteHost(
try {
const response = await fetch(jsonUrl);
data = await response.json();
consola.debug(
logger.debug(
"Found previous events file remotely, will add new events to it",
);
} catch (_e) {
consola.debug("No previous events file found, will create a new one");
logger.debug("No previous events file found, will create a new one");
}

return data;
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/logging/globals/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createConsola } from "consola";
import { env } from "../../config/globals/env.js";
import { logLevelToConsolaLevel } from "../types/log-level.js";

export const logger = createConsola({
level: logLevelToConsolaLevel[env.LOG_LEVEL],
});
10 changes: 10 additions & 0 deletions packages/cli/src/logging/types/log-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from "zod";

export const logLevels = ["info", "debug"] as const;
export type LogLevel = (typeof logLevels)[number];
export const logLevelSchema = z.enum(logLevels).default("info");

export const logLevelToConsolaLevel = {
info: 3,
debug: 4,
} as const satisfies Record<LogLevel, number>;
13 changes: 5 additions & 8 deletions packages/cli/src/rav-hen/models/film-event.client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { URL } from "node:url";
import consola from "consola";
import { format } from "date-fns";
import { z } from "zod";
import { logger } from "../../logging/globals/logger.js";
import { config } from "../globals/config.js";
import type { BranchId } from "../types/branch-id.js";
import { filmEventSchema } from "./film-event.model.js";
Expand All @@ -22,30 +22,27 @@ export async function findFilmEvents(
try {
response = await fetch(url);
} catch (e) {
consola.error("Failed to fetch", url.toString());
logger.error("Failed to fetch", url.toString());
throw e;
}

let body: unknown;
try {
body = await response.json();
} catch (e) {
consola.error("Failed to parse response body from", url.toString());
logger.error("Failed to parse response body from", url.toString());
throw e;
}

try {
const parsed = findFilmEventsResponseBodySchema.parse(body);
consola.debug(
logger.debug(
`Fetched ${parsed.body.films.length} films and ${parsed.body.events.length} events`,
);

return parsed;
} catch (e) {
consola.error(
"Response body didn't pass schema validation",
url.toString(),
);
logger.error("Response body didn't pass schema validation", url.toString());
throw e;
}
}
Expand Down
18 changes: 9 additions & 9 deletions packages/cli/src/rav-hen/scripts/create-calendar.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import fs from "node:fs/promises";
import { parseArgs } from "node:util";
import consola from "consola";
import { differenceInDays } from "date-fns";
import { z } from "zod";
import { createICalCalendar } from "../../ical/utils/create-ical-calendar.js";
import { getPathsOfDataFiles } from "../../ical/utils/get-paths-of-data-files.js";
import { logger } from "../../logging/globals/logger.js";
import { findFilmEvents } from "../models/film-event.client.js";
import {
branchNameSchema,
branchNameToBranchId,
} from "../types/branch-name.js";
import { fillCalendarWithFilmEvents } from "../utils/fill-calendar-with-film-events.js";

consola.start("Creating Rav-Hen calendar");
logger.start("Creating Rav-Hen calendar");

consola.info("Parsing CLI args");
logger.info("Parsing CLI args");
const args = parseArgs({
options: {
branchName: {
Expand All @@ -41,22 +41,22 @@ const argsSchema = z.object({
});
const { branchName, date } = argsSchema.parse(args.values);

consola.info("Ensuring data dir is in place");
logger.info("Ensuring data dir is in place");
const [icsPath, jsonPath] = await getPathsOfDataFiles("rav-hen", branchName);

consola.info("Creating calendar");
logger.info("Creating calendar");
const calendar = await createICalCalendar("rav-hen", branchName);

consola.info("Fetching film events...");
logger.info("Fetching film events...");
const data = await findFilmEvents(branchNameToBranchId[branchName], date);

consola.info("Filling calendar with fetched events...");
logger.info("Filling calendar with fetched events...");
fillCalendarWithFilmEvents(calendar, data);

consola.info("Saving calendar files...");
logger.info("Saving calendar files...");
await Promise.all([
fs.writeFile(icsPath, calendar.toString()),
fs.writeFile(jsonPath, JSON.stringify(calendar)),
]);

consola.success("Successfully created Ran-Hen calendar");
logger.success("Successfully created Ran-Hen calendar");
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import consola from "consola";
import { addMinutes } from "date-fns";
import type { ICalCalendar } from "ical-generator";
import { logger } from "../../logging/globals/logger.js";
import type { FindFilmEventsResponseBody } from "../models/film-event.client.js";
import type { Film } from "../models/film.model.js";

Expand All @@ -15,7 +15,7 @@ export function fillCalendarWithFilmEvents(

for (const event of events) {
if (existingEvents.has(event.id)) {
consola.debug(
logger.debug(
`Skipping event with id ${event.id} since it is already in calendar`,
);
continue;
Expand Down

0 comments on commit 7263592

Please sign in to comment.