UNPKG

@lark-project/cli

Version:

飞书项目插件开发工具

55 lines (54 loc) 2.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.saveSettings = exports.loadSettings = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const auth_config_1 = require("../v2/utils/auth-config"); const SETTINGS_FILE = 'settings.json'; function resolveSettingsPath() { return path_1.default.join((0, auth_config_1.resolveConfigDir)(), SETTINGS_FILE); } function loadSettings() { try { const raw = fs_1.default.readFileSync(resolveSettingsPath(), 'utf-8'); return JSON.parse(raw); } catch (_a) { return {}; } } exports.loadSettings = loadSettings; function saveSettings(update) { const current = loadSettings(); const merged = Object.assign(Object.assign({}, current), update); // Remove keys set to undefined/null for (const key of Object.keys(merged)) { if (merged[key] == null) { delete merged[key]; } } const settingsPath = resolveSettingsPath(); fs_1.default.mkdirSync(path_1.default.dirname(settingsPath), { recursive: true }); // Atomic write: write to a pid-scoped temp file then rename, so concurrent // lpm processes (each command forks a fresh process) can't observe a // half-written settings.json. rename(2) is atomic within the same filesystem. const tmpPath = `${settingsPath}.${process.pid}.tmp`; try { fs_1.default.writeFileSync(tmpPath, JSON.stringify(merged, null, 2), { mode: 0o644 }); fs_1.default.renameSync(tmpPath, settingsPath); } catch (e) { // Don't leave a half-written temp file behind if write/rename failed. try { fs_1.default.rmSync(tmpPath, { force: true }); } catch (_a) { // best-effort cleanup } throw e; } } exports.saveSettings = saveSettings;