You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
987 B
JavaScript

#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const iniFile = process.argv[2];
if (!iniFile) {
console.error("Usage: ini-to-json.js file.ini");
process.exit(1);
}
const system = path.basename(iniFile, path.extname(iniFile));
const lines = fs.readFileSync(iniFile, "utf8").split(/\r?\n/);
const result = {
[system]: {}
};
let section = null;
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith(";") || trimmed.startsWith("#")) {
continue;
}
const sectionMatch = trimmed.match(/^\[(.+)\]$/);
if (sectionMatch) {
section = sectionMatch[1];
result[system][section] ??= {};
continue;
}
if (!section) {
continue;
}
const eq = trimmed.indexOf("=");
if (eq === -1) {
continue;
}
const key = trimmed.slice(0, eq).trim();
const value = trimmed.slice(eq + 1).trim();
result[system][section][key] = value;
}
console.log(JSON.stringify(result, null, 2));