json-smart-repair
Version:
A smart tool to repair malformed JSON strings, making them valid and parseable.
111 lines (109 loc) • 3.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.repairJson = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const repair_engine_1 = require("./repair-engine");
Object.defineProperty(exports, "repairJson", { enumerable: true, get: function () { return repair_engine_1.repairJson; } });
// Utility: read from stdin
function readStdin() {
return new Promise((resolve) => {
let data = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => (data += chunk));
process.stdin.on("end", () => resolve(data));
});
}
async function main() {
const args = process.argv.slice(2);
const inputArg = args[0];
// Handle version flag
if (args.includes("--version") || args.includes("-v")) {
const pkgPath = path_1.default.resolve(__dirname, "../package.json");
const pkg = JSON.parse(fs_1.default.readFileSync(pkgPath, "utf8"));
console.log(`json-smart-repair v${pkg.version}`);
process.exit(0);
}
// Handle help flag
if (args.includes("--help") || args.includes("-h") || !inputArg) {
console.log(`
JSON Smart Repair 🧩
---------------------
Usage:
json-smart-repair <input-file> [options]
cat broken.json | json-smart-repair > fixed.json
echo '{ id: 1, name: "John" age: 30 }' | json-smart-repair
Options:
-o, --output <file> Write output to file instead of stdout
-s, --silent Suppress warnings
-v, --version Show CLI version
-h, --help Show this help message
`);
process.exit(0);
}
// Determine input source
let input = "";
if (inputArg === "-") {
input = await readStdin();
}
else {
try {
input = fs_1.default.readFileSync(inputArg, "utf8");
}
catch (err) {
console.error(`❌ Cannot read file: ${inputArg}`);
process.exit(2);
}
}
if (!input || input.trim() === "") {
console.error("⚠️ No input provided. Provide a file or pipe JSON via stdin.");
process.exit(1);
}
// Repair JSON
let repaired = "";
try {
repaired = (0, repair_engine_1.repairJson)(input);
}
catch (err) {
console.error("❌ Failed to repair JSON:", err.message);
process.exit(3);
}
// Try to prettify if valid
let outputText = repaired;
try {
const parsed = JSON.parse(repaired);
outputText = JSON.stringify(parsed, null, 2);
}
catch {
if (!args.includes("--silent") && !args.includes("-s")) {
console.warn("⚠️ Warning: output may still not be valid JSON.");
}
}
// Output
const outputIndex = args.findIndex((a) => a === "-o" || a === "--output");
if (outputIndex !== -1 && args[outputIndex + 1]) {
const outFile = args[outputIndex + 1];
try {
fs_1.default.writeFileSync(outFile, outputText, "utf8");
console.log(`✅ Fixed JSON written to ${outFile}`);
}
catch (err) {
console.error(`❌ Failed to write output file: ${err.message}`);
process.exit(4);
}
}
else {
process.stdout.write(outputText);
}
}
// Only run if called directly from CLI
if (require.main === module) {
main().catch((err) => {
console.error("💥 Fatal error:", err);
process.exit(1);
});
}