Skip to content

Commit 4592a87

Browse files
committed
fix: repoConfig now creates useful defaults when no .sidekickrc file is found. Non language specific analysers are added by default. Language specific analysers are added if the repo contains the lanaguage in question. Analysers that require a config file, e.g. .eslintrc, are only added if the config file is found in the repo. Only lint analysers are specified to fail a ci build. Fixes sidekickcode/tracker#216.
1 parent 7678c5d commit 4592a87

File tree

4 files changed

+104
-15
lines changed

4 files changed

+104
-15
lines changed

repoConfig.js

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var eslintConfigLoader = require('sidekick-eslint/configLoader');
1313

1414
var jsonSchema = require('./repoConfig/SidekickSchema.json');
1515
var files = require('./files');
16+
//build up analysers in config - start with non language specific analysers
17+
const DEFAULT_CONFIG = require('./repoConfig/default_sidekickrc.json');
1618

1719
var CONFIG_FILENAME = '.sidekickrc';
1820

@@ -93,20 +95,18 @@ function RepoConfig(conf /*: RawConfig */) {
9395
// if we're missing a .sidekickrc, we do the 'right thing' as far as possible
9496
function getDefault(repoPath) /*: RawConfig */ {
9597

96-
//build up analysers in config - start with non language specific analysers
97-
var defaultConfig = require('./repoConfig/default_sidekickrc.json');
98-
98+
var defaultConfig = _.cloneDeep(DEFAULT_CONFIG);
9999
if(repoHasFilesOfType(repoPath, '.js')){
100100
doJs();
101101
}
102102

103-
/* if(repoHasFilesOfType(repoPath, '.ts')){
103+
if(repoHasFilesOfType(repoPath, '.ts')){
104104
doTs();
105-
}*/
105+
}
106106

107-
/* if(repoHasFilesOfType(repoPath, '.cs')){
107+
if(repoHasFilesOfType(repoPath, '.cs')){
108108
doCs();
109-
}*/
109+
}
110110

111111
return RepoConfig(defaultConfig);
112112

@@ -121,6 +121,7 @@ function getDefault(repoPath) /*: RawConfig */ {
121121
defaultConfig.languages.js = {};
122122

123123
doTodos();
124+
doJscs();
124125
doEsLint();
125126
doJsHint();
126127

@@ -129,6 +130,11 @@ function getDefault(repoPath) /*: RawConfig */ {
129130
defaultConfig.languages.js['sidekick-js-todos'] = {failCiOnError: false};
130131
}
131132

133+
//add jscs
134+
function doJscs(){
135+
defaultConfig.languages.js['sidekick-jscs'] = {failCiOnError: false};
136+
}
137+
132138
//add eslint if we find any eslint config
133139
function doEsLint(){
134140
if(eslintConfigLoader.hasConfigFile(repoPath)){
@@ -137,9 +143,38 @@ function getDefault(repoPath) /*: RawConfig */ {
137143
}
138144

139145
function doJsHint(){
140-
if(fs.statSync(path.join(repoPath, '/.jshintrc'))){
146+
try {
147+
fs.statSync(path.join(repoPath, '/.jshintrc'));
141148
defaultConfig.languages.js['sidekick-jshint'] = {failCiOnError: true};
142-
}
149+
}catch(e){}
150+
}
151+
}
152+
153+
function doTs(){
154+
defaultConfig.languages.ts = {};
155+
156+
doTsLint();
157+
158+
//add tslint if we find any tslint config
159+
function doTsLint(){
160+
try {
161+
fs.statSync(path.join(repoPath, '/tsconfig.json'));
162+
defaultConfig.languages.ts['sidekick-tslint'] = {failCiOnError: true};
163+
}catch(e){}
164+
}
165+
}
166+
167+
function doCs(){
168+
defaultConfig.languages.cs = {};
169+
170+
doCsLint();
171+
172+
//add cslint if we find any cslint config
173+
function doCsLint(){
174+
try {
175+
fs.statSync(path.join(repoPath, '/coffeelint.json'));
176+
defaultConfig.languages.cs['sidekick-coffeelint'] = {failCiOnError: true};
177+
}catch(e){}
143178
}
144179
}
145180

@@ -180,7 +215,6 @@ function parse(string) {
180215
if(analyserNotObj){
181216
throw Error('Analysers must be objects');
182217
}
183-
184218
return reformat(raw);
185219
}
186220

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/repoConfigTest.js

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,18 @@ describe('RepoConfig', function() {
103103
assert.sameMembers(_.keys(config.analysers("json")), ["sidekick-david"]);
104104
assert.sameMembers(_.keys(config.analysers("all")), ["sidekick-security"]);
105105
assert.lengthOf(config.analysers("js"), 0);
106+
assert.lengthOf(config.analysers("ts"), 0);
107+
assert.lengthOf(config.analysers("cs"), 0);
106108
})
107109
});
108110

