Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Feb 9, 2016
0 parents commit 8bdde61
Show file tree
Hide file tree
Showing 20 changed files with 672 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[package.json]
indent_size = 2
8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"root": true,
"extends": [
"./node_modules/@satazor/eslint-config/es6.js",
"./node_modules/@satazor/eslint-config/addons/node.js",
"./node_modules/@satazor/eslint-config/addons/node-v4-es6.js"
]
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
/npm-debug.*
/test/tmp
/test/coverage
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "4"
- "5"
script: "npm run test-travis"
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 IndigoUnited

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# detect-repo-linters

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]

[npm-url]:https://npmjs.org/package/detect-repo-linters
[downloads-image]:http://img.shields.io/npm/dm/detect-repo-linters.svg
[npm-image]:http://img.shields.io/npm/v/detect-repo-linters.svg
[travis-url]:https://travis-ci.org/IndigoUnited/node-detect-repo-linters
[travis-image]:http://img.shields.io/travis/IndigoUnited/node-detect-repo-linters.svg
[coveralls-url]:https://coveralls.io/r/IndigoUnited/node-detect-repo-linters
[coveralls-image]:https://img.shields.io/coveralls/IndigoUnited/node-detect-repo-linters.svg
[david-dm-url]:https://david-dm.org/IndigoUnited/node-detect-repo-linters
[david-dm-image]:https://img.shields.io/david/IndigoUnited/node-detect-repo-linters.svg
[david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-detect-repo-linters#info=devDependencies
[david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-detect-repo-linters.svg

Scans a repository directory, searching for configured linters.


## Installation

`$ npm install detect-repo-linters`


## Usage

`detectRepoLinters(repo, [callback]) -> Promise`

You may consume the API using promises or callbacks, it's up to you.

```js
const detectRepoLinters = require('detect-repo-linters');

detectRepoLinters('repo-directory')
.then((linters) => {
console.log(linters);

// {
// general: ['editorconfig'],
// js: ['eslint'],
// css: ['stylelint'],
// html: [],
// }
});
```

At the moment, `detect-repo-linters` can detect the following linters:

- `general`: [editorconfig](http://editorconfig.org)
- `js`: [eslint](http://eslint.org), [jscs](http://jscs.info) and [jshint](http://jshint.com)
- `css`: [stylelint](http://stylelint.io) and [csslint](http://csslint.net)
- `html`: [htmlhint](http://htmlhint.com) and [htmllint](http://htmllint.github.io)

Feel free to a PR to include other linters as part of the detection!


## Tests

`$ npm test`
`$ npm test-cov` to get coverage report


## License

Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
67 changes: 67 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

const fs = require('fs');
const requireDirectory = require('require-directory');
const callMeMaybe = require('call-me-maybe');
const detectors = requireDirectory(module, './lib', { recurse: false });

const linters = {
general: [
{ name: 'editorconfig', fn: detectors.editorconfig },
],
js: [
{ name: 'eslint', fn: detectors.eslint },
{ name: 'jscs', fn: detectors.jscs },
{ name: 'jshint', fn: detectors.jshint },
],
css: [
{ name: 'stylelint', fn: detectors.stylelint },
{ name: 'csslint', fn: detectors.csslint },
],
html: [
{ name: 'htmlhint', fn: detectors.htmlhint },
{ name: 'htmllint', fn: detectors.htmllint },
],
};

function detectLinters(dir, linters) {
return Promise.all(linters.map((linter) => linter.fn(dir)))
.then((results) => {
return linters
.filter((linter, index) => results[index])
.map((linter) => linter.name);
});
}

function detectRepoLinters(dir, callback) {
// Check if dir exists
const promise = new Promise((resolve, reject) => {
fs.stat(dir, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
})
// Run the linter detectors and build the results
.then(() => {
const types = Object.keys(linters);
const promises = types.map((type) => detectLinters(dir, linters[type]));

return Promise.all(promises)
.then((results) => {
const ret = {};

results.forEach((result, index) => {
ret[types[index]] = result;
});

return ret;
});
});

return callMeMaybe(callback, promise);
}

module.exports = detectRepoLinters;
13 changes: 13 additions & 0 deletions lib/csslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const path = require('path');
const tryFiles = require('./util/tryFiles');

function detectCssLint(dir) {
const paths = ['.csslintrc']
.map((entry) => path.join(dir, entry));

return tryFiles(paths);
}

module.exports = detectCssLint;
12 changes: 12 additions & 0 deletions lib/editorconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const path = require('path');
const tryFiles = require('./util/tryFiles');

function detectEditorConfig(dir) {
const paths = ['.editorconfig'].map((entry) => path.join(dir, entry));

return tryFiles(paths);
}

module.exports = detectEditorConfig;
18 changes: 18 additions & 0 deletions lib/eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const path = require('path');
const tryFiles = require('./util/tryFiles');
const tryPackageJson = require('./util/tryPackageJson');

function detectEslint(dir) {
const paths = ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']
.map((entry) => path.join(dir, entry));

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'eslintConfig'),
])
.then((booleans) => booleans.some((bool) => bool));
}

module.exports = detectEslint;
13 changes: 13 additions & 0 deletions lib/htmlhint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const path = require('path');
const tryFiles = require('./util/tryFiles');

function detectHtmlHint(dir) {
const paths = ['.htmlhintrc']
.map((entry) => path.join(dir, entry));

return tryFiles(paths);
}

module.exports = detectHtmlHint;
13 changes: 13 additions & 0 deletions lib/htmllint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const path = require('path');
const tryFiles = require('./util/tryFiles');

function detectHtmlHint(dir) {
const paths = ['.htmllintrc']
.map((entry) => path.join(dir, entry));

return tryFiles(paths);
}

module.exports = detectHtmlHint;
18 changes: 18 additions & 0 deletions lib/jscs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const path = require('path');
const tryFiles = require('./util/tryFiles');
const tryPackageJson = require('./util/tryPackageJson');

function detectJscs(dir) {
const paths = ['.jscsrc', '.jscs.json']
.map((entry) => path.join(dir, entry));

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'jscsConfig'),
])
.then((booleans) => booleans.some((bool) => bool));
}

module.exports = detectJscs;
18 changes: 18 additions & 0 deletions lib/jshint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const path = require('path');
const tryFiles = require('./util/tryFiles');
const tryPackageJson = require('./util/tryPackageJson');

function detectJsHint(dir) {
const paths = ['.jshintrc']
.map((entry) => path.join(dir, entry));

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'jshintConfig'),
])
.then((booleans) => booleans.some((bool) => bool));
}

