forked from martysweet/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ts
54 lines (39 loc) · 1.11 KB
/
parser.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
52
53
54
import yaml = require('js-yaml');
import fs = require('fs');
import buildYamlSchema from './yamlSchema';
const yamlSchema = buildYamlSchema();
export function openFile(path: string){
// Check the file path is valid
if (!fs.existsSync(path)) {
throw Error(`Could not find file ${path}. Check the input path.`);
}
// Try JSON loading
try {
return openJson(path);
}catch (e){
}
// Try YAML loading
try {
return openYaml(path);
}catch (e){
throw Error(`Could not determine file type. Check your template is not malformed. ${e.message}`);
}
};
function openYaml(path: string){
// Try and load the Yaml
let yamlParse = yaml.safeLoad(fs.readFileSync(path, 'utf8'), {
filename: path,
schema: yamlSchema,
onWarning: (warning) => {
console.error(warning);
}
});
if(typeof yamlParse == 'object'){
return yamlParse
}
// Yaml Parsing error
throw new Error("YAML could not be parsed.");
}
function openJson(path: string){
return JSON.parse(fs.readFileSync(path, 'utf8'));
}