-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (76 loc) · 2.3 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { getInput, setOutput, setFailed, info } from "@actions/core";
import { XMLValidator } from "fast-xml-parser";
import * as fs from "fs";
import * as paths from "path";
async function walk(dir) {
let files = fs.readdirSync(dir, { withFileTypes: true });
files = await Promise.all(
files.map(async (dirEnt) => {
const filePath = paths.join(dir, dirEnt.name); // dirEnt.path does not yet exist in nodejs 16.16.0
if (dirEnt.isDirectory()) {
return walk(filePath);
} else {
if (dirEnt.isFile()) {
return filePath;
}
}
})
);
return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
export async function validate(path, extensionsStr) {
const extensionsArray = (extensionsStr || ".xml")
.split(",")
.map((str) => str.trim().toLocaleUpperCase());
let fileCount = 0;
let outErrorStr = "";
if (path && path.charAt(0) === "/") {
path = path.substring(1);
}
await walk("./" + (path || "/")).then((files) =>
files
.filter((entry) => {
const filenameCaps = entry.toLocaleUpperCase();
const isMatch = extensionsArray.some((extension, index, array) =>
filenameCaps.endsWith(extension)
);
return isMatch;
})
.every((file) => {
const xmlData = fs.readFileSync(file, "utf8");
const result = XMLValidator.validate(xmlData, {
allowBooleanAttributes: false,
});
if (result !== true) {
outErrorStr = `file '${file}', colum ${result.err.col}, line ${result.err.line}: ${result.err.code} - ${result.err.msg}`;
return false;
}
++fileCount;
info("validated " + file);
return true;
})
);
return { fileCount, outErrorStr };
}
// execute action
try {
const path = getInput("path");
const fileEndingsStr = getInput("file-endings");
info(
"Running nodeJS " +
process.version +
". path=" +
path +
", file-endings=" +
fileEndingsStr
);
const result = await validate(path, fileEndingsStr);
if (result.outErrorStr != "") {
setOutput("result", result.outErrorStr);
setFailed(result.outErrorStr);
} else {
setOutput("result", `Successfully validated ${result.fileCount} files.`);
}
} catch (error) {
setFailed(error.message);
}