unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
42 lines • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const yaml = require("js-yaml");
const path = require("path");
const utils_1 = require("../utils");
const fileSizeLimitOnInit = 5 * Math.pow(1024, 2);
class FSLogger {
constructor({ directory, filename = "unmock.log", }) {
const absPath = directory || utils_1.resolveUnmockRootDirectory();
if (!(fs.existsSync(absPath) && fs.statSync(absPath).isDirectory())) {
this.enabled = false;
return;
}
this.enabled = true;
this.targetFile = path.join(absPath, filename);
if (!fs.existsSync(this.targetFile) ||
fs.statSync(this.targetFile).size > fileSizeLimitOnInit) {
fs.writeFileSync(this.targetFile, "### Request-Response log generated by unmock\n\n");
}
}
static toIndentedYaml(input) {
return yaml
.dump(input)
.split("\n")
.map((line) => `\t${line.replace("!<tag:yaml.org,2002:js/undefined> ''", "")}`)
.join("\n");
}
notify({ req, res, }) {
if (!this.enabled) {
return;
}
fs.appendFileSync(this.targetFile, `[${new Date().toISOString()}]:\n\n` +
`Intercepted request:\n${FSLogger.toIndentedYaml(req)}\n\n\n` +
(res
? `Generated response:\n${FSLogger.toIndentedYaml(res)}`
: "No matching response") +
`\n\n=============================================\n\n`);
}
}
exports.default = FSLogger;
//# sourceMappingURL=filesystem-logger.js.map