diff --git a/webapp/src/app/[lang]/page.tsx b/webapp/src/app/[lang]/page.tsx index 0790a72f..dcd310e9 100644 --- a/webapp/src/app/[lang]/page.tsx +++ b/webapp/src/app/[lang]/page.tsx @@ -1,9 +1,8 @@ 'use client'; -import MessageForm from '@/components/MessageForm'; import { MessageData } from '@/utils/readTypedMessages'; +import MessageForm from '@/components/MessageForm'; import { Box, Button, Link, Typography } from '@mui/joy'; -import { useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; export default function Home({ params }: { params: { lang: string } }) { @@ -57,17 +56,17 @@ export default function Home({ params }: { params: { lang: string } }) { const res = await fetch( `/api/translations/${params.lang}/${msg.id}`, { - method: 'PUT', - headers: { - 'Content-Type': 'application/json', - }, body: JSON.stringify({ text, }), + headers: { + 'Content-Type': 'application/json', + }, + method: 'PUT', }, ); - const payload = await res.json(); + await res.json(); setTranslations((cur) => ({ ...cur, [msg.id]: text, diff --git a/webapp/src/app/api/languages.ts b/webapp/src/app/api/languages.ts index 197b3614..cf49905a 100644 --- a/webapp/src/app/api/languages.ts +++ b/webapp/src/app/api/languages.ts @@ -1,6 +1,7 @@ -import { envVarNotFound } from '@/utils/util'; +/* global globalThis */ import fs from 'fs/promises'; import { parse } from 'yaml'; +import { envVarNotFound, logDebug } from '@/utils/util'; import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git'; const REPO_PATH = process.env.REPO_PATH ?? envVarNotFound('REPO_PATH'); @@ -9,7 +10,7 @@ const MAIN_BRANCH = 'main'; export async function getLanguage(lang: string) { let languages: Map>; if (!globalThis.languages) { - console.debug('Initializing languages'); + logDebug('Initializing languages'); const options: Partial = { baseDir: REPO_PATH, binary: 'git', @@ -17,28 +18,28 @@ export async function getLanguage(lang: string) { trimmed: false, }; const git: SimpleGit = simpleGit(options); - console.debug('git checkout main pull...'); + logDebug('git checkout main pull...'); await git.checkout(MAIN_BRANCH); - console.debug('git pull...'); + logDebug('git pull...'); await git.pull(); - console.debug('git done checkout main branch and pull'); + logDebug('git done checkout main branch and pull'); languages = new Map>(); globalThis.languages = languages; } else { - console.debug('read languages from globalThis'); + logDebug('read languages from globalThis'); languages = globalThis.languages; } let translations: Record; if (!languages.has(lang)) { - console.debug('read languages from file'); + logDebug('read languages from file'); const yamlPath = REPO_PATH + `/src/locale/${lang}.yml`; const yamlBuf = await fs.readFile(yamlPath); translations = parse(yamlBuf.toString()) as Record; languages.set(lang, translations); } else { - console.debug('read languages from Memory'); + logDebug('read languages from Memory'); translations = languages.get(lang) ?? throwLangNotFound(lang); } diff --git a/webapp/src/app/api/messages/route.ts b/webapp/src/app/api/messages/route.ts index 855cc1b2..ff93e4b2 100644 --- a/webapp/src/app/api/messages/route.ts +++ b/webapp/src/app/api/messages/route.ts @@ -1,7 +1,7 @@ import { envVarNotFound } from '@/utils/util'; import fs from 'fs/promises'; -import path from 'path'; import { NextResponse } from 'next/server'; +import path from 'path'; import readTypedMessages, { MessageData, } from '@/utils/readTypedMessages'; diff --git a/webapp/src/app/api/pull-request/route.ts b/webapp/src/app/api/pull-request/route.ts index bff2d17b..5ae236e0 100644 --- a/webapp/src/app/api/pull-request/route.ts +++ b/webapp/src/app/api/pull-request/route.ts @@ -1,10 +1,11 @@ -import { envVarNotFound } from '@/utils/util'; -import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git'; +/* global globalThis */ +import fs from 'fs/promises'; import { NextResponse } from 'next/server'; import { Octokit } from '@octokit/rest'; -import fs from 'fs/promises'; import { stringify } from 'yaml'; import { version } from '@/../package.json'; +import { envVarNotFound, logError, logWarn } from '@/utils/util'; +import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git'; const REPO_PATH = process.env.REPO_PATH ?? envVarNotFound('REPO_PATH'); const GITHUB_AUTH = process.env.GITHUB_AUTH ?? envVarNotFound('GITHUB_AUTH'); @@ -39,8 +40,8 @@ export async function POST() { for (const lang of languages.keys()) { const yamlPath = REPO_PATH + `/src/locale/${lang}.yml`; const yamlOutput = stringify(languages.get(lang), { - singleQuote: true, doubleQuotedAsJSON: true, + singleQuote: true, }); await fs.writeFile(yamlPath, yamlOutput); } @@ -65,7 +66,7 @@ export async function POST() { pullRequestUrl, }); } catch (e) { - console.log(e); + logError(e); throw e; } finally { syncLock = false; @@ -74,29 +75,29 @@ export async function POST() { async function createPR(branchName: string, nowIso: string): Promise { const octokit = new Octokit({ auth: GITHUB_AUTH, - userAgent: 'Lyra v' + version, - timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, baseUrl: 'https://api.github.com', log: { debug: () => {}, + error: logError, info: () => {}, - warn: console.warn, - error: console.error, + warn: logWarn, }, request: { agent: undefined, fetch: undefined, timeout: 0, }, + timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, + userAgent: 'Lyra v' + version, }); const response = await octokit.rest.pulls.create({ + base: MAIN_BRANCH, + body: 'Created by LYRA at: ' + nowIso, + head: branchName, owner: GITHUB_OWNER, repo: GITHUB_REPO, title: 'LYRA Translate PR: ' + nowIso, - body: 'Created by LYRA at: ' + nowIso, - head: branchName, - base: MAIN_BRANCH, }); return response.data.html_url; diff --git a/webapp/src/app/api/translations/[lang]/[msgId]/route.ts b/webapp/src/app/api/translations/[lang]/[msgId]/route.ts index ea5f928b..0505d1da 100644 --- a/webapp/src/app/api/translations/[lang]/[msgId]/route.ts +++ b/webapp/src/app/api/translations/[lang]/[msgId]/route.ts @@ -1,5 +1,5 @@ -import { NextRequest, NextResponse } from 'next/server'; import { getLanguage } from '@/app/api/languages'; +import { NextRequest, NextResponse } from 'next/server'; export async function PUT( req: NextRequest, diff --git a/webapp/src/app/api/translations/[lang]/route.ts b/webapp/src/app/api/translations/[lang]/route.ts index 385c6a0b..c572f66e 100644 --- a/webapp/src/app/api/translations/[lang]/route.ts +++ b/webapp/src/app/api/translations/[lang]/route.ts @@ -1,5 +1,5 @@ -import { NextRequest, NextResponse } from 'next/server'; import { getLanguage } from '@/app/api/languages'; +import { NextRequest, NextResponse } from 'next/server'; export async function GET( req: NextRequest, // keep this here even if unused @@ -22,6 +22,7 @@ function flattenObject( const result: Record = {}; for (const key in obj) { + // eslint-disable-next-line no-prototype-builtins if (obj.hasOwnProperty(key)) { const newKey = parentKey ? `${parentKey}.${key}` : key; diff --git a/webapp/src/app/layout.tsx b/webapp/src/app/layout.tsx index 9c545160..5e10eae6 100644 --- a/webapp/src/app/layout.tsx +++ b/webapp/src/app/layout.tsx @@ -1,5 +1,6 @@ -import type { Metadata } from 'next'; import { Inter } from 'next/font/google'; +import type { Metadata } from 'next'; +import React from 'react'; const inter = Inter({ subsets: ['latin'] }); diff --git a/webapp/src/components/MessageForm.tsx b/webapp/src/components/MessageForm.tsx index 76353372..d5acb913 100644 --- a/webapp/src/components/MessageForm.tsx +++ b/webapp/src/components/MessageForm.tsx @@ -12,6 +12,7 @@ import { FC, useEffect, useState } from 'react'; type Props = { message: MessageData; + // eslint-disable-next-line no-unused-vars onSave: (text: string) => void; translation: string; }; diff --git a/webapp/src/types.ts b/webapp/src/types.ts index 0e33593f..193638ca 100644 --- a/webapp/src/types.ts +++ b/webapp/src/types.ts @@ -1,5 +1,6 @@ export type LanguageMap = Map>; declare global { + // eslint-disable-next-line no-unused-vars var languages: LanguageMap; } diff --git a/webapp/src/utils/util.ts b/webapp/src/utils/util.ts index 58289683..abb6b82d 100644 --- a/webapp/src/utils/util.ts +++ b/webapp/src/utils/util.ts @@ -1,3 +1,23 @@ 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); +} + +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) +}