-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathconfiguration.js
More file actions
175 lines (146 loc) · 4.42 KB
/
configuration.js
File metadata and controls
175 lines (146 loc) · 4.42 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import path from 'path';
import JSONFormatter from './formatters/json_formatter.js';
import TextFormatter from './formatters/text_formatter.js';
import CompactFormatter from './formatters/compact_formatter.js';
import expandPaths from './util/expandPaths.js';
export class Configuration {
/*
options:
- format: (required) `text` | `json`
- rules: [string array] whitelist rules
- customRulePaths: [string array] path to additional custom rules to be loaded
- commentDescriptions: [boolean] use old way of defining descriptions in GraphQL SDL
- oldImplementsSyntax: [boolean] use old way of defining implemented interfaces in GraphQL SDL
*/
constructor(schema, options = {}) {
const defaultOptions = {
format: 'text',
customRulePaths: [],
commentDescriptions: false,
oldImplementsSyntax: false,
};
this.schema = schema;
this.options = { ...defaultOptions, ...options };
this.rules = null;
this.builtInRulePaths = path.join(__dirname, 'rules/*.js');
this.rulePaths = this.options.customRulePaths.concat(this.builtInRulePaths);
}
getCommentDescriptions() {
return this.options.commentDescriptions;
}
getOldImplementsSyntax() {
return this.options.oldImplementsSyntax;
}
getSchema() {
return this.schema.definition;
}
getSchemaSourceMap() {
return this.schema.sourceMap;
}
getFormatter() {
switch (this.options.format) {
case 'json':
return JSONFormatter;
case 'text':
return TextFormatter;
case 'compact':
return CompactFormatter;
}
}
getRules() {
let rules = this.getAllRules();
let specifiedRules;
if (this.options.rules && this.options.rules.length > 0) {
specifiedRules = this.options.rules.map(toUpperCamelCase);
rules = this.getAllRules().filter(rule => {
return specifiedRules.indexOf(rule.name) >= 0;
});
}
// DEPRECATED - This code should be removed in v1.0.0.
if (this.options.only && this.options.only.length > 0) {
specifiedRules = this.options.only.map(toUpperCamelCase);
rules = this.getAllRules().filter(rule => {
return specifiedRules.indexOf(rule.name) >= 0;
});
}
// DEPRECATED - This code should be removed in v1.0.0.
if (this.options.except && this.options.except.length > 0) {
specifiedRules = this.options.except.map(toUpperCamelCase);
rules = this.getAllRules().filter(rule => {
return specifiedRules.indexOf(rule.name) == -1;
});
}
return rules;
}
getAllRules() {
if (this.rules !== null) {
return this.rules;
}
this.rules = this.getRulesFromPaths(this.rulePaths);
return this.rules;
}
getRulesFromPaths(rulePaths) {
const expandedPaths = expandPaths(rulePaths);
const rules = new Set([]);
expandedPaths.map(rulePath => {
let ruleMap = require(rulePath);
Object.keys(ruleMap).forEach(k => rules.add(ruleMap[k]));
});
return Array.from(rules);
}
getAllBuiltInRules() {
return this.getRulesFromPaths([this.builtInRulePaths]);
}
validate() {
const issues = [];
let rules;
try {
rules = this.getAllRules();
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
issues.push({
message: `There was an issue loading the specified custom rules: '${
e.message.split('\n')[0]
}'`,
field: 'custom-rule-paths',
type: 'error',
});
rules = this.getAllBuiltInRules();
} else {
throw e;
}
}
const ruleNames = rules.map(rule => rule.name);
let misConfiguredRuleNames = []
.concat(
this.options.only || [],
this.options.except || [],
this.options.rules || []
)
.map(toUpperCamelCase)
.filter(name => ruleNames.indexOf(name) == -1);
if (this.getFormatter() == null) {
issues.push({
message: `The output format '${this.options.format}' is invalid`,
field: 'format',
type: 'error',
});
}
if (misConfiguredRuleNames.length > 0) {
issues.push({
message: `The following rule(s) are invalid: ${misConfiguredRuleNames.join(
', '
)}`,
field: 'rules',
type: 'warning',
});
}
return issues;
}
}
function toUpperCamelCase(string) {
return string
.split('-')
.map(part => part[0].toUpperCase() + part.slice(1))
.join('');
}