jsonfiver
Version:
JSON fixer for human using JSON5
66 lines • 2.45 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const JSON5 = require("json5");
function readStdin() {
process.stdin.setEncoding('utf8');
return new Promise(resolve => {
let buf = '';
process.stdin.on('data', chunk => {
buf += chunk;
});
process.stdin.on('end', () => {
resolve(buf);
});
});
}
class JsonFiver {
constructor(config) {
this.config = Object.assign({ write: false, indent: 2, minify: false }, config || {});
}
run(paths) {
return __awaiter(this, void 0, void 0, function* () {
const stdin = paths.length === 0;
if (stdin) {
const obj = yield this.requireStdin();
process.stdout.setEncoding('utf8');
this.writeAsJson(process.stdout, obj);
}
else {
throw new Error('Fixing file is not implemented yet');
}
});
}
runString(input) {
return __awaiter(this, void 0, void 0, function* () {
const obj = JSON5.parse(input);
process.stdout.setEncoding('utf8');
this.writeAsJson(process.stdout, obj);
});
}
writeAsJson(writer, obj) {
return __awaiter(this, void 0, void 0, function* () {
const { indent, minify } = this.config;
const level = minify ? 0 : indent || 2;
writer.write(JSON.stringify(obj, null, level) + '\n');
});
}
requireStdin() {
return __awaiter(this, void 0, void 0, function* () {
return JSON5.parse(yield readStdin());
});
}
}
exports.default = JsonFiver;
function convert(input, config) {
return new JsonFiver(config).runString(input);
}
exports.convert = convert;
//# sourceMappingURL=index.js.map