Skip to content

Commit

Permalink
refactor import and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
amerharb committed Dec 15, 2023
1 parent 6f2b7b8 commit c010d93
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
3 changes: 3 additions & 0 deletions webapp/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'@/(.*)': '<rootDir>/src/$1',
}
};
17 changes: 9 additions & 8 deletions webapp/src/Store.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -13,30 +14,30 @@ export class Store {
public static async getLanguage(lang: string) {
let languages: Map<string, Record<string, string>>;
if (!globalThis.languages) {
logDebug('Initializing languages');
debug('Initializing languages');
const options: Partial<SimpleGitOptions> = {
baseDir: REPO_PATH,
binary: 'git',
maxConcurrentProcesses: 1,
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<string, Record<string, string>>();
globalThis.languages = languages;
} else {
logDebug('find languages in Memory');
debug('find languages in Memory');
languages = globalThis.languages;
}

let translation: Record<string, string> = {};

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);
Expand All @@ -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);
}

Expand Down
9 changes: 5 additions & 4 deletions webapp/src/app/api/pull-request/route.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -67,7 +68,7 @@ export async function POST() {
pullRequestUrl,
});
} catch (e) {
logError(e);
err(e);
throw e;
} finally {
syncLock = false;
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class LyraConfig {
})
);
} catch (e) {
logError(`error reading ${filename} file`);
err(`error reading ${filename} file`);
throw new LyraConfigReadingError(filename);
}
}
Expand Down
20 changes: 20 additions & 0 deletions webapp/src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 0 additions & 21 deletions webapp/src/utils/util.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c010d93

Please sign in to comment.