From 7263592046ca66146ee2b64b41ec4c8a584a5d00 Mon Sep 17 00:00:00 2001 From: Mordechai Dror Date: Wed, 24 Jul 2024 09:52:21 +0300 Subject: [PATCH] add log level, adjust workflow --- .../{rav-hen.yml => rav-hen-create.yml} | 18 ++++++++++++++---- packages/cli/package.json | 4 +++- packages/cli/src/config/globals/env.ts | 3 +++ .../cli/src/ical/utils/create-ical-calendar.ts | 10 +++++----- packages/cli/src/logging/globals/logger.ts | 7 +++++++ packages/cli/src/logging/types/log-level.ts | 10 ++++++++++ .../src/rav-hen/models/film-event.client.ts | 13 +++++-------- .../cli/src/rav-hen/scripts/create-calendar.ts | 18 +++++++++--------- .../utils/fill-calendar-with-film-events.ts | 4 ++-- 9 files changed, 58 insertions(+), 29 deletions(-) rename .github/workflows/{rav-hen.yml => rav-hen-create.yml} (68%) create mode 100644 packages/cli/src/logging/globals/logger.ts create mode 100644 packages/cli/src/logging/types/log-level.ts diff --git a/.github/workflows/rav-hen.yml b/.github/workflows/rav-hen-create.yml similarity index 68% rename from .github/workflows/rav-hen.yml rename to .github/workflows/rav-hen-create.yml index d18c54d..be4d63b 100644 --- a/.github/workflows/rav-hen.yml +++ b/.github/workflows/rav-hen-create.yml @@ -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 @@ -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 @@ -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: @@ -41,6 +51,6 @@ jobs: deploy: runs-on: ubuntu-latest needs: - - generate + - create steps: - uses: actions/deploy-pages@v4 diff --git a/packages/cli/package.json b/packages/cli/package.json index 152dae8..abe192f 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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", diff --git a/packages/cli/src/config/globals/env.ts b/packages/cli/src/config/globals/env.ts index 5043bae..46aadce 100644 --- a/packages/cli/src/config/globals/env.ts +++ b/packages/cli/src/config/globals/env.ts @@ -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; diff --git a/packages/cli/src/ical/utils/create-ical-calendar.ts b/packages/cli/src/ical/utils/create-ical-calendar.ts index 34792cb..efbfff6 100644 --- a/packages/cli/src/ical/utils/create-ical-calendar.ts +++ b/packages/cli/src/ical/utils/create-ical-calendar.ts @@ -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"; @@ -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; @@ -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; diff --git a/packages/cli/src/logging/globals/logger.ts b/packages/cli/src/logging/globals/logger.ts new file mode 100644 index 0000000..df6b24c --- /dev/null +++ b/packages/cli/src/logging/globals/logger.ts @@ -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], +}); diff --git a/packages/cli/src/logging/types/log-level.ts b/packages/cli/src/logging/types/log-level.ts new file mode 100644 index 0000000..20cd912 --- /dev/null +++ b/packages/cli/src/logging/types/log-level.ts @@ -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; diff --git a/packages/cli/src/rav-hen/models/film-event.client.ts b/packages/cli/src/rav-hen/models/film-event.client.ts index 8f677e7..658ed41 100644 --- a/packages/cli/src/rav-hen/models/film-event.client.ts +++ b/packages/cli/src/rav-hen/models/film-event.client.ts @@ -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"; @@ -22,7 +22,7 @@ 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; } @@ -30,22 +30,19 @@ export async function findFilmEvents( 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; } } diff --git a/packages/cli/src/rav-hen/scripts/create-calendar.ts b/packages/cli/src/rav-hen/scripts/create-calendar.ts index e7d432f..d4b177b 100644 --- a/packages/cli/src/rav-hen/scripts/create-calendar.ts +++ b/packages/cli/src/rav-hen/scripts/create-calendar.ts @@ -1,10 +1,10 @@ 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, @@ -12,9 +12,9 @@ import { } 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: { @@ -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"); diff --git a/packages/cli/src/rav-hen/utils/fill-calendar-with-film-events.ts b/packages/cli/src/rav-hen/utils/fill-calendar-with-film-events.ts index 3380755..cbdb1c7 100644 --- a/packages/cli/src/rav-hen/utils/fill-calendar-with-film-events.ts +++ b/packages/cli/src/rav-hen/utils/fill-calendar-with-film-events.ts @@ -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"; @@ -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;