UNPKG

read-config-ng

Version:
95 lines 3.06 kB
import { promises as fs, readFileSync } from 'fs'; import * as path from 'path'; import { ReadConfigError } from '../../read-config-error.js'; // Properties library doesn't have official types and is CommonJS import { createRequire } from 'module'; const require = createRequire(import.meta.url); const properties = require('properties'); /** * Properties parser implementation */ export const parser = { /** * Load and parse a properties 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 properties 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 properties content asynchronously */ async parse(content) { if (!content || !content.trim().length) { return {}; } return new Promise((resolve, reject) => { properties.parse(content, (error, result) => { if (error) { reject(new ReadConfigError(`Failed to parse properties content: ${error.message}`, 'PARSE_ERROR')); } else { resolve(result || {}); } }); }); }, /** * Parse properties content synchronously */ parseSync(content) { if (!content || !content.trim().length) { return {}; } try { // Properties library doesn't have a sync parse, so we'll use a workaround let result = {}; let error = null; properties.parse(content, (err, res) => { if (err) { error = err; } else { result = res || {}; } }); if (error) { throw new ReadConfigError(`Failed to parse properties content: ${error.message}`, 'PARSE_ERROR'); } return result; } catch (error) { if (error instanceof ReadConfigError) { throw error; } throw new ReadConfigError(`Failed to parse properties content: ${error.message}`, 'PARSE_ERROR'); } } }; export default parser; //# sourceMappingURL=properties.js.map