@plugjs/plug
Version:
PlugJS Build System ===================
74 lines (72 loc) • 2.72 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// utils/jsonc.ts
var jsonc_exports = {};
__export(jsonc_exports, {
JsoncError: () => JsoncError,
parseJsonc: () => parseJsonc
});
module.exports = __toCommonJS(jsonc_exports);
var import_node_os = require("node:os");
var import_jsonc_parser = require("jsonc-parser");
var import_colors = require("../logging/colors.cjs");
var JsoncError = class extends Error {
constructor(errors) {
const message = [`Found ${(0, import_colors.$plur)(errors.length, "error", "errors", false)} parsing`];
for (const { code, line, column } of errors) {
message.push(` ${code} at line ${line}, column ${column}`);
}
super(message.join("\n"));
this.errors = errors;
}
};
function parseJsonc(data, options = {}) {
const { disallowComments = false, allowTrailingComma = true } = options;
if (!data) return void 0;
const errors = [];
const result = (0, import_jsonc_parser.parse)(data, errors, {
disallowComments,
allowTrailingComma,
allowEmptyContent: false
});
if (errors.length === 0) return result;
const offsets = data.split(import_node_os.EOL).reduce(({ offsets: offsets2, offset }, line) => {
offset += line.length + import_node_os.EOL.length;
offsets2.push(offset);
return { offsets: offsets2, offset };
}, { offset: 0, offsets: [0] }).offsets;
function resolveOffset(offset) {
for (let i = offsets.length - 1; i > 0; i--) {
const lineOffset = offsets[i];
if (offset < lineOffset) continue;
return { line: i + 1, column: offset - lineOffset + 1 };
}
return { line: 1, column: offset + 1 };
}
throw new JsoncError(errors.map((error) => {
const code = (0, import_jsonc_parser.printParseErrorCode)(error.error);
return { code, ...resolveOffset(error.offset) };
}));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
JsoncError,
parseJsonc
});
//# sourceMappingURL=jsonc.cjs.map