Skip to content

Commit

Permalink
Add Lesshint.getFiles() method
Browse files Browse the repository at this point in the history
This is a more general method which works on globs and handles everything instead of having separate methods.
  • Loading branch information
jwilsson committed Apr 22, 2017
1 parent da0bdd3 commit 689d2da
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
13 changes: 13 additions & 0 deletions docs/developer-guide/api/lesshint.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ result.then((results) => {
});
```

## `Lesshint.checkFiles(pattern)`
Check a glob pattern asynchronously. Will not check `fileExtensions` and `excludedFiles` options.

A `Promise` will be returned.

```js
const result = lesshint.checkFiles('/path/**/*.less');

result.then((results) => {

});
```

## `Lesshint.checkPath(checkPath)`
Check a path asynchronously. If a file is passed it will check that, if a directory is passed it will check that recursively. Will respect `fileExtensions` and `excludedFiles` options.

Expand Down
45 changes: 34 additions & 11 deletions lib/lesshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const configLoader = require('./config-loader');
const minimatch = require('minimatch');
const merge = require('lodash.merge');
const Linter = require('./linter');
const globby = require('globby');
const path = require('path');
const fs = require('fs');

Expand Down Expand Up @@ -48,17 +49,7 @@ class Lesshint {
});
});

Promise.all(files)
.then((results) => {
results = [].concat.apply([], results);

resolve(results);
})
.catch((error) => {
error = new LesshintError(error, error.path);

reject(error);
});
this.formatResults(files, resolve, reject);
});
});
}
Expand All @@ -79,6 +70,24 @@ class Lesshint {
});
}

checkFiles (pattern) {
return new Promise((resolve, reject) => {
globby(pattern)
.then((files) => {
files = files.map((file) => {
return this.checkFile(file);
});

this.formatResults(files, resolve, reject);
})
.catch((error) => {
error = new LesshintError(error, error.path);

reject(error);
});
});
}

checkPath (checkPath) {
return new Promise((resolve, reject) => {
if (this.isExcluded(checkPath)) {
Expand Down Expand Up @@ -151,6 +160,20 @@ class Lesshint {
return fileExtensions.indexOf(path.extname(file)) !== -1;
}

formatResults (files, resolve, reject) {
Promise.all(files)
.then((results) => {
results = [].concat.apply([], results);

resolve(results);
})
.catch((error) => {
error = new LesshintError(error, error.path);

reject(error);
});
}

isExcluded (checkPath) {
return this.config.excludedFiles.some(function (pattern) {
return minimatch(checkPath, pattern, {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"commander": "^2.8.0",
"globby": "^6.1.0",
"lodash.merge": "^4.0.1",
"lodash.sortby": "^4.0.1",
"minimatch": "^3.0.2",
Expand Down
14 changes: 14 additions & 0 deletions test/specs/lesshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ describe('lesshint', function () {
});
});

describe('checkFiles', function () {
const glob = path.join(path.dirname(__dirname), '/data/files/**/*.less');

it('should check all files matched by glob', function () {
const lesshint = new Lesshint();

lesshint.configure();

return lesshint.checkFiles(glob).then(function (result) {
expect(result).to.have.length(2);
});
});
});

describe('checkPath', function () {
it('should check all files and directories on all levels of a path', function () {
const lesshint = new Lesshint();
Expand Down

0 comments on commit 689d2da

Please sign in to comment.