109111
describe('loads defaults - for javascript', function() {
110112
var config;
111113
var repoPath = path.join(__dirname, '/fixtures/repoWithoutConfig');
114+
112115
before(function() {
113-
var testJS = fs.readFileSync(path.join(__dirname, '/repoConfigTest.js'));
114-
fs.writeFile(path.join(repoPath, '/test.js'), testJS);
115-
return repoConfig.load(path.join(__dirname, '/fixtures/repoWithoutConfig'))
116+
fs.writeFile(path.join(repoPath, '/test.js'), 'function(){}');
117+
return repoConfig.load(repoPath)
116118
.then(function(_config){
117119
config = _config;
118120
})
@@ -121,11 +123,62 @@ describe('RepoConfig', function() {
121123
it('config has javascript specific defaults', function() {
122124
assert.sameMembers(_.keys(config.analysers("json")), ["sidekick-david"]);
123125
assert.sameMembers(_.keys(config.analysers("all")), ["sidekick-security"]);
124-
assert.sameMembers(_.keys(config.analysers("js")), ["sidekick-eslint", "sidekick-js-todos", "sidekick-jshint"]);
126+
assert.sameMembers(_.keys(config.analysers("js")), ["sidekick-eslint", "sidekick-js-todos", "sidekick-jshint", "sidekick-jscs"]);
127+
assert.lengthOf(config.analysers("ts"), 0);
128+
assert.lengthOf(config.analysers("cs"), 0);
125129
});
126130

127-
after(function(){
131+
});
132+
133+
describe('loads defaults - for typescript', function() {
134+
var config;
135+
var repoPath = path.join(__dirname, '/fixtures/repoWithoutConfig');
136+
137+
before(function() {
128138
fs.removeSync(path.join(repoPath, '/test.js'));
139+
fs.writeFile(path.join(repoPath, '/test.ts'), 'function(){}');
140+
return repoConfig.load(repoPath)
141+
.then(function(_config){
142+
config = _config;
143+
})
144+
});
145+
146+
it('config has typescript specific defaults', function() {
147+
assert.sameMembers(_.keys(config.analysers("json")), ["sidekick-david"]);
148+
assert.sameMembers(_.keys(config.analysers("all")), ["sidekick-security"]);
149+
assert.sameMembers(_.keys(config.analysers("ts")), ["sidekick-tslint"]);
150+
assert.lengthOf(config.analysers("js"), 0);
151+
assert.lengthOf(config.analysers("cs"), 0);
152+
});
153+
154+
after(function(){
155+
fs.removeSync(path.join(repoPath, '/test.ts'));
156+
})
157+
})
158+
159+
describe('loads defaults - for coffeescript', function() {
160+
var config;
161+
var repoPath = path.join(__dirname, '/fixtures/repoWithoutConfig');
162+
163+
before(function() {
164+
fs.removeSync(path.join(repoPath, '/test.ts'));
165+
fs.writeFile(path.join(repoPath, '/test.cs'), 'function(){}');
166+
return repoConfig.load(repoPath)
167+
.then(function(_config){
168+
config = _config;
169+
})
170+
});
171+
172+
it('config has coffeescript specific defaults', function() {
173+
assert.sameMembers(_.keys(config.analysers("json")), ["sidekick-david"]);
174+
assert.sameMembers(_.keys(config.analysers("all")), ["sidekick-security"]);
175+
assert.sameMembers(_.keys(config.analysers("cs")), ["sidekick-coffeelint"]);
176+
assert.lengthOf(config.analysers("js"), 0);
177+
assert.lengthOf(config.analysers("ts"), 0);
178+
});
179+
180+
after(function(){
181+
fs.removeSync(path.join(repoPath, '/test.cs'));
129182
})
130183
})
131184

0 commit comments

Comments
 (0)