UNPKG

photo-watermark-cli

Version:

A modern TypeScript CLI tool to add timestamp watermarks to photos with intelligent size scaling

84 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_CONFIG = void 0; exports.loadConfig = loadConfig; exports.saveConfig = saveConfig; exports.resetConfig = resetConfig; exports.getConfigPath = getConfigPath; const fs_1 = require("fs"); const path_1 = require("path"); const os_1 = require("os"); // 配置文件路径 const CONFIG_DIR = (0, path_1.join)((0, os_1.homedir)(), '.watermark-cli'); const CONFIG_FILE = (0, path_1.join)(CONFIG_DIR, 'config.json'); // 默认配置 exports.DEFAULT_CONFIG = { timeFormat: 'YYYY-MM-DD HH:mm:ss', position: 'bottom-left', fontSize: 24, fontColor: 'white', addShadow: true, quality: 95, brightness: 1.0 // 默认亮度,1.0为原始亮度 }; /** * 确保配置目录存在 */ async function ensureConfigDir() { try { await fs_1.promises.mkdir(CONFIG_DIR, { recursive: true }); } catch (error) { // 目录可能已存在,忽略错误 } } /** * 加载配置文件 * @returns 配置对象 */ async function loadConfig() { try { await ensureConfigDir(); const configData = await fs_1.promises.readFile(CONFIG_FILE, 'utf8'); const config = JSON.parse(configData); // 合并默认配置和用户配置 return { ...exports.DEFAULT_CONFIG, ...config }; } catch (error) { // 配置文件不存在或损坏,返回默认配置 return { ...exports.DEFAULT_CONFIG }; } } /** * 保存配置文件 * @param config - 要保存的配置 */ async function saveConfig(config) { try { await ensureConfigDir(); const configToSave = { ...exports.DEFAULT_CONFIG, ...config }; await fs_1.promises.writeFile(CONFIG_FILE, JSON.stringify(configToSave, null, 2)); } catch (error) { throw new Error(`保存配置失败: ${error.message}`); } } /** * 重置配置为默认值 */ async function resetConfig() { try { await saveConfig(exports.DEFAULT_CONFIG); } catch (error) { throw new Error(`重置配置失败: ${error.message}`); } } /** * 获取配置文件路径 * @returns 配置文件路径 */ function getConfigPath() { return CONFIG_FILE; } //# sourceMappingURL=config.js.map