-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlint.js
192 lines (165 loc) · 5.23 KB
/
lint.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
/**
* Gulp Linters.
*
* This gulp plugin verifies that the code is following standard styleguides
* and warns when it isn't. It allows us to quickly catch issues such as
* tabs instead of spaces, missing comments, errors etc.
*
* Calling the linters is really simple:
*
* ```
* var gulp = require('gulp');
* var linters = require('gulp-linters');
* linters.register(gulp, {
* files: {
* scss: ['scssFilesToInclude.scss'],
* js: ['jsFilesToInclude.js']
* }
* });
* gulp.task('default', linters.all);
* ```
*
* Depending on which environment you're working from, you may want to either
* just start a watcher, or lint one time your entire codebase.
*
* * `all`: Lint all the files, for all formats.
* * `scss`: Lint only scss files.
* * `js`: Lint only js files.
* * `py`: Lint only python files
* * `watch`: Launch the watcher which runs whenever one file (scss, js)
* is updated. If you want the watcher to only execute for a specific
* file format, specify the options `disableJs` or `disableScss`.
* * `dev`: Launch the linter, and attach the watcher.
*
* @author Michael Ortali <[email protected]>
*/
'use strict';
var reporter = require('./lib/reporter.js');
var cache = require('gulp-cached');
var extend = require('node.extend');
var shell = require('gulp-shell');
var jsCs = require('gulp-jscs');
var jsHint = require('gulp-jshint');
var scssLint = require('gulp-scss-lint');
var gutil = require('gulp-util');
/**
* Configuration of the plugin.
*/
var config = {
jsHint: require('./config/jshint.js'),
jsCs: require('./config/jscs.js'),
scss: __dirname + '/config/scss.yml',
watch: false,
cache: false,
// Look for all the .scss files except the ones in node_modules.
files: {
js: [],
php: [],
py: [],
scss: []
}
};
/**
* Register the tasks on the existing Gulp process.
*
* @param {Gulp} gulp The current gulp process.
* @param {Object} options The options for the linters.
*/
var register = function (gulp, options) {
options = options || {};
config.files = extend(config.files, options.files);
config.disableJS = options.disableJS;
config.disableScss = options.disableScss;
config.disablePhp = options.disablePhp;
config.jsHint.globals = extend(config.jsHint.globals, options.jsHintGlobals);
reporter.setFailureHandler(options.onFail);
/**
* Task: JS Linter.
*
* Look for all the js file within a specific directory and run jslint on
* them.
*/
gulp.task('linter:js', function () {
return gulp.src(config.files.js)
.pipe(cache())
// JS Hint
.pipe(jsHint(config.jsHint))
.pipe(jsHint.reporter(reporter.js('jsHint')))
// JS CS
.pipe(jsCs(config.jsCs))
.on('error', function(err) {
gutil.log('\n' + err.message);
options.onFail();
this.emit('end');
});
});
/**
* Task: SCSS Linter.
*
* Look for all the scss files within a specific directory and run scss
* linter on them.
*/
gulp.task('linter:scss', function () {
return gulp.src(config.files.scss)
.pipe(scssLint({
config: config.scss,
customReport: reporter.scss
}));
});
/**
* Task: Python linter.
*
* Look for all the python files within a specific directory and run the
* pyflakes and pep8 against them.
*/
gulp.task('linter:python', function () {
var pylint = shell([
'pyflakes <%= file.path %>',
'pylint --rcfile=config/pylint <%= file.path %>'
],
{quiet: true, ignoreErrors: true});
return gulp.src(config.files.py, {read: false})
.pipe(pylint)
.on('data', reporter.py);
});
/**
* Task: php linter (PHP_CodeSniffer)
*/
gulp.task('linter:php', function() {
var codeSniffer = shell(
[__dirname + '/vendor/bin/phpcs --standard=PSR2 <%= file.path %>'],
{quiet: true, ignoreErrors: true});
return gulp.src(config.files.php, {read: false})
.pipe(codeSniffer)
.on('data', reporter.php);
});
/**
* Watcher: relaunch the tasks whenever a file is updated.
*/
gulp.task('watch:all', function () {
if (config.files.scss.length > 0) {
gulp.watch(config.cssFiles, ['linter:scss']);
}
if (config.files.js.length > 0) {
gulp.watch(config.jsFiles, ['linter:js']);
}
if (config.files.php.length > 0) {
gulp.watch(config.phpFiles, ['linter:php']);
}
if (config.files.py.length > 0) {
gulp.watch(config.phpFiles, ['linter:py']);
}
});
};
module.exports = {
config: config,
register: register,
all: ['linter:scss', 'linter:js', 'linter:python', 'linter:php'],
dev: [
'linter:scss', 'linter:js', 'linter:python', 'linter:php',
'watch:all'],
php: ['linter:php'],
python: ['linter:python'],
scss: ['linter:scss'],
watch: ['watch:all']
};