module.exports = detectJsHint;
18 changes: 18 additions & 0 deletions lib/stylelint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const path = require('path');
const tryFiles = require('./util/tryFiles');
const tryPackageJson = require('./util/tryPackageJson');

function detectStyleLint(dir) {
const paths = ['.stylelintrc.js', '.stylelintrc.yaml', '.stylelintrc.json', '.stylelintrc']
.map((entry) => path.join(dir, entry));

return Promise.all([
tryFiles(paths),
tryPackageJson(dir, 'stylelint'),
])
.then((booleans) => booleans.some((bool) => bool));
}

module.exports = detectStyleLint;
30 changes: 30 additions & 0 deletions lib/util/tryFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const fs = require('fs');

function isFile(path) {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (!err) {
return resolve(stats.isFile());
}

if (err.code === 'ENOENT') {
return resolve(false);
}

reject(err);
});
});
}

function tryFiles(paths) {
return Promise.all(paths.map((path) => isFile(path)))
.then((results) => {
const filteredResults = results.filter((exists) => exists);

return !!filteredResults[0];
});
}

module.exports = tryFiles;
17 changes: 17 additions & 0 deletions lib/util/tryPackageJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const path = require('path');
const loadJsonFile = require('load-json-file');

function tryPackageJson(dir, prop) {
return loadJsonFile(path.join(dir, 'package.json'))
.then((json) => !!json[prop], (err) => {
if (err.code === 'ENOENT') {
return false;
}

throw err;
});
}

module.exports = tryPackageJson;
Loading

0 comments on commit 8bdde61

Please sign in to comment.