@lodestar/prover
Version:
A Typescript implementation of the Ethereum Consensus light client
49 lines • 1.59 kB
JavaScript
import fs from "node:fs";
import path from "node:path";
import yaml from "js-yaml";
const { load, FAILSAFE_SCHEMA, Type } = yaml;
var FileFormat;
(function (FileFormat) {
FileFormat["json"] = "json";
FileFormat["yaml"] = "yaml";
FileFormat["yml"] = "yml";
FileFormat["toml"] = "toml";
})(FileFormat || (FileFormat = {}));
const yamlSchema = FAILSAFE_SCHEMA.extend({
implicit: [
new Type("tag:yaml.org,2002:str", {
kind: "scalar",
construct: function construct(data) {
return data !== null ? data : "";
},
}),
],
});
/**
* Parse file contents as Json.
*/
function parse(contents, fileFormat) {
switch (fileFormat) {
case FileFormat.json:
return JSON.parse(contents);
case FileFormat.yaml:
case FileFormat.yml:
return load(contents, { schema: yamlSchema });
default:
return contents;
}
}
/**
* Read a JSON serializable object from a file
*
* Parse either from json, yaml, or toml
* Optional acceptedFormats object can be passed which can be an array of accepted formats, in future can be extended to include parseFn for the accepted formats
*/
export function readFile(filepath, acceptedFormats) {
const fileFormat = path.extname(filepath).substr(1);
if (acceptedFormats && !acceptedFormats.includes(fileFormat))
throw new Error(`UnsupportedFileFormat: ${filepath}`);
const contents = fs.readFileSync(filepath, "utf-8");
return parse(contents, fileFormat);
}
//# sourceMappingURL=file.js.map