ts-packager
Version:
TypeScript npm packager
52 lines • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.overrideJson = exports.writeJson = exports.readJson = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
/**
* Read a JSON file
*
* @param dir Input directory
* @param filename Input filename
* @returns Returns the contents as an object
*/
function readJson(dir, filename) {
const absolute = (0, path_1.join)(dir, filename);
const file = (0, fs_1.readFileSync)(absolute).toString();
return JSON.parse(file);
}
exports.readJson = readJson;
/**
* Write a JSON file
*
* @param dir Output directory
* @param filename Output filename
* @param data Contents as an object
*/
function writeJson(dir, filename, data) {
const absolute = (0, path_1.join)(dir, filename);
(0, fs_1.writeFileSync)(absolute, JSON.stringify(data, null, '\t'));
}
exports.writeJson = writeJson;
/**
* Override JSON values
*
* @param data Original object
* @param overrides Overrides
* @returns Returns overrided values
*/
function overrideJson(data, overrides) {
const out = Object.assign(Object.assign({}, data), overrides);
for (const key in out) {
const value = out[key];
if (typeof value === 'function') {
out[key] = value(key, data);
}
else if (value === undefined) {
delete out[key];
}
}
return out;
}
exports.overrideJson = overrideJson;
//# sourceMappingURL=index.js.map