-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.js
103 lines (96 loc) · 2.3 KB
/
config.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// vim: set tw=99 ts=2 sw=2 et:
'use strict';
const Ajv = require('ajv');
const betterAjvErrors = require('better-ajv-errors');
const fs = require('fs-extra');
const jsonlint = require('jsonlint');
const configAjv = new Ajv({ jsonPointers: true });
configAjv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
const configSchema = {
type: 'object',
properties: {
targets: {
type: 'object',
additionalProperties: {
type: 'object',
properties: {
patterns: {
type: 'array',
items: {
type: 'string',
},
},
policy: {
type: 'object',
properties: {
include: {
type: 'array',
items: {
type: 'string',
},
},
exclude: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
},
additionalProperties: false,
},
},
graphs: {
type: 'array',
items: {
type: 'string',
},
},
code: {
type: 'string',
},
trace: {
type: 'string',
},
report: {
type: 'string',
},
bundle: {
type: 'object',
properties: {
rules: {
type: 'string',
},
resources: {
type: 'string',
},
},
required: ['rules', 'resources'],
additionalProperties: false,
},
},
additionalProperties: false,
};
exports.configSchema = configSchema;
const validateConfigImpl = configAjv.compile(configSchema);
const validateConfig = config => {
if (!validateConfigImpl(config)) {
throw new TypeError(
betterAjvErrors(configSchema, config, validateConfigImpl.errors, { indent: 2 })
);
}
};
exports.validateConfig = validateConfig;
const parseConfig = configSrc => {
const config = jsonlint.parse(configSrc);
validateConfig(config);
return config;
};
exports.parseConfig = parseConfig;
const parseConfigFile = async configFilePath => {
const configSrc = await fs.readFile(configFilePath, { encoding: 'utf8' });
return parseConfig(configSrc);
};
exports.parseConfigFile = parseConfigFile;