promisified-properties
Version:
Handle .properties file via promisified and typed API
61 lines (60 loc) • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.write = exports.stringify = exports.parse = exports.parseFile = void 0;
const fs_1 = require("fs");
const escape_1 = require("./escape");
const parser_1 = require("./parser");
/**
* Parse the file pointed by the given path as [the Properties defined by Java](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html).
*
* @param path - Path to the target file to parse
* @returns Promise which returns parsed properties
*/
async function parseFile(path) {
return fs_1.promises.readFile(path, { encoding: "utf8" }).then((s) => {
return (0, parser_1.parse)(s);
});
}
exports.parseFile = parseFile;
/**
* Parse given text as [the Properties defined by Java](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html).
*
* @param data - Text to parse
* @returns Promise which returns parsed properties
*/
function parse(data) {
return (0, parser_1.parse)(data);
}
exports.parse = parse;
/**
* Convert properties data to text, based on [the spec defined by Java](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html).
* @param data - Properties data to convert into text format
* @returns Converted text which represents the given properties
*/
function stringify(data) {
let result = "";
data.forEach((line) => {
if ("text" in line) {
result += line.text + "\n";
}
else {
result +=
(0, escape_1.escape)((0, escape_1.escapeKey)(line.key)) + " = " + (0, escape_1.escape)(line.value ?? "") + "\n";
}
});
return result;
}
exports.stringify = stringify;
/**
* Convert properties data to text, based on [the spec defined by Java](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html),
* and write it to the file pointed by the given path.
* @param data - Properties data to convert into text format
* @param path - Path to the target file to write
* @returns Promise which is resolved when file is successfully written
*/
function write(data, path) {
return fs_1.promises.writeFile(path, stringify(data), {
encoding: "utf8",
});
}
exports.write = write;