The minimalistic document archiving platform.
fix(config): no longer log on stderr when git is not available (#880)
Corentin Thomasset4f5b29b7edc1
diff --git a/.changeset/lovely-cars-worry.md b/.changeset/lovely-cars-worry.mdnew file mode 100644index 00000000..2d3b9372--- /dev/null+++ b/.changeset/lovely-cars-worry.md@@ -0,0 +1,5 @@+---+"@papra/app": patch+---++Removed misleading "git missing" error log on app startup when git isn't available, like in Docker env.diff --git a/apps/papra-server/src/modules/config/config.ts b/apps/papra-server/src/modules/config/config.tsindex 45359a63..3efc214c 100644--- a/apps/papra-server/src/modules/config/config.ts+++ b/apps/papra-server/src/modules/config/config.ts@@ -172,7 +172,7 @@ export async function parseConfig({ env = process.env }: { env?: Record<string,cwd: env.PAPRA_CONFIG_DIR ?? process.cwd(),});- const { gitCommitSha, gitCommitDate } = getCommitInfo();+ const { gitCommitSha, gitCommitDate } = await getCommitInfo();const [configResult, configError] = safelySync(() => defineConfig(configDefinition, {envSource: env,diff --git a/apps/papra-server/src/modules/config/config.usecases.ts b/apps/papra-server/src/modules/config/config.usecases.tsindex 028ecca5..96a2f8b7 100644--- a/apps/papra-server/src/modules/config/config.usecases.ts+++ b/apps/papra-server/src/modules/config/config.usecases.ts@@ -1,12 +1,17 @@-import { execSync } from 'node:child_process';-import { safelySync } from '@corentinth/chisels';+import { executeCommandSafely } from '../shared/exec/exec.services';+import { isNilOrEmptyString } from '../shared/utils';-export function getCommitInfo() {- const [gitCommitSha] = safelySync(() => execSync('git rev-parse HEAD', { encoding: 'utf-8' }).trim());- const [gitCommitDate] = safelySync(() => execSync('git show -s --format=%cI HEAD', { encoding: 'utf-8' }).trim());+export async function getCommitInfo() {+ const [+ { stdout: gitCommitShaStdout },+ { stdout: gitCommitDateStdout },+ ] = await Promise.all([+ executeCommandSafely('git rev-parse HEAD'),+ executeCommandSafely('git show -s --format=%cI HEAD'),+ ]);return {- gitCommitSha: gitCommitSha ?? undefined,- gitCommitDate: gitCommitDate ?? undefined,+ gitCommitSha: isNilOrEmptyString(gitCommitShaStdout) ? undefined : gitCommitShaStdout,+ gitCommitDate: isNilOrEmptyString(gitCommitDateStdout) ? undefined : gitCommitDateStdout,};}diff --git a/apps/papra-server/src/modules/shared/exec/exec.services.test.ts b/apps/papra-server/src/modules/shared/exec/exec.services.test.tsnew file mode 100644index 00000000..f180a73d--- /dev/null+++ b/apps/papra-server/src/modules/shared/exec/exec.services.test.ts@@ -0,0 +1,59 @@+import { describe, expect, test } from 'vitest';+import { executeCommand, executeCommandSafely } from './exec.services';++describe('exec services', () => {+ describe('executeCommand', () => {+ test('execute a shell command and return stdout and stderr', async () => {+ expect(+ await executeCommand('echo "Hello, World!" && echo "This is an error message." 1>&2'),+ ).to.eql({+ stdout: 'Hello, World!',+ stderr: 'This is an error message.',+ });+ });++ test('an error is thrown if the command fails', async () => {+ await expect(executeCommand('exit 1')).rejects.toThrow();+ });++ test('stdout and stderr are trimmed', async () => {+ expect(+ await executeCommand('echo " Hello, World! " && echo " This is an error message. " 1>&2'),+ ).to.eql({+ stdout: 'Hello, World!',+ stderr: 'This is an error message.',+ });+ });+ });++ describe('executeCommandSafely', () => {+ test('returns stdout and stderr even if the command fails', async () => {+ expect(+ await executeCommandSafely('echo "Hello, World!" && echo "This is an error message." 1>&2 && exit 1'),+ ).to.eql({+ stdout: 'Hello, World!',+ stderr: 'This is an error message.',+ });++ expect(+ await executeCommandSafely('exit 1'),+ ).to.eql({+ stdout: '',+ stderr: '',+ });++ expect(await executeCommandSafely('echo "Only stdout"'),+ ).to.eql({+ stdout: 'Only stdout',+ stderr: '',+ });++ expect(+ await executeCommandSafely('echo "Only stderr" 1>&2 && exit 1'),+ ).to.eql({+ stdout: '',+ stderr: 'Only stderr',+ });+ });+ });+});diff --git a/apps/papra-server/src/modules/shared/exec/exec.services.ts b/apps/papra-server/src/modules/shared/exec/exec.services.tsnew file mode 100644index 00000000..23c0c179--- /dev/null+++ b/apps/papra-server/src/modules/shared/exec/exec.services.ts@@ -0,0 +1,30 @@+import type { ExecException, ExecOptions } from 'node:child_process';+import { exec } from 'node:child_process';+import { promisify } from 'node:util';++const execAsync = promisify(exec);++export async function executeCommand(command: string, options?: ExecOptions) {+ const { stdout, stderr } = await execAsync(command, {+ ...options,+ encoding: 'utf-8',+ });++ return {+ stdout: stdout.trim(),+ stderr: stderr.trim(),+ };+}++export async function executeCommandSafely(command: string, options?: ExecOptions) {+ try {+ return await executeCommand(command, options);+ } catch (error) {+ const execError = error as ExecException;++ return {+ stdout: execError.stdout?.trim() ?? '',+ stderr: execError.stderr?.trim() ?? '',+ };+ }+}