This repository has been archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
yaml-parse.ts
51 lines (48 loc) · 1.63 KB
/
yaml-parse.ts
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
import * as Joi from 'joi';
import * as yaml from 'js-yaml';
import * as fs from 'fs';
import * as path from 'path';
import { writeErrorMessage } from './handler';
import { Schema as joiSchema } from './configSchema';
/**
* Read and parse the test files to JSON
* @param pathArray Array of the test-config files
* @param dir Directory for execution
*/
export const parseTestingFiles = (pathArray: string[], dir: string) => {
let responseData: object[] = [];
pathArray.map((filePath: any) => {
try {
if(typeof filePath === 'string') {
const data = fs.readFileSync(filePath.toString(), 'utf8');
const parsed: any = yaml.safeLoad(data);
const { name:fileName } = path.parse(filePath);
const removeFilename = path.dirname(filePath) + path.sep;
if(dir === null) {
dir = '';
}
parsed.raw = data.replace(/(\rn|\n|\r)/g, '\n')
parsed.fileName = fileName;
parsed.relativePath = removeFilename.replace(path.join(process.cwd(), dir), '.' + path.sep);
responseData.push(parsed);
}
} catch(e) {
writeErrorMessage(`An error occured while parsing ${path.relative(process.cwd(), filePath)}`);
writeErrorMessage(e);
}
})
return responseData;
}
export const validateSchema = (testSettings: object[]) => {
let proofedSettings: object[] = [];
let errors: Joi.ValidationError[] = [];
testSettings.map((fileSetting) => {
const test = Joi.validate(fileSetting, joiSchema);
if(test.error === null) {
proofedSettings.push(fileSetting);
} else {
errors.push(test.error);
}
});
return { proofedSettings, errors};
}