Skip to content

Commit

Permalink
eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
amerharb committed Nov 27, 2023
1 parent d32cb1c commit 4b88185
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 31 deletions.
13 changes: 6 additions & 7 deletions webapp/src/app/[lang]/page.tsx
Original file line number Diff line number Diff line change
@@ -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 } }) {
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions webapp/src/app/api/languages.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -9,36 +10,36 @@ const MAIN_BRANCH = 'main';
export async function getLanguage(lang: string) {
let languages: Map<string, Record<string, unknown>>;
if (!globalThis.languages) {
console.debug('Initializing languages');
logDebug('Initializing languages');
const options: Partial<SimpleGitOptions> = {
baseDir: REPO_PATH,
binary: 'git',
maxConcurrentProcesses: 1,
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<string, Record<string, unknown>>();
globalThis.languages = languages;
} else {
console.debug('read languages from globalThis');
logDebug('read languages from globalThis');
languages = globalThis.languages;
}

let translations: Record<string, unknown>;
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<string, unknown>;
languages.set(lang, translations);
} else {
console.debug('read languages from Memory');
logDebug('read languages from Memory');
translations = languages.get(lang) ?? throwLangNotFound(lang);
}

Expand Down
2 changes: 1 addition & 1 deletion webapp/src/app/api/messages/route.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
25 changes: 13 additions & 12 deletions webapp/src/app/api/pull-request/route.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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);
}
Expand All @@ -65,7 +66,7 @@ export async function POST() {
pullRequestUrl,
});
} catch (e) {
console.log(e);
logError(e);
throw e;
} finally {
syncLock = false;
Expand All @@ -74,29 +75,29 @@ export async function POST() {
async function createPR(branchName: string, nowIso: string): Promise<string> {
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;
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/app/api/translations/[lang]/[msgId]/route.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/app/api/translations/[lang]/route.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,6 +22,7 @@ function flattenObject(
const result: Record<string, any> = {};

for (const key in obj) {
// eslint-disable-next-line no-prototype-builtins
if (obj.hasOwnProperty(key)) {
const newKey = parentKey ? `${parentKey}.${key}` : key;

Expand Down
3 changes: 2 additions & 1 deletion webapp/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -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'] });

Expand Down
1 change: 1 addition & 0 deletions webapp/src/components/MessageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
1 change: 1 addition & 0 deletions webapp/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type LanguageMap = Map<string, Record<string, unknown>>;

declare global {
// eslint-disable-next-line no-unused-vars
var languages: LanguageMap;
}
20 changes: 20 additions & 0 deletions webapp/src/utils/util.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4b88185

Please sign in to comment.