-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepoConfig.js
321 lines (267 loc) · 9.59 KB
/
repoConfig.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"use strict";
var Promise = require("bluebird");
var _ = require('lodash');
var jsonWithComments = _.compose(JSON.parse, require("strip-json-comments"));
var Validator = require('jsonschema').Validator;
var gitignoreParser = require("gitignore-parser");
const EventEmitter = require('events').EventEmitter;
var fs = Promise.promisifyAll(require('fs'));
var path = require('path');
var jsonSchema = require('./repoConfig/SidekickSchema.json');
var files = require('./files');
//build up analysers in config - start with non language specific analysers
const DEFAULT_CONFIG = require('./repoConfig/default_sidekickrc.json');
var CONFIG_FILENAME = '.sidekickrc';
// TODO add gitignore to excludes
exports.events = new EventEmitter;
/**
* Load config data from .sidekickrc file
* @param repoPath the abs file path of the config file.
*/
exports.load = function(repoPath) /*: RepoConfig */ {
var filePath = path.join(repoPath, CONFIG_FILENAME);
return fs.readFileAsync(filePath, {encoding: "utf8"})
.then(exports.fromString, function(){
exports.events.emit('message', 'No .sidekickrc file found in this repo.');
exports.events.emit('message', 'Will run default analysers (security and dependencies).');
exports.events.emit('message', 'Parsing repo contents to determine which other analysers to run..');
return getDefault(repoPath)
});
};
/**
* Save the .sidekickrc file and commit the modifications
* @param repoPath the abs path to the repo containing the .sidekickrc file
* @param contents the contents to save
*/
exports.save = function(repoPath, contents) {
var filePath = path.join(repoPath, CONFIG_FILENAME);
var contentsAsString = JSON.stringify(contents, null, 4);
return fs.writeFile(filePath, contentsAsString)
};
exports.fromString = (content) => RepoConfig(parse(content || "{}"));
// value object - immutable
function RepoConfig(conf /*: RawConfig */) {
conf = _.extend({
exclude: [],
languages: {},
}, conf);
return {
includedPaths(paths, language) {
// TODO probably faster to | together, not sure right now
var excludes = gitignoreParser.compile(conf.exclude.join("\n"));
var languageRe = getLanguageFileRe(language);
return paths.filter(checkPath);
function checkPath(path) {
if(!languageRe.test(path)) {
return false;
}
return excludes.accepts(path);
}
},
analyserFailsCi(analyser) {
return Boolean(analyser.failCiOnError);
},
languages() {
return _.keys(conf.languages);
},
analysers(lang) {
return conf.languages[lang] || [];
},
analysersByLanguages() {
return conf.languages;
},
allAnalysers(){
return _.flatten(_.values(conf.languages));
},
getContents(){
let copyConf = _.cloneDeep(conf);
//analysers are stored in an array (per language). These need to be converted into kv before returning
_.each(copyConf.languages, function(analysersForLang, lang){
if(analysersForLang.length){
let obj = {}
_.each(analysersForLang, function(analyser){
obj[analyser.name] = {"failCiOnError" : analyser.failCiOnError};
});
copyConf.languages[lang] = obj;
}
});
return JSON.stringify(copyConf);
},
};
}
// if we're missing a .sidekickrc, we do the 'right thing' as far as possible
function getDefault(repoPath) /*: RawConfig */ {
var defaultConfig = _.cloneDeep(DEFAULT_CONFIG);
if(repoHasFilesOfType(repoPath, '.js')){
exports.events.emit('message', 'JavaScript files found in this repo.');
doJs();
}
if(repoHasFilesOfType(repoPath, '.ts')){
exports.events.emit('message', 'TypeScript files found in this repo.');
doTs();
}
if(repoHasFilesOfType(repoPath, '.cs')){
exports.events.emit('message', 'CoffeeScript files found in this repo.');
doCs();
}
const parsedDefaultConfig = parse(JSON.stringify(defaultConfig)); //so that analysers get mutated
return RepoConfig(parsedDefaultConfig);
function repoHasFilesOfType(repoPath, type){
return files.findFilesInDir(repoPath, type).length > 0;
}
/**
* Add Javascript analysers if we find corresponding config files
*/
function doJs(){
defaultConfig.languages.js = {};
doTodos();
doJscs();
doEsLint();
doJsHint();
//add js-todos
function doTodos(){
exports.events.emit('message', ' Adding js-todos analyser.');
defaultConfig.languages.js['sidekick-js-todos'] = {failCiOnError: false};
}
//add jscs
function doJscs(){
try {
fs.statSync(path.join(repoPath, '/.jscsrc'));
exports.events.emit('message', ' jscs config file found - adding jscs analyser.');
defaultConfig.languages.js['sidekick-jscs'] = {failCiOnError: false};
}catch(e){
exports.events.emit('message', ' jscs config file not found. Will not run jscs.');
}
}
//add eslint if we find any eslint config
function doEsLint(){
if(isEsLintConfig(repoPath)){
exports.events.emit('message', ' eslint config file found - adding eslint analyser.');
defaultConfig.languages.js['sidekick-eslint'] = {failCiOnError: true};
} else {
exports.events.emit('message', ' eslint config file not found. Will not run eslint.');
}
}
function doJsHint(){
try {
fs.statSync(path.join(repoPath, '/.jshintrc'));
exports.events.emit('message', ' jshint config file found - adding jshint analyser.');
defaultConfig.languages.js['sidekick-jshint'] = {failCiOnError: true};
}catch(e){
exports.events.emit('message', ' jshint config file not found. Will not run jshint.');
}
}
}
function doTs(){
defaultConfig.languages.ts = {};
doTsLint();
//add tslint if we find any tslint config
function doTsLint(){
try {
fs.statSync(path.join(repoPath, '/tsconfig.json'));
exports.events.emit('message', ' tslint config file found - adding tslint analyser.');
defaultConfig.languages.ts['sidekick-tslint'] = {failCiOnError: true};
}catch(e){
exports.events.emit('message', ' tslint config file not found. Will not run tslint.');
}
}
}
function doCs(){
defaultConfig.languages.cs = {};
doCsLint();
//add cslint if we find any cslint config
function doCsLint(){
try {
fs.statSync(path.join(repoPath, '/coffeelint.json'));
exports.events.emit('message', ' coffeelint config file found - adding coffeelint analyser.');
defaultConfig.languages.cs['sidekick-coffeelint'] = {failCiOnError: true};
}catch(e){
exports.events.emit('message', ' coffeelint config file not found. Will not run coffeelint.');
}
}
}
//TODO get list of analysers from HTTP
//TODO get list of known config files and create analyser entries if required
//TODO get list of analyser ignores and add
}
exports._eslintConfigExists = isEsLintConfig;
function isEsLintConfig(repoPath){
const CONFIG_FILES = [
".eslintrc.js",
".eslintrc.yaml",
".eslintrc.yml",
".eslintrc.json",
".eslintrc",
"package.json"
];
return getFilenameForDirectory(repoPath) !== null;
/**
* Retrieves the configuration filename for a given directory. It loops over all
* of the valid configuration filenames in order to find the first one that exists.
* @param {string} directory The directory to check for a config file.
* @returns {?string} The filename of the configuration file for the directory
* or null if there is no configuration file in the directory.
*/
function getFilenameForDirectory(directory) {
var filename;
for (var i = 0, len = CONFIG_FILES.length; i < len; i++) {
filename = path.join(directory, CONFIG_FILES[i]);
if (fs.existsSync(filename)) {
if(_.endsWith(filename, 'package.json')){
const config = jsonWithComments(fs.readFileSync(filename, {encoding: 'utf8'}));
return config.eslintConfig ? filename : null;
} else {
return filename;
}
}
}
return null;
}
}
function parse(string) {
if(typeof string !== "string") {
throw Error("need JSON config, not '" + string + "'");
}
const raw = _.defaults(jsonWithComments(string), {
exclude: [],
languages: {},
});
var validator = new Validator();
var validationResult = validator.validate(raw, jsonSchema);
if(validationResult.errors.length > 0){
throw Error('invalid .sidekickrc: ' + JSON.stringify(validationResult.errors, null, 4));
}
/*json schema currently does not validate the format of additional properties, so do extra validation on the
contents of a language - should be objects with a single key (analyserName) and a single value (object)
*/
var throwaway = RepoConfig(raw);
var analyserNotObj = _.some(throwaway.allAnalysers(), function(analyser){
var notEmpty = _.keys(analyser).length > 0;
if(notEmpty){
var details = analyser[_.keys(analyser)[0]];
return !_.isPlainObject(details);
} else {
return false;
}
});
if(analyserNotObj){
throw Error('Analysers must be objects');
}
return reformat(raw);
}
function reformat(content) {
return _.defaults({
languages: _.mapValues(content.languages, function(analysers) {
return _.map(analysers, function(config, name) {
return _.defaults({ name }, config);
})
}),
}, content);
}
function getLanguageFileRe(language) {
if(language === 'all'){
return new RegExp("\\.*$"); //we want to see all files for analysers for language 'all'
} else {
return new RegExp("\\." + language + "$");
}
}