UNPKG

@shutootaki/gwm

Version:
137 lines 5.25 kB
import TOML from '@ltd/j-toml'; import { DEFAULT_CONFIG } from './defaults.js'; /** * TOML文字列をパースする */ export function parseTOML(content) { return TOML.parse(content); } /** * worktree_base_path設定を検証・処理 */ function parseWorktreeBasePath(parsed) { return typeof parsed.worktree_base_path === 'string' && parsed.worktree_base_path.trim() ? parsed.worktree_base_path : DEFAULT_CONFIG.worktree_base_path; } /** * main_branches設定を検証・処理 */ function parseMainBranches(parsed) { const mainBranches = Array.isArray(parsed.main_branches) ? parsed.main_branches.filter((v) => typeof v === 'string' && v.trim() !== '') : DEFAULT_CONFIG.main_branches; return mainBranches.length > 0 ? mainBranches : DEFAULT_CONFIG.main_branches; } /** * clean_branch設定を検証・処理 */ function parseCleanBranch(parsed) { const cleanBranchRaw = parsed.clean_branch; return cleanBranchRaw === 'auto' || cleanBranchRaw === 'never' ? cleanBranchRaw : 'ask'; } /** * copy_ignored_files設定を検証・処理 */ function parseCopyIgnoredFiles(parsed) { let copyIgnoredFiles = DEFAULT_CONFIG.copy_ignored_files; if (parsed.copy_ignored_files && typeof parsed.copy_ignored_files === 'object') { const cif = parsed.copy_ignored_files; copyIgnoredFiles = { enabled: typeof cif.enabled === 'boolean' ? cif.enabled : DEFAULT_CONFIG.copy_ignored_files.enabled, patterns: Array.isArray(cif.patterns) ? cif.patterns.filter((v) => typeof v === 'string') : DEFAULT_CONFIG.copy_ignored_files.patterns, exclude_patterns: Array.isArray(cif.exclude_patterns) ? cif.exclude_patterns.filter((v) => typeof v === 'string') : DEFAULT_CONFIG.copy_ignored_files.exclude_patterns, }; } return copyIgnoredFiles; } /** * virtual_env_handling設定を検証・処理 */ function parseVirtualEnvHandling(parsed) { let virtualEnvHandling = DEFAULT_CONFIG.virtual_env_handling; if (parsed.virtual_env_handling && typeof parsed.virtual_env_handling === 'object') { const veh = parsed.virtual_env_handling; const isolateRaw = veh['isolate_virtual_envs']; const modeRaw = veh['mode']; // deprecated // 後方互換: isolate_virtual_envs が優先。未指定なら mode を解釈。 let isolate_virtual_envs; if (typeof isolateRaw === 'boolean') { isolate_virtual_envs = isolateRaw; } else if (modeRaw === 'ignore') { isolate_virtual_envs = false; } else if (modeRaw === 'skip') { isolate_virtual_envs = true; } else { isolate_virtual_envs = false; // デフォルト } const maxFileSizeRaw = veh['max_file_size_mb']; const maxCopySizeRaw = veh['max_copy_size_mb']; // deprecated key const max_file_size_mb = typeof maxFileSizeRaw === 'number' && maxFileSizeRaw >= -1 ? maxFileSizeRaw : typeof maxCopySizeRaw === 'number' && maxCopySizeRaw >= 0 ? maxCopySizeRaw : 100; const max_dir_size_mb_raw = veh['max_dir_size_mb']; const max_dir_size_mb = typeof max_dir_size_mb_raw === 'number' && max_dir_size_mb_raw >= -1 ? max_dir_size_mb_raw : 500; const max_scan_depth_raw = veh['max_scan_depth']; const max_scan_depth = typeof max_scan_depth_raw === 'number' && max_scan_depth_raw >= -1 ? max_scan_depth_raw : 5; const copy_parallelism_raw = veh['copy_parallelism']; const copy_parallelism = typeof copy_parallelism_raw === 'number' && copy_parallelism_raw >= 0 ? copy_parallelism_raw : 4; const customRaw = veh['custom_patterns']; let customPatternsFiltered; if (Array.isArray(customRaw)) { customPatternsFiltered = customRaw.filter((p) => typeof p === 'object' && p !== null && typeof p.language === 'string' && Array.isArray(p.patterns)); } virtualEnvHandling = { isolate_virtual_envs, custom_patterns: customPatternsFiltered, max_file_size_mb, max_dir_size_mb, max_scan_depth, copy_parallelism, // backward compatibility max_copy_size_mb: typeof maxCopySizeRaw === 'number' ? maxCopySizeRaw : undefined, mode: typeof modeRaw === 'string' ? modeRaw : undefined, }; } return virtualEnvHandling; } /** * パースされたTOMLデータを設定オブジェクトに変換 */ export function parseConfigFromTOML(parsed) { return { worktree_base_path: parseWorktreeBasePath(parsed), main_branches: parseMainBranches(parsed), clean_branch: parseCleanBranch(parsed), copy_ignored_files: parseCopyIgnoredFiles(parsed), virtual_env_handling: parseVirtualEnvHandling(parsed), }; } //# sourceMappingURL=parser.js.map