-
Notifications
You must be signed in to change notification settings - Fork 4
/
logger.js
44 lines (36 loc) · 1.5 KB
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* logger.js
*
* log stuff from the validator
*/
import chalk from "chalk";
import { existsSync, writeFile } from "fs";
import { join, sep } from "path";
import { MODE_URL, MODE_SL } from "./ui.js";
export function createPrefix(req) {
const logDir = join(".", "arch");
if (!existsSync(logDir)) return null;
const getDate = (d) => {
const fillZero = (t) => (t < 10 ? `0${t}` : t);
return `${d.getFullYear()}-${fillZero(d.getMonth() + 1)}-${fillZero(d.getDate())} ${fillZero(d.getHours())}.${fillZero(d.getMinutes())}.${fillZero(d.getSeconds())}`;
};
const fname = req.body.doclocation == MODE_URL ? req.body.XMLurl.substr(req.body.XMLurl.lastIndexOf("/") + 1) : req?.files?.XMLfile?.name;
if (!fname) return null;
return `${logDir}${sep}${getDate(new Date())} (${req.body.testtype == MODE_SL ? "SL" : req.body.requestType}) ${fname.replace(/[/\\?%*:|"<>]/g, "-")}`;
}
export default function writeOut(errs, filebase, markup, req = null) {
if (!filebase || errs.markupXML?.length == 0) return;
let outputLines = [];
if (markup && req?.body?.XMLurl) outputLines.push(`<!-- source: ${req.body.XMLurl} -->`);
errs.markupXML.forEach((line) => {
outputLines.push(line.value);
if (markup && line.validationErrors)
line.validationErrors.forEach((error) => {
outputLines.push(`<!--${error.replace(/[\n]/g, "")}-->`);
});
});
const filename = markup ? `${filebase}.mkup.txt` : `${filebase}.raw.txt`;
writeFile(filename, outputLines.join("\n"), (err) => {
if (err) console.log(chalk.red(err));
});
}