UNPKG

@ply-ct/ply

Version:

REST API Automated Testing

42 lines 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.merge = exports.parseJsonc = void 0; const os_1 = require("os"); const jsonc_parser_1 = require("jsonc-parser"); function parseJsonc(file, input) { const errs = []; const output = (0, jsonc_parser_1.parse)(input, errs); printErrors(file, errs); return output; } exports.parseJsonc = parseJsonc; function printErrors(file, errors) { if (errors.length > 0) { console.error(`jsonc-parser errors in ${file}:`); for (const err of errors) { const label = (0, jsonc_parser_1.printParseErrorCode)(err.error); console.error(` - ${label}:` + JSON.stringify(err)); } console.log(''); } } /** * Merge a raw object into jsonc content, respecting existing comments. */ function merge(file, json, delta, indent = 2) { try { const edits = []; Object.keys(delta).forEach((key) => { edits.push(...(0, jsonc_parser_1.modify)(json, [key], delta[key], { formattingOptions: { tabSize: indent, insertSpaces: true, eol: os_1.EOL } })); }); return (0, jsonc_parser_1.applyEdits)(json, edits); } catch (err) { console.error(err); throw new Error(`Error creating edits for file ${file}: ${err}`); } } exports.merge = merge; //# sourceMappingURL=json.js.map