kibana/tasks/lint_staged_files.js
Spencer 654a3be6f0 [grunt/eslint] fix precommit linting (#9510)
* [grunt/eslint] fix precommit linting

 - remove use of `minimatch.makeRe()` because it does not support the entire glob syntax
 - log a warning whenever a js file is excluded by the `lintStagedFiles` task
 - eslint globs are relative to the project root, ensure that we check against relative version

* [grunt/eslint] only log warning wtr grunt paths
2016-12-16 12:29:35 -07:00

47 lines
1.5 KiB
JavaScript

import { extname, resolve, relative } from 'path';
import { isStaged, getFilename } from './utils/files_to_commit';
import { CLIEngine } from 'eslint';
import { red, blue } from 'ansicolors';
import minimatch from 'minimatch';
const root = resolve(__dirname, '..');
export default function (grunt) {
grunt.registerTask('lintStagedFiles', function () {
grunt.task.requires('collectFilesToCommit');
// convert eslint paths to globs
const cli = new CLIEngine();
const eslintSourcePaths = grunt.config.get('eslint.options.paths');
if (!eslintSourcePaths) grunt.fail.warn('eslint.options.paths is not defined');
const sourcePathGlobs = cli.resolveFileGlobPatterns(eslintSourcePaths);
const files = grunt.config
.get('filesToCommit')
.filter(isStaged)
.map(getFilename)
.map(file => relative(root, resolve(file))) // resolve to pwd, then get relative from the root
.filter(file => {
if (!sourcePathGlobs.some(glob => minimatch(file, glob))) {
if (extname(file) === '.js') {
grunt.log.writeln(`${red('WARNING:')} ${file} not selected by grunt eslint config`);
}
return false;
}
if (cli.isPathIgnored(file)) {
if (extname(file) === '.js') {
grunt.log.writeln(`${blue('DEBUG:')} ${file} ignored by .eslintignore`);
}
return false;
}
return true;
});
grunt.config.set('eslint.staged.options.paths', files);
grunt.task.run(['eslint:staged']);
});
}