file-config.adapter.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. // File Config Adapter - Load and patch JSON configuration files
  2. import {
  3. existsSync,
  4. readFileSync,
  5. renameSync,
  6. unlinkSync,
  7. writeFileSync,
  8. } from "node:fs";
  9. import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
  10. import { homedir } from "node:os";
  11. import { basename, dirname, join, resolve } from "node:path";
  12. import type { RuntimeConfigPatch, UiLocale } from "@zcode/contracts";
  13. import { z } from "zod";
  14. import {
  15. CANONICAL_CUA_PLUGIN_ID,
  16. canonicalizePluginId,
  17. LEGACY_CUA_PLUGIN_ID,
  18. parseConfigFileToRuntimePatchWithDiagnostics,
  19. pluginIdAliases,
  20. type ConfigDiagnostic,
  21. } from "./schema.js";
  22. interface FileConfigOptions {
  23. baseDir?: string;
  24. configFileName?: string;
  25. }
  26. export interface LoadedConfig {
  27. config: RuntimeConfigPatch;
  28. diagnostics: ConfigDiagnostic[];
  29. path: string;
  30. loaded: boolean;
  31. }
  32. export interface UiLocalePatchResult {
  33. locale: UiLocale;
  34. path: string;
  35. }
  36. export interface PluginEnabledPatchResult {
  37. enabled: boolean;
  38. path: string;
  39. pluginId: string;
  40. }
  41. export interface PluginOptionsPatchResult {
  42. clearedOptionKeys: string[];
  43. options: Record<string, string | number | boolean>;
  44. path: string;
  45. pluginId: string;
  46. }
  47. export interface PluginRemovePatchResult {
  48. path: string;
  49. pluginId: string;
  50. removedEnabled: boolean;
  51. removedOptions: boolean;
  52. }
  53. const DEFAULT_CONFIG_FILE = "config.json";
  54. const DEFAULT_BASE_DIR = "~/.zcode/cli";
  55. /**
  56. * Resolve path with ~ expansion
  57. */
  58. export function resolvePath(path: string): string {
  59. if (path.startsWith("~/")) {
  60. return join(homedir(), path.slice(2));
  61. }
  62. return resolve(path);
  63. }
  64. /**
  65. * Load configuration from a JSON file
  66. */
  67. export function loadFileConfig(filePath?: string, options: FileConfigOptions = {}): LoadedConfig {
  68. const resolvedPath = filePath
  69. ? resolvePath(filePath)
  70. : join(
  71. resolvePath(options.baseDir ?? DEFAULT_BASE_DIR),
  72. options.configFileName ?? DEFAULT_CONFIG_FILE,
  73. );
  74. if (!existsSync(resolvedPath)) {
  75. return {
  76. config: {},
  77. diagnostics: [],
  78. path: resolvedPath,
  79. loaded: false,
  80. };
  81. }
  82. try {
  83. const content = readFileSync(resolvedPath, "utf-8");
  84. const parsed = JSON.parse(content);
  85. const migrated = migratePluginConfigInFile(parsed);
  86. if (migrated) {
  87. // 仅装载态归一化会让旧 key 永久留在磁盘,后续版本无法安全删除迁移逻辑。
  88. persistPluginConfigMigration(resolvedPath, migrated);
  89. }
  90. const result = parseConfigFileToRuntimePatchWithDiagnostics(parsed);
  91. return {
  92. config: result.config,
  93. diagnostics: attachDiagnosticFilePath(result.diagnostics, resolvedPath),
  94. path: resolvedPath,
  95. loaded: true,
  96. };
  97. } catch (error) {
  98. return {
  99. config: {},
  100. diagnostics: [createConfigFileInvalidDiagnostic(error, resolvedPath)],
  101. path: resolvedPath,
  102. loaded: false,
  103. };
  104. }
  105. }
  106. function migratePluginConfigInFile(value: unknown): Record<string, unknown> | undefined {
  107. if (!isRecord(value) || !isRecord(value.plugins)) return undefined;
  108. const plugins = value.plugins;
  109. const nextPlugins = { ...plugins };
  110. let changed = false;
  111. if (isRecord(plugins.enabledPlugins)) {
  112. const enabledPlugins = { ...plugins.enabledPlugins };
  113. for (const [id, enabled] of Object.entries(plugins.enabledPlugins)) {
  114. if (id === LEGACY_CUA_PLUGIN_ID) {
  115. const canonicalId = CANONICAL_CUA_PLUGIN_ID;
  116. if (enabledPlugins[canonicalId] === undefined) enabledPlugins[canonicalId] = enabled;
  117. delete enabledPlugins[id];
  118. changed = true;
  119. }
  120. }
  121. if (changed) nextPlugins.enabledPlugins = enabledPlugins;
  122. }
  123. if (Array.isArray(plugins.suppressedBuiltins)) {
  124. const suppressedBuiltins = plugins.suppressedBuiltins.map((id) =>
  125. id === LEGACY_CUA_PLUGIN_ID ? CANONICAL_CUA_PLUGIN_ID : id,
  126. );
  127. if (JSON.stringify(suppressedBuiltins) !== JSON.stringify(plugins.suppressedBuiltins)) {
  128. nextPlugins.suppressedBuiltins = suppressedBuiltins;
  129. changed = true;
  130. }
  131. }
  132. if (isRecord(plugins.options)) {
  133. const options = { ...plugins.options };
  134. for (const [id, pluginOptions] of Object.entries(plugins.options)) {
  135. if (id === LEGACY_CUA_PLUGIN_ID) {
  136. const canonicalId = CANONICAL_CUA_PLUGIN_ID;
  137. if (options[canonicalId] === undefined) options[canonicalId] = pluginOptions;
  138. delete options[id];
  139. changed = true;
  140. }
  141. }
  142. if (changed) nextPlugins.options = options;
  143. }
  144. return changed ? { ...value, plugins: nextPlugins } : undefined;
  145. }
  146. function persistPluginConfigMigration(
  147. filePath: string,
  148. value: Record<string, unknown>,
  149. ): void {
  150. const tempPath = `${filePath}.migrate.${process.pid}.${Date.now()}.tmp`;
  151. try {
  152. writeFileSync(tempPath, `${JSON.stringify(value, null, 2)}\n`, {
  153. encoding: "utf-8",
  154. mode: 0o600,
  155. });
  156. renameSync(tempPath, filePath);
  157. } catch {
  158. // 迁移写回是 best-effort 副作用,失败不能改变合法配置的装载语义。
  159. try {
  160. unlinkSync(tempPath);
  161. } catch {
  162. // 临时文件清理失败不影响当前配置装载。
  163. }
  164. }
  165. }
  166. function createConfigFileInvalidDiagnostic(error: unknown, filePath: string): ConfigDiagnostic {
  167. return {
  168. code: "config_file_invalid",
  169. filePath,
  170. message:
  171. error instanceof z.ZodError
  172. ? formatZodError(error)
  173. : error instanceof Error
  174. ? error.message
  175. : "Unable to parse config file.",
  176. severity: "error",
  177. };
  178. }
  179. function attachDiagnosticFilePath(
  180. diagnostics: ConfigDiagnostic[],
  181. filePath: string,
  182. ): ConfigDiagnostic[] {
  183. return diagnostics.map((diagnostic) => ({
  184. ...diagnostic,
  185. filePath: diagnostic.filePath ?? filePath,
  186. }));
  187. }
  188. function formatZodError(error: z.ZodError): string {
  189. return error.issues
  190. .map((issue) => {
  191. const path = issue.path.length > 0 ? issue.path.join(".") : "<config>";
  192. return `${path}: ${issue.message}`;
  193. })
  194. .join("; ");
  195. }
  196. /**
  197. * Patch only the UI locale selection in a JSON config file.
  198. */
  199. export async function updateUiLocaleInFileConfig(
  200. filePath: string,
  201. locale: UiLocale,
  202. ): Promise<UiLocalePatchResult> {
  203. const resolvedPath = resolvePath(filePath);
  204. const parsed = await readJsonConfigFileOrEmpty(resolvedPath);
  205. const next = patchUiLocale(parsed, locale);
  206. await atomicWriteJson(resolvedPath, next);
  207. return {
  208. locale,
  209. path: resolvedPath,
  210. };
  211. }
  212. /**
  213. * Patch the user plugin enabled map without touching plugin installation state.
  214. */
  215. export async function updatePluginEnabledInFileConfig(
  216. filePath: string,
  217. pluginId: string,
  218. enabled: boolean,
  219. ): Promise<PluginEnabledPatchResult> {
  220. const resolvedPath = resolvePath(filePath);
  221. const parsed = await readJsonConfigFileOrEmpty(resolvedPath);
  222. const next = patchPluginEnabled(parsed, pluginId, enabled);
  223. await atomicWriteJson(resolvedPath, next);
  224. return {
  225. enabled,
  226. path: resolvedPath,
  227. pluginId,
  228. };
  229. }
  230. /**
  231. * Mark freshly installed plugins as enabled by default, in a single atomic write.
  232. *
  233. * 设计:安装即默认启用(仅对本次安装的插件 + 其依赖闭包生效)。这里只对**用户配置里尚未显式声明**
  234. * 的 id 写入 `true`——若用户先前显式停用过(例如停用后重装),尊重其选择不覆盖;已是 true 的也跳过。
  235. * 一次性读改写,避免逐个 id 反复读写配置文件。返回真正被新置为启用的 id 列表,便于上层据此决定是否回写。
  236. */
  237. export async function enablePluginsByDefaultInFileConfig(
  238. filePath: string,
  239. pluginIds: readonly string[],
  240. ): Promise<{ enabledIds: string[]; path: string }> {
  241. const resolvedPath = resolvePath(filePath);
  242. if (pluginIds.length === 0) {
  243. return { enabledIds: [], path: resolvedPath };
  244. }
  245. const parsed = await readJsonConfigFileOrEmpty(resolvedPath);
  246. const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
  247. const enabledPlugins = isRecord(plugins.enabledPlugins) ? plugins.enabledPlugins : {};
  248. const enabledIds = pluginIds.filter(
  249. (id) => !Object.prototype.hasOwnProperty.call(enabledPlugins, id),
  250. );
  251. if (enabledIds.length === 0) {
  252. return { enabledIds: [], path: resolvedPath };
  253. }
  254. const next = {
  255. ...parsed,
  256. plugins: {
  257. ...plugins,
  258. enabledPlugins: {
  259. ...enabledPlugins,
  260. ...Object.fromEntries(enabledIds.map((id) => [id, true])),
  261. },
  262. },
  263. };
  264. await atomicWriteJson(resolvedPath, next);
  265. return { enabledIds, path: resolvedPath };
  266. }
  267. /**
  268. * Patch plugin user options without touching plugin installation state.
  269. */
  270. export async function updatePluginOptionsInFileConfig(
  271. filePath: string,
  272. pluginId: string,
  273. options: Record<string, string | number | boolean>,
  274. clearOptionKeys: string[] = [],
  275. ): Promise<PluginOptionsPatchResult> {
  276. const resolvedPath = resolvePath(filePath);
  277. const parsed = await readJsonConfigFileOrEmpty(resolvedPath);
  278. const next = patchPluginOptions(parsed, pluginId, options, clearOptionKeys);
  279. await atomicWriteJson(resolvedPath, next);
  280. return {
  281. clearedOptionKeys: clearOptionKeys,
  282. options,
  283. path: resolvedPath,
  284. pluginId,
  285. };
  286. }
  287. /**
  288. * Remove a plugin's user config footprint when it is uninstalled.
  289. *
  290. * Uninstall is a thorough teardown, so it
  291. * must drop both the `plugins.enabledPlugins[id]` flag and any saved
  292. * `plugins.options[id]`. updatePluginEnabledInFileConfig/...Options can only set
  293. * values; deleting the keys needs its own patch so a reinstall starts clean.
  294. */
  295. export async function removePluginFromFileConfig(
  296. filePath: string,
  297. pluginId: string,
  298. ): Promise<PluginRemovePatchResult> {
  299. const resolvedPath = resolvePath(filePath);
  300. const parsed = await readJsonConfigFileOrEmpty(resolvedPath);
  301. const { next, removedEnabled, removedOptions } = patchPluginRemoved(parsed, pluginId);
  302. if (removedEnabled || removedOptions) {
  303. await atomicWriteJson(resolvedPath, next);
  304. }
  305. return {
  306. path: resolvedPath,
  307. pluginId,
  308. removedEnabled,
  309. removedOptions,
  310. };
  311. }
  312. /**
  313. * 只删除 Plugin 的启用覆盖并保留 options。
  314. *
  315. * Workspace“恢复继承”是配置视图操作,只应删除 Workspace 的启用覆盖,不能误删
  316. * 单独保存的 Workspace options 或 secret。
  317. */
  318. export async function removePluginEnabledFromFileConfig(
  319. filePath: string,
  320. pluginId: string,
  321. ): Promise<{ path: string; pluginId: string; removedEnabled: boolean }> {
  322. const resolvedPath = resolvePath(filePath);
  323. const parsed = await readJsonConfigFileOrEmpty(resolvedPath);
  324. const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
  325. const enabledPlugins = isRecord(plugins.enabledPlugins) ? plugins.enabledPlugins : {};
  326. const aliases = pluginIdAliases(pluginId);
  327. const removedEnabled = aliases.some((id) =>
  328. Object.prototype.hasOwnProperty.call(enabledPlugins, id),
  329. );
  330. if (!removedEnabled) {
  331. return { path: resolvedPath, pluginId, removedEnabled: false };
  332. }
  333. const nextEnabled = { ...enabledPlugins };
  334. for (const id of aliases) delete nextEnabled[id];
  335. await atomicWriteJson(resolvedPath, {
  336. ...parsed,
  337. plugins: {
  338. ...plugins,
  339. enabledPlugins: nextEnabled,
  340. },
  341. });
  342. return { path: resolvedPath, pluginId, removedEnabled: true };
  343. }
  344. export interface SuppressedBuiltinPatchResult {
  345. path: string;
  346. pluginId: string;
  347. suppressed: boolean;
  348. }
  349. /**
  350. * Persist that a built-in (official) plugin is uninstalled, so seeding skips it
  351. * across restarts and app upgrades. Idempotent. Stored in user config beside
  352. * enabledPlugins so it survives bundle re-seeds.
  353. */
  354. export async function addSuppressedBuiltinInFileConfig(
  355. filePath: string,
  356. pluginId: string,
  357. ): Promise<SuppressedBuiltinPatchResult> {
  358. const resolvedPath = resolvePath(filePath);
  359. const parsed = await readJsonConfigFileOrEmpty(resolvedPath);
  360. const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
  361. const canonicalPluginId = canonicalizePluginId(pluginId);
  362. const aliases = pluginIdAliases(canonicalPluginId);
  363. const current = Array.isArray(plugins.suppressedBuiltins)
  364. ? (plugins.suppressedBuiltins as unknown[]).filter((v): v is string => typeof v === "string")
  365. : [];
  366. const retained = current.filter((id) => !aliases.includes(id));
  367. if (retained.length === current.length && current.includes(canonicalPluginId)) {
  368. return { path: resolvedPath, pluginId, suppressed: true };
  369. }
  370. const next = {
  371. ...parsed,
  372. plugins: { ...plugins, suppressedBuiltins: [...retained, canonicalPluginId] },
  373. };
  374. await atomicWriteJson(resolvedPath, next);
  375. return { path: resolvedPath, pluginId, suppressed: true };
  376. }
  377. /**
  378. * Reverse of addSuppressedBuiltinInFileConfig: restore a built-in by dropping its
  379. * suppression marker. The seeder re-materializes it on the next resolve.
  380. */
  381. export async function removeSuppressedBuiltinInFileConfig(
  382. filePath: string,
  383. pluginId: string,
  384. ): Promise<SuppressedBuiltinPatchResult> {
  385. const resolvedPath = resolvePath(filePath);
  386. const parsed = await readJsonConfigFileOrEmpty(resolvedPath);
  387. const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
  388. const aliases = pluginIdAliases(pluginId);
  389. const current = Array.isArray(plugins.suppressedBuiltins)
  390. ? (plugins.suppressedBuiltins as unknown[]).filter((v): v is string => typeof v === "string")
  391. : [];
  392. const nextSuppressedBuiltins = current.filter((id) => !aliases.includes(id));
  393. if (nextSuppressedBuiltins.length === current.length) {
  394. return { path: resolvedPath, pluginId, suppressed: false };
  395. }
  396. const next = {
  397. ...parsed,
  398. plugins: { ...plugins, suppressedBuiltins: nextSuppressedBuiltins },
  399. };
  400. await atomicWriteJson(resolvedPath, next);
  401. return { path: resolvedPath, pluginId, suppressed: false };
  402. }
  403. /**
  404. * Get default config file path
  405. */
  406. export function getDefaultConfigPath(): string {
  407. return join(resolvePath(DEFAULT_BASE_DIR), DEFAULT_CONFIG_FILE);
  408. }
  409. /**
  410. * Check if config file exists at default location
  411. */
  412. export function hasDefaultConfigFile(): boolean {
  413. return existsSync(getDefaultConfigPath());
  414. }
  415. async function readJsonConfigFile(filePath: string): Promise<Record<string, unknown>> {
  416. let content: string;
  417. try {
  418. content = await readFile(filePath, "utf-8");
  419. } catch (error) {
  420. throw new Error(`Unable to read config file: ${filePath}`, { cause: error });
  421. }
  422. let parsed: unknown;
  423. try {
  424. parsed = JSON.parse(content);
  425. } catch (error) {
  426. throw new Error(`Unable to parse config file as JSON: ${filePath}`, { cause: error });
  427. }
  428. if (!isRecord(parsed)) {
  429. throw new Error(`Config file must contain a JSON object: ${filePath}`);
  430. }
  431. return parsed;
  432. }
  433. async function readJsonConfigFileOrEmpty(filePath: string): Promise<Record<string, unknown>> {
  434. try {
  435. return await readJsonConfigFile(filePath);
  436. } catch (error) {
  437. const cause = error instanceof Error ? error.cause : undefined;
  438. if (isNodeError(cause) && cause.code === "ENOENT") {
  439. return {};
  440. }
  441. throw error;
  442. }
  443. }
  444. function patchUiLocale(parsed: Record<string, unknown>, locale: UiLocale): Record<string, unknown> {
  445. const currentUi = isRecord(parsed.ui) ? parsed.ui : {};
  446. return {
  447. ...parsed,
  448. ui: {
  449. ...currentUi,
  450. locale,
  451. },
  452. };
  453. }
  454. function patchPluginEnabled(
  455. parsed: Record<string, unknown>,
  456. pluginId: string,
  457. enabled: boolean,
  458. ): Record<string, unknown> {
  459. const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
  460. const enabledPlugins = isRecord(plugins.enabledPlugins) ? plugins.enabledPlugins : {};
  461. const canonicalPluginId = canonicalizePluginId(pluginId);
  462. const nextEnabledPlugins = { ...enabledPlugins };
  463. for (const id of pluginIdAliases(canonicalPluginId)) delete nextEnabledPlugins[id];
  464. return {
  465. ...parsed,
  466. plugins: {
  467. ...plugins,
  468. enabledPlugins: {
  469. ...nextEnabledPlugins,
  470. [canonicalPluginId]: enabled,
  471. },
  472. },
  473. };
  474. }
  475. function patchPluginOptions(
  476. parsed: Record<string, unknown>,
  477. pluginId: string,
  478. options: Record<string, string | number | boolean>,
  479. clearOptionKeys: string[],
  480. ): Record<string, unknown> {
  481. const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
  482. const currentOptions = isRecord(plugins.options) ? plugins.options : {};
  483. const canonicalPluginId = canonicalizePluginId(pluginId);
  484. const aliases = pluginIdAliases(canonicalPluginId);
  485. const legacyPluginId = aliases.length > 1 ? aliases[1] : undefined;
  486. const currentPluginOptions = isRecord(currentOptions[canonicalPluginId])
  487. ? currentOptions[canonicalPluginId]
  488. : legacyPluginId && isRecord(currentOptions[legacyPluginId])
  489. ? currentOptions[legacyPluginId]
  490. : {};
  491. const nextOptions = { ...currentOptions };
  492. for (const id of aliases) delete nextOptions[id];
  493. const clearedOptionKeySet = new Set(clearOptionKeys);
  494. const retainedPluginOptions = Object.fromEntries(
  495. Object.entries(currentPluginOptions).filter(([key]) => !clearedOptionKeySet.has(key)),
  496. );
  497. return {
  498. ...parsed,
  499. plugins: {
  500. ...plugins,
  501. options: {
  502. ...nextOptions,
  503. // 敏感字段按脱敏合同不会回传 UI,二次保存普通字段时请求中自然缺少
  504. // 已存 secret。这里按 option key 合并,避免整对象替换把同 scope 的密钥静默清空。
  505. // 显式清除走 clearOptionKeys,先删除指定键,再合并本次输入;不会连带删除启用状态
  506. // 或同插件的其他配置。
  507. [canonicalPluginId]: {
  508. ...retainedPluginOptions,
  509. ...options,
  510. },
  511. },
  512. },
  513. };
  514. }
  515. function patchPluginRemoved(
  516. parsed: Record<string, unknown>,
  517. pluginId: string,
  518. ): { next: Record<string, unknown>; removedEnabled: boolean; removedOptions: boolean } {
  519. const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
  520. const enabledPlugins = isRecord(plugins.enabledPlugins) ? plugins.enabledPlugins : {};
  521. const options = isRecord(plugins.options) ? plugins.options : {};
  522. const aliases = pluginIdAliases(pluginId);
  523. const removedEnabled = aliases.some((id) => id in enabledPlugins);
  524. const removedOptions = aliases.some((id) => id in options);
  525. if (!removedEnabled && !removedOptions) {
  526. return { next: parsed, removedEnabled, removedOptions };
  527. }
  528. const nextEnabled = { ...enabledPlugins };
  529. for (const id of aliases) delete nextEnabled[id];
  530. const nextOptions = { ...options };
  531. for (const id of aliases) delete nextOptions[id];
  532. return {
  533. next: {
  534. ...parsed,
  535. plugins: {
  536. ...plugins,
  537. enabledPlugins: nextEnabled,
  538. options: nextOptions,
  539. },
  540. },
  541. removedEnabled,
  542. removedOptions,
  543. };
  544. }
  545. async function atomicWriteJson(filePath: string, value: Record<string, unknown>): Promise<void> {
  546. const directory = dirname(filePath);
  547. await mkdir(directory, { recursive: true });
  548. const tempPath = join(
  549. directory,
  550. `.${basename(filePath)}.${process.pid}.${Date.now()}.${Math.random()
  551. .toString(16)
  552. .slice(2)}.tmp`,
  553. );
  554. const content = `${JSON.stringify(value, null, 2)}\n`;
  555. try {
  556. await writeFile(tempPath, content, { mode: 0o600 });
  557. await rename(tempPath, filePath);
  558. } catch (error) {
  559. await unlink(tempPath).catch(() => undefined);
  560. throw new Error(`Unable to write config file: ${filePath}`, { cause: error });
  561. }
  562. }
  563. function isRecord(value: unknown): value is Record<string, unknown> {
  564. return typeof value === "object" && value !== null && !Array.isArray(value);
  565. }
  566. function isNodeError(value: unknown): value is NodeJS.ErrnoException {
  567. return value instanceof Error && "code" in value;
  568. }