From b9f92f7430ac46d965287e1bb236cbb040f29a5e Mon Sep 17 00:00:00 2001 From: Nico <47644445+nicotsx@users.noreply.github.com> Date: Sat, 10 Jan 2026 11:57:24 +0100 Subject: [PATCH] chore: add a pre-commit hook to format files (#336) chore: add a pre-commit hook to format files --- .oxlintrc.json | 3 +- app/client/hooks/use-server-events.ts | 20 ++++++------- app/server/cli/commands/disable-2fa.ts | 26 +++++----------- app/server/cli/commands/reset-password.ts | 30 +++++-------------- app/server/core/capabilities.ts | 2 -- .../notifications/notifications.service.ts | 2 +- lefthook.yml | 7 +++++ 7 files changed, 35 insertions(+), 55 deletions(-) create mode 100644 lefthook.yml diff --git a/.oxlintrc.json b/.oxlintrc.json index 0caeb87a..cde16021 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -113,7 +113,8 @@ "unicorn/no-useless-spread": "warn", "unicorn/prefer-set-size": "warn", "unicorn/prefer-string-starts-ends-with": "warn", - "import/no-cycle": "error" + "import/no-cycle": "error", + "eslint/no-console": ["warn", { "allow": ["warn", "error", "info"] }] }, "settings": { "jsx-a11y": { diff --git a/app/client/hooks/use-server-events.ts b/app/client/hooks/use-server-events.ts index 211d33dc..fe3fc4bf 100644 --- a/app/client/hooks/use-server-events.ts +++ b/app/client/hooks/use-server-events.ts @@ -61,14 +61,14 @@ export function useServerEvents() { eventSourceRef.current = eventSource; eventSource.addEventListener("connected", () => { - console.log("[SSE] Connected to server events"); + console.info("[SSE] Connected to server events"); }); eventSource.addEventListener("heartbeat", () => {}); eventSource.addEventListener("backup:started", (e) => { const data = JSON.parse(e.data) as BackupEvent; - console.log("[SSE] Backup started:", data); + console.info("[SSE] Backup started:", data); handlersRef.current.get("backup:started")?.forEach((handler) => { handler(data); @@ -85,7 +85,7 @@ export function useServerEvents() { eventSource.addEventListener("backup:completed", (e) => { const data = JSON.parse(e.data) as BackupEvent; - console.log("[SSE] Backup completed:", data); + console.info("[SSE] Backup completed:", data); void queryClient.invalidateQueries(); void queryClient.refetchQueries(); @@ -97,7 +97,7 @@ export function useServerEvents() { eventSource.addEventListener("volume:mounted", (e) => { const data = JSON.parse(e.data) as VolumeEvent; - console.log("[SSE] Volume mounted:", data); + console.info("[SSE] Volume mounted:", data); handlersRef.current.get("volume:mounted")?.forEach((handler) => { handler(data); @@ -106,7 +106,7 @@ export function useServerEvents() { eventSource.addEventListener("volume:unmounted", (e) => { const data = JSON.parse(e.data) as VolumeEvent; - console.log("[SSE] Volume unmounted:", data); + console.info("[SSE] Volume unmounted:", data); handlersRef.current.get("volume:unmounted")?.forEach((handler) => { handler(data); @@ -115,7 +115,7 @@ export function useServerEvents() { eventSource.addEventListener("volume:updated", (e) => { const data = JSON.parse(e.data) as VolumeEvent; - console.log("[SSE] Volume updated:", data); + console.info("[SSE] Volume updated:", data); void queryClient.invalidateQueries(); @@ -126,7 +126,7 @@ export function useServerEvents() { eventSource.addEventListener("volume:status_updated", (e) => { const data = JSON.parse(e.data) as VolumeEvent; - console.log("[SSE] Volume status updated:", data); + console.info("[SSE] Volume status updated:", data); void queryClient.invalidateQueries(); @@ -137,7 +137,7 @@ export function useServerEvents() { eventSource.addEventListener("mirror:started", (e) => { const data = JSON.parse(e.data) as MirrorEvent; - console.log("[SSE] Mirror copy started:", data); + console.info("[SSE] Mirror copy started:", data); handlersRef.current.get("mirror:started")?.forEach((handler) => { handler(data); @@ -146,7 +146,7 @@ export function useServerEvents() { eventSource.addEventListener("mirror:completed", (e) => { const data = JSON.parse(e.data) as MirrorEvent; - console.log("[SSE] Mirror copy completed:", data); + console.info("[SSE] Mirror copy completed:", data); // Invalidate queries to refresh mirror status in the UI void queryClient.invalidateQueries(); @@ -161,7 +161,7 @@ export function useServerEvents() { }; return () => { - console.log("[SSE] Disconnecting from server events"); + console.info("[SSE] Disconnecting from server events"); eventSource.close(); eventSourceRef.current = null; }; diff --git a/app/server/cli/commands/disable-2fa.ts b/app/server/cli/commands/disable-2fa.ts index 1e0f1413..f4de9046 100644 --- a/app/server/cli/commands/disable-2fa.ts +++ b/app/server/cli/commands/disable-2fa.ts @@ -6,16 +6,11 @@ import { db } from "../../db/db"; import { twoFactor, usersTable } from "../../db/schema"; const listUsers = () => { - return db - .select({ id: usersTable.id, username: usersTable.username }) - .from(usersTable); + return db.select({ id: usersTable.id, username: usersTable.username }).from(usersTable); }; const disable2FA = async (username: string) => { - const [user] = await db - .select() - .from(usersTable) - .where(eq(usersTable.username, username)); + const [user] = await db.select().from(usersTable).where(eq(usersTable.username, username)); if (!user) { throw new Error(`User "${username}" not found`); @@ -26,10 +21,7 @@ const disable2FA = async (username: string) => { } await db.transaction(async (tx) => { - await tx - .update(usersTable) - .set({ twoFactorEnabled: false }) - .where(eq(usersTable.id, user.id)); + await tx.update(usersTable).set({ twoFactorEnabled: false }).where(eq(usersTable.id, user.id)); await tx.delete(twoFactor).where(eq(twoFactor.userId, user.id)); }); }; @@ -38,7 +30,7 @@ export const disable2FACommand = new Command("disable-2fa") .description("Disable two-factor authentication for a user") .option("-u, --username ", "Username of the account") .action(async (options) => { - console.log("\nšŸ” Zerobyte 2FA Disable\n"); + console.info("\nšŸ” Zerobyte 2FA Disable\n"); let username = options.username; @@ -47,9 +39,7 @@ export const disable2FACommand = new Command("disable-2fa") if (users.length === 0) { console.error("āŒ No users found in the database."); - console.log( - " Please create a user first by starting the application.", - ); + console.info(" Please create a user first by starting the application."); process.exit(1); } @@ -61,10 +51,8 @@ export const disable2FACommand = new Command("disable-2fa") try { await disable2FA(username); - console.log( - `\nāœ… Two-factor authentication has been disabled for user "${username}".`, - ); - console.log(" The user can re-enable 2FA from their account settings."); + console.info(`\nāœ… Two-factor authentication has been disabled for user "${username}".`); + console.info(" The user can re-enable 2FA from their account settings."); } catch (error) { console.error(`\nāŒ Failed to disable 2FA: ${toMessage(error)}`); process.exit(1); diff --git a/app/server/cli/commands/reset-password.ts b/app/server/cli/commands/reset-password.ts index ae482c6d..0306f802 100644 --- a/app/server/cli/commands/reset-password.ts +++ b/app/server/cli/commands/reset-password.ts @@ -7,16 +7,11 @@ import { db } from "../../db/db"; import { account, sessionsTable, usersTable } from "../../db/schema"; const listUsers = () => { - return db - .select({ id: usersTable.id, username: usersTable.username }) - .from(usersTable); + return db.select({ id: usersTable.id, username: usersTable.username }).from(usersTable); }; const resetPassword = async (username: string, newPassword: string) => { - const [user] = await db - .select() - .from(usersTable) - .where(eq(usersTable.username, username)); + const [user] = await db.select().from(usersTable).where(eq(usersTable.username, username)); if (!user) { throw new Error(`User "${username}" not found`); @@ -28,16 +23,11 @@ const resetPassword = async (username: string, newPassword: string) => { await tx .update(account) .set({ password: newPasswordHash }) - .where( - and(eq(account.userId, user.id), eq(account.providerId, "credential")), - ); + .where(and(eq(account.userId, user.id), eq(account.providerId, "credential"))); if (user.passwordHash) { const legacyHash = await Bun.password.hash(newPassword); - await tx - .update(usersTable) - .set({ passwordHash: legacyHash }) - .where(eq(usersTable.id, user.id)); + await tx.update(usersTable).set({ passwordHash: legacyHash }).where(eq(usersTable.id, user.id)); } await tx.delete(sessionsTable).where(eq(sessionsTable.userId, user.id)); @@ -49,7 +39,7 @@ export const resetPasswordCommand = new Command("reset-password") .option("-u, --username ", "Username of the account") .option("-p, --password ", "New password for the account") .action(async (options) => { - console.log("\nšŸ” Zerobyte Password Reset\n"); + console.info("\nšŸ” Zerobyte Password Reset\n"); let username = options.username; let newPassword = options.password; @@ -59,9 +49,7 @@ export const resetPasswordCommand = new Command("reset-password") if (users.length === 0) { console.error("āŒ No users found in the database."); - console.log( - " Please create a user first by starting the application.", - ); + console.info(" Please create a user first by starting the application."); process.exit(1); } @@ -99,10 +87,8 @@ export const resetPasswordCommand = new Command("reset-password") try { await resetPassword(username, newPassword); - console.log( - `\nāœ… Password for user "${username}" has been reset successfully.`, - ); - console.log(" All existing sessions have been invalidated."); + console.info(`\nāœ… Password for user "${username}" has been reset successfully.`); + console.info(" All existing sessions have been invalidated."); } catch (error) { console.error(`\nāŒ Failed to reset password: ${toMessage(error)}`); process.exit(1); diff --git a/app/server/core/capabilities.ts b/app/server/core/capabilities.ts index 41e75581..8f7dc029 100644 --- a/app/server/core/capabilities.ts +++ b/app/server/core/capabilities.ts @@ -1,6 +1,4 @@ import * as fs from "node:fs/promises"; -import os from "node:os"; -import path from "node:path"; import { logger } from "../utils/logger"; export type SystemCapabilities = { diff --git a/app/server/modules/notifications/notifications.service.ts b/app/server/modules/notifications/notifications.service.ts index e927e9ac..395056ff 100644 --- a/app/server/modules/notifications/notifications.service.ts +++ b/app/server/modules/notifications/notifications.service.ts @@ -229,7 +229,7 @@ const testDestination = async (id: number) => { const shoutrrrUrl = buildShoutrrrUrl(decryptedConfig); - console.log("Testing notification with Shoutrrr URL:", shoutrrrUrl); + logger.debug("Testing notification with Shoutrrr URL:", shoutrrrUrl); const result = await sendNotification({ shoutrrrUrl, diff --git a/lefthook.yml b/lefthook.yml new file mode 100644 index 00000000..0af659fe --- /dev/null +++ b/lefthook.yml @@ -0,0 +1,7 @@ +pre-commit: + commands: + oxfmt: + glob: '*.{js,jsx,ts,tsx,json,jsonc}' + run: bunx oxfmt --write {staged_files} + stage_fixed: true +