-
Notifications
You must be signed in to change notification settings - Fork 8
/
valid.js
59 lines (48 loc) · 1.53 KB
/
valid.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
const chalk = require('chalk');
const Check = require('./check');
/**
* This checks if known files have a valid format
*/
class validCheck extends Check {
constructor(connector, reporter) {
super(connector, reporter);
}
async check(context, args) {
let results = [];
for (let arg of args) {
const type = Object.keys(arg)[0]; // json, yaml, ...
const filePath = arg[type];
// json files
if (type === 'json') {
// try to read the file
const readFile = (await this.connector.exec(`cat ${filePath}`));
let valid;
// if was able to read the file
if (readFile.exitCode == 0)
valid = this._validateJSON(readFile.stdout);
else
valid = readFile.stderr.includes('No such file') ? 'No such file or directory' : readFile.stderr;
results.push({ type, filePath, valid });
}
}
return results;
}
async report(results) {
results.forEach((result) => {
let message = chalk`{gray [${result.filePath}]} {white valid:} {gray ${result.valid}}`;
this.reporter.report(message, result.valid === true);
});
}
_validateJSON(JSONText) {
try {
JSON.parse(JSONText);
return true;
} catch (err) {
return false;
}
}
_validateYAML(YAMLText) {
// TODO
}
}
module.exports = validCheck;