From c010d936f2591439612efe31c312f03f818518ae Mon Sep 17 00:00:00 2001 From: Amer Harb Date: Fri, 15 Dec 2023 20:35:02 +0100 Subject: [PATCH] refactor import and logging --- webapp/jest.config.js | 3 +++ webapp/src/Store.ts | 17 +++++++++-------- webapp/src/app/api/pull-request/route.ts | 9 +++++---- webapp/src/utils/config.ts | 4 ++-- webapp/src/utils/log.ts | 20 ++++++++++++++++++++ webapp/src/utils/util.ts | 21 --------------------- 6 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 webapp/src/utils/log.ts diff --git a/webapp/jest.config.js b/webapp/jest.config.js index 3745fc22..de7db418 100644 --- a/webapp/jest.config.js +++ b/webapp/jest.config.js @@ -2,4 +2,7 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', + moduleNameMapper: { + '@/(.*)': '/src/$1', + } }; diff --git a/webapp/src/Store.ts b/webapp/src/Store.ts index a9ff5a7f..6a0b4eed 100644 --- a/webapp/src/Store.ts +++ b/webapp/src/Store.ts @@ -1,9 +1,10 @@ /* global globalThis */ +import { debug } from '@/utils/log'; +import { envVarNotFound } from '@/utils/util'; import { LanguageNotFound } from '@/errors'; import LyraConfig from './utils/config'; import YAMLTranslationAdapter from './utils/adapters/YAMLTranslationAdapter'; -import { envVarNotFound, logDebug } from '@/utils/util'; import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git'; const REPO_PATH = process.env.REPO_PATH ?? envVarNotFound('REPO_PATH'); @@ -13,7 +14,7 @@ export class Store { public static async getLanguage(lang: string) { let languages: Map>; if (!globalThis.languages) { - logDebug('Initializing languages'); + debug('Initializing languages'); const options: Partial = { baseDir: REPO_PATH, binary: 'git', @@ -21,22 +22,22 @@ export class Store { trimmed: false, }; const git: SimpleGit = simpleGit(options); - logDebug('git checkout main pull...'); + debug('git checkout main pull...'); await git.checkout(MAIN_BRANCH); - logDebug('git pull...'); + debug('git pull...'); await git.pull(); - logDebug('git done checkout main branch and pull'); + debug('git done checkout main branch and pull'); languages = new Map>(); globalThis.languages = languages; } else { - logDebug('find languages in Memory'); + debug('find languages in Memory'); languages = globalThis.languages; } let translation: Record = {}; if (!languages.has(lang)) { - logDebug('read language[' + lang + '] from file'); + debug('read language[' + lang + '] from file'); const config = await LyraConfig.readFromDir(REPO_PATH); // TODO: make it multi projects const adapter = new YAMLTranslationAdapter(config.projects[0].translationsPath); @@ -48,7 +49,7 @@ export class Store { languages.set(lang, translation); } else { - logDebug('read language [' + lang + '] from Memory'); + debug('read language [' + lang + '] from Memory'); translation = languages.get(lang) ?? Store.throwLangNotFound(lang); } diff --git a/webapp/src/app/api/pull-request/route.ts b/webapp/src/app/api/pull-request/route.ts index 27ae53ad..aacdf080 100644 --- a/webapp/src/app/api/pull-request/route.ts +++ b/webapp/src/app/api/pull-request/route.ts @@ -1,12 +1,13 @@ /* global globalThis */ +import { envVarNotFound } from '@/utils/util'; import fs from 'fs/promises'; import { NextResponse } from 'next/server'; import { Octokit } from '@octokit/rest'; import packageJson from '@/../package.json'; import { stringify } from 'yaml'; import { unflatten } from 'flat'; -import { envVarNotFound, logError, logWarn } from '@/utils/util'; +import { err, warn } from '@/utils/log'; import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git'; const REPO_PATH = process.env.REPO_PATH ?? envVarNotFound('REPO_PATH'); @@ -67,7 +68,7 @@ export async function POST() { pullRequestUrl, }); } catch (e) { - logError(e); + err(e); throw e; } finally { syncLock = false; @@ -79,9 +80,9 @@ export async function POST() { baseUrl: 'https://api.github.com', log: { debug: () => {}, - error: logError, + error: err, info: () => {}, - warn: logWarn, + warn: warn, }, request: { agent: undefined, diff --git a/webapp/src/utils/config.ts b/webapp/src/utils/config.ts index 24e15385..15dea912 100644 --- a/webapp/src/utils/config.ts +++ b/webapp/src/utils/config.ts @@ -1,5 +1,5 @@ +import { err } from '@/utils/log'; import fs from 'fs/promises'; -import { logError } from '@/utils/util'; import { LyraConfigReadingError } from '@/errors'; import { parse } from 'yaml'; import path from 'path'; @@ -51,7 +51,7 @@ export default class LyraConfig { }) ); } catch (e) { - logError(`error reading ${filename} file`); + err(`error reading ${filename} file`); throw new LyraConfigReadingError(filename); } } diff --git a/webapp/src/utils/log.ts b/webapp/src/utils/log.ts new file mode 100644 index 00000000..3e4218d9 --- /dev/null +++ b/webapp/src/utils/log.ts @@ -0,0 +1,20 @@ +export function debug(msg: string) { + // eslint-disable-next-line no-console + console.debug(msg); +} + +export function info(msg: string) { + // eslint-disable-next-line no-console + console.info(msg); +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function err(msg: any) { + // eslint-disable-next-line no-console + console.error(msg) +} + +export function warn(msg: string) { + // eslint-disable-next-line no-console + console.warn(msg) +} diff --git a/webapp/src/utils/util.ts b/webapp/src/utils/util.ts index 2b427bac..58289683 100644 --- a/webapp/src/utils/util.ts +++ b/webapp/src/utils/util.ts @@ -1,24 +1,3 @@ export function envVarNotFound(varName: string): never { throw new Error(`${varName} variable not defined`); } - -export function logDebug(msg: string) { - // eslint-disable-next-line no-console - console.debug(msg); -} - -export function logInfo(msg: string) { - // eslint-disable-next-line no-console - console.info(msg); -} - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function logError(msg: any) { - // eslint-disable-next-line no-console - console.error(msg) -} - -export function logWarn(msg: string) { - // eslint-disable-next-line no-console - console.warn(msg) -}