UNPKG

read-config-ng

Version:
73 lines 2.15 kB
import { promises as fs, readFileSync } from 'fs'; import * as path from 'path'; import * as yaml from 'js-yaml'; import { ReadConfigError } from '../../read-config-error.js'; /** * YAML parser implementation */ export const parser = { /** * Load and parse a YAML file asynchronously */ async load(filePath) { const absolutePath = path.resolve(filePath); try { const content = await fs.readFile(absolutePath, 'utf8'); return await this.parse(content); } catch (error) { if (error.code === 'ENOENT') { throw ReadConfigError.fileNotFound(absolutePath); } throw ReadConfigError.parseError(absolutePath, error); } }, /** * Load and parse a YAML file synchronously */ loadSync(filePath) { const absolutePath = path.resolve(filePath); try { const content = readFileSync(absolutePath, 'utf8'); return this.parseSync(content); } catch (error) { if (error.code === 'ENOENT') { throw ReadConfigError.fileNotFound(absolutePath); } throw ReadConfigError.parseError(absolutePath, error); } }, /** * Parse YAML content asynchronously */ async parse(content) { if (!content || !content.trim().length) { return {}; } try { const result = yaml.load(content); return (result || {}); } catch (error) { throw new ReadConfigError(`Failed to parse YAML content: ${error.message}`, 'PARSE_ERROR'); } }, /** * Parse YAML content synchronously */ parseSync(content) { if (!content || !content.trim().length) { return {}; } try { const result = yaml.load(content); return (result || {}); } catch (error) { throw new ReadConfigError(`Failed to parse YAML content: ${error.message}`, 'PARSE_ERROR'); } } }; export default parser; //# sourceMappingURL=yaml.js.map