Files
systemd-timer/src/utils/fs.ts
Max P. 2a13ee2539 refactor(cli): integrate i18n support across commands
- Centralize CLI text strings using the i18n module for localization
- Refactor `createCommand` and `createCli` to improve modularity
- Update logging and error messages to use translated strings
2025-05-28 18:09:52 +02:00

48 lines
1.5 KiB
TypeScript

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';
import { t } from '../i18n/mod.ts';
export async function writeUnitFiles(
name: string,
serviceContent: string,
timerContent: string,
options: TimerOptions,
): Promise<{ servicePath: string; timerPath: string } | undefined> {
const basePath = resolveUnitTargetPath(options);
await ensureDir(basePath);
const servicePath = join(basePath, `${name}.service`);
const timerPath = join(basePath, `${name}.timer`);
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(t('rollback_failed'), rollbackError);
}
console.error(t('error_write_units'), error);
return undefined;
}
return { servicePath, timerPath };
}
export function resolveUnitTargetPath(
options: Pick<TimerOptions, 'output' | 'user'>,
): string {
if (options.output) return options.output;
if (options.user) return `${Deno.env.get('HOME')}/.config/systemd/user`;
return '/etc/systemd/system';
}