UNPKG

read-config-ng

Version:
73 lines 2.14 kB
import { promises as fs, readFileSync } from 'fs'; import * as path from 'path'; import * as json5 from 'json5'; import { ReadConfigError } from '../../read-config-error.js'; /** * JSON5 parser implementation */ export const parser = { /** * Load and parse a JSON5 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 JSON5 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 JSON5 content asynchronously */ async parse(content) { if (!content || !content.trim().length) { return {}; } try { const result = json5.parse(content); return result; } catch (error) { throw new ReadConfigError(`Failed to parse JSON5 content: ${error.message}`, 'PARSE_ERROR'); } }, /** * Parse JSON5 content synchronously */ parseSync(content) { if (!content || !content.trim().length) { return {}; } try { const result = json5.parse(content); return result; } catch (error) { throw new ReadConfigError(`Failed to parse JSON5 content: ${error.message}`, 'PARSE_ERROR'); } } }; export default parser; //# sourceMappingURL=json5.js.map