-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemstone-linter-yaml.js
67 lines (61 loc) · 2.23 KB
/
gemstone-linter-yaml.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
/*
** GemstoneJS -- Gemstone JavaScript Technology Stack
** Copyright (c) 2016-2019 Gemstone Project <http://gemstonejs.com>
** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>
*/
/* load external requirements */
const path = require("path")
const fs = require("mz/fs")
const jsYAML = require("js-yaml")
/* exported API function */
module.exports = async function (filenames, opts = {}, report = { sources: {}, findings: [] }) {
/* interate over all source files */
let passed = true
if (typeof opts.progress === "function")
opts.progress(0.0, "linting YAML: starting")
for (let i = 0; i < filenames.length; i++) {
/* indicate progress */
if (typeof opts.progress === "function")
opts.progress(i / filenames.length, `linting YAML: ${filenames[i]}`)
/* read source code */
const source = await fs.readFile(filenames[i], "utf8")
/* determine name */
const name = path.relative(process.cwd(), filenames[i])
/* lint the source code */
try {
jsYAML.safeLoad(source, {
filename: name,
schema: jsYAML.DEFAULT_SAFE_SCHEMA,
onWarning: (warning) => {
report.findings.push({
ctx: "YAML",
filename: name,
line: warning.mark.line,
column: warning.mark.column,
message: warning.reason,
ruleProc: "jsYAML",
ruleId: "*"
})
passed = false
report.sources[name] = source
}
})
}
catch (ex) {
report.findings.push({
ctx: "YAML",
filename: name,
line: ex.mark.line,
column: ex.mark.column,
message: ex.reason,
ruleProc: "jsYAML",
ruleId: "*"
})
report.sources[name] = source
passed = false
}
}
if (typeof opts.progress === "function")
opts.progress(1.0, "")
return passed
}