-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.ts
190 lines (160 loc) · 4.25 KB
/
config.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Rotten Deps configuration library
*
* @module
*/
import Table from 'cli-table3';
import { readFileSync } from 'fs';
interface Rule {
readonly dependencyName: string;
readonly ignore?: boolean;
readonly daysUntilExpiration?: number;
readonly reason?: string;
}
export interface Config {
defaultExpiration?: number;
readonly kind?: 'config';
readonly rules: Rule[];
}
interface FileReader {
(): Promise<string>;
}
interface Issue {
error: string;
recommendation: string;
}
interface Problem {
readonly dependencyName: string;
readonly issues: Issue[];
}
interface ParsedRules {
kind: 'rules';
rules: Rule[];
}
interface ErrorReport {
readonly kind: 'error';
readonly message: string;
readonly report: Problem[];
readonly error: Error;
}
type MaybeRules = ParsedRules | ErrorReport;
/**
* reviews each rule of a configuration to determine validity
* generates a report of violations with recommendations on how to resolve
* @param config rotten deps configuration object
*/
const parseRules = (config: Config): MaybeRules => {
const { rules } = config;
const problems: Problem[] = [];
const parsedRules: Rule[] = [];
let isValidConfig = true;
rules.forEach(({
dependencyName,
ignore = false,
daysUntilExpiration = 0,
reason = '',
}, i) => {
const ruleNumber = i + 1;
const rule = {
dependencyName,
ignore,
daysUntilExpiration,
reason,
};
let isRuleValid = true;
const issues: Issue[] = [];
if (typeof dependencyName !== 'string' || dependencyName.length <= 0) {
isRuleValid = false;
issues.push({
error: `Rule ${ruleNumber} is missing required field dependencyName`,
recommendation: 'You must provide a dependency name of type string',
});
}
if (typeof ignore !== 'boolean') {
isRuleValid = false;
issues.push({
error: `Rule ${ruleNumber} has a type mismatch for ignore`,
recommendation: 'The ignore field must be of type boolean',
});
}
if (typeof daysUntilExpiration !== 'number') {
isRuleValid = false;
issues.push({
error: `Rule ${ruleNumber} has a type mismatch for daysUntilExpiration`,
recommendation: 'The daysUntilExpiration field must be of type number',
});
}
if (typeof reason !== 'string') {
isRuleValid = false;
issues.push({
error: `Rule ${ruleNumber} has a type mismatch for the reason property`,
recommendation: 'The value for reason must be a string',
});
}
if (!isRuleValid) {
isValidConfig = false;
problems.push({
dependencyName,
issues,
});
} else {
parsedRules.push(rule);
}
});
if (!isValidConfig) {
return {
kind: 'error',
message: 'Configuration file contains invalid config',
report: problems,
error: new Error('Configuration file contains invalid config'),
};
}
return {
kind: 'rules',
rules: parsedRules,
};
};
/**
* Validates a raw configuration file and generates a report if any rules are
* misconfigured.
* @param config rotten deps configuration object
*/
export const createConfig = (config: Config): Config => {
const maybeRules = parseRules(config);
const table = new Table({
head: ['dependency', 'issue', 'recommendation'],
});
switch (maybeRules.kind) {
case 'error':
maybeRules.report.forEach(x => {
const dep = String(x.dependencyName);
x.issues.forEach(y => {
table.push([dep, y.error, y.recommendation]);
});
});
console.log(table.toString());
throw maybeRules.error;
case 'rules':
return {
kind: 'config',
...config,
rules: maybeRules.rules,
};
default:
throw new Error('Parsed rules was neither collection of rules or error');
}
};
/**
* Creates a filereader function for fetching the contents of a config
* file at the provided path.
* @param absoluteFilePath absolute path to the configuration file
*/
export const createFileReader = (absoluteFilePath: string): FileReader =>
async () => {
const data = readFileSync(absoluteFilePath, { encoding: 'utf-8' });
return String(data);
};
export default {
createFileReader,
createConfig,
};