UNPKG

declapract

Version:

A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.

42 lines (39 loc) 1.14 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseJSON = exports.JSONParsingError = void 0; const json5_1 = __importDefault(require("json5")); class JSONParsingError extends Error { constructor({ jsonError, json5Error, contents, }) { super(` Could not parse contents as neither JSON nor JSON5. JSON Error: ${jsonError.message} JSON5 Error: ${json5Error.message} Contents Head: ${contents.slice(0, 50)}${contents.length > 50 ? '...' : ''} `.trim()); } } exports.JSONParsingError = JSONParsingError; /** * parse json with support for json5, when needed */ const parseJSON = (contents) => { try { return JSON.parse(contents); } catch (jsonError) { try { return json5_1.default.parse(contents); } catch (json5Error) { throw new JSONParsingError({ json5Error, jsonError, contents }); } } }; exports.parseJSON = parseJSON; //# sourceMappingURL=parseJSON.js.map