2025-08-02 02:17:27 +07:00
|
|
|
// a basic drop-in replacement for jq to handle HJSON
|
|
|
|
|
// tries to preserve comments but doesn't do a very good job :(
|
|
|
|
|
|
2025-08-02 05:10:30 +07:00
|
|
|
import Hjson from "hjson";
|
|
|
|
|
import jqModule from "jq-web";
|
2025-08-02 02:17:27 +07:00
|
|
|
import fs from "node:fs";
|
|
|
|
|
|
2025-08-02 05:10:30 +07:00
|
|
|
export const n = 42;
|
|
|
|
|
|
2025-08-02 02:17:27 +07:00
|
|
|
const jq = await jqModule;
|
|
|
|
|
|
|
|
|
|
var filter = process.argv[2];
|
|
|
|
|
if (!filter) {
|
|
|
|
|
filter = '.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const input = fs.readFileSync(0, 'utf8');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const data = Hjson.parse(input, { keepWsc: true });
|
|
|
|
|
|
|
|
|
|
const result = jq.json(data, filter);
|
|
|
|
|
|
|
|
|
|
// Only try to preserve comments if the result is an object or array
|
|
|
|
|
if (result !== null && typeof result === 'object') {
|
|
|
|
|
// Extract comments before transformation
|
|
|
|
|
const comments = Hjson.comments.extract(data);
|
|
|
|
|
|
|
|
|
|
// Merge comments back into the transformed result
|
|
|
|
|
if (comments) {
|
|
|
|
|
Hjson.comments.merge(comments, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const output = Hjson.stringify(result, {
|
|
|
|
|
keepWsc: true,
|
|
|
|
|
bracesSameLine: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(output);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(JSON.stringify(result));
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|