Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding valid check (JSON validation)
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |