diff --git a/src/templates/unit-generator.ts b/src/templates/unit-generator.ts index faa14d6..9674b0a 100644 --- a/src/templates/unit-generator.ts +++ b/src/templates/unit-generator.ts @@ -12,14 +12,20 @@ export async function generateUnitFiles(options: TimerOptions): Promise { console.log(`\n===== ${name}.timer =====`); console.log(timerUnit); } else { - const { servicePath, timerPath } = await writeUnitFiles( + const result = await writeUnitFiles( name, serviceUnit, timerUnit, options, ); - console.log(`Service unit written to: ${servicePath}`); - console.log(`Timer unit written to: ${timerPath}`); + + if (result) { + const { servicePath, timerPath } = result; + console.log(`Service Unit geschrieben in: ${servicePath}`); + console.log(`Timer Unit geschrieben in: ${timerPath}`); + } else { + return; + } console.log(`\nℹ️ Hinweis:`); diff --git a/src/utils/__tests__/fs.test.ts b/src/utils/__tests__/fs.test.ts index a15dbaa..5109a54 100644 --- a/src/utils/__tests__/fs.test.ts +++ b/src/utils/__tests__/fs.test.ts @@ -24,7 +24,7 @@ Deno.test('writeUnitFiles schreibt .service und .timer korrekt', async () => { serviceContent, timerContent, options as TimerOptions, - ); + ) as { servicePath: string; timerPath: string }; // Überprüfe Pfade assertEquals(servicePath, join(tmp, 'test-backup.service')); diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 7294178..7dcc7df 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -1,4 +1,4 @@ -import { ensureDir } from 'https://deno.land/std@0.224.0/fs/mod.ts'; +import { ensureDir, exists } from 'https://deno.land/std@0.224.0/fs/mod.ts'; import { join } from 'https://deno.land/std@0.224.0/path/mod.ts'; import { TimerOptions } from '../types/mod.ts'; @@ -7,7 +7,7 @@ export async function writeUnitFiles( serviceContent: string, timerContent: string, options: TimerOptions, -): Promise<{ servicePath: string; timerPath: string }> { +): Promise<{ servicePath: string; timerPath: string } | undefined> { const basePath = resolveUnitTargetPath(options); await ensureDir(basePath); @@ -15,8 +15,24 @@ export async function writeUnitFiles( const servicePath = join(basePath, `${name}.service`); const timerPath = join(basePath, `${name}.timer`); - await Deno.writeTextFile(servicePath, serviceContent); - await Deno.writeTextFile(timerPath, timerContent); + try { + await Deno.writeTextFile(servicePath, serviceContent); + await Deno.writeTextFile(timerPath, timerContent); + } catch (error) { + // Rollback: Remove any files that were written + try { + if (await exists(servicePath)) { + await Deno.remove(servicePath); + } + if (await exists(timerPath)) { + await Deno.remove(timerPath); + } + } catch (rollbackError) { + console.error('Rollback fehlgeschlagen:', rollbackError); + } + console.error('Fehler beim Schreiben der Units:', error); + return undefined; + } return { servicePath, timerPath }; }