UNPKG

5.18 kBJavaScriptView Raw
1var gulp = require('gulp');
2var gulpIf = require('gulp-if');
3var fs = require('fs');
4var cwd = process.cwd();
5var paths = require('./paths');
6var path = require('path');
7var logger = require('./logger');
8var args = require('./args');
9var hasJsHintConfig = fs.existsSync(cwd + '/.jshintrc');
10var esLintConfig = paths.findClosest('.eslintrc');
11
12var hasJSX = paths.hasSourceFiles('jsx');
13var hasES6 = paths.hasSourceFiles('es6');
14
15if (esLintConfig) {
16 esLintConfig = path.relative(cwd, esLintConfig);
17}
18
19exports.exec = function(langConfig, lintConfig) {
20 if (lintConfig.level === 'none') {
21 logger.logInfo('Code linting is disabled.');
22 return;
23 }
24
25 if (hasJsHintConfig) {
26 // We only use jshint if it's explicitly configured
27 // with a .jshintrc file.
28 runJsHint(lintConfig);
29 } else {
30 runEslint(langConfig, lintConfig);
31 }
32};
33
34function runJsHint(lintConfig) {
35 var jshint = require('gulp-jshint');
36 var jshintConfig;
37
38 if (!hasJsHintConfig) {
39 logger.logInfo('\t- Using default JSHint configuration (in js-builder). Override by defining a .jshintrc in this folder.');
40 jshintConfig = require('../res/default.jshintrc');
41 }
42 function _runJsHint(pathSet) {
43 for (var i = 0; i < pathSet.length; i++) {
44 // TODO: eslint for .jsx and .es6 files.
45 gulp.src(['./index.js', pathSet[i] + '/**/*.js'])
46 .pipe(jshint(jshintConfig))
47 .pipe(jshint.reporter('default'))
48 .pipe(jshint.reporter('fail'));
49 }
50 }
51 if (lintConfig.src) {
52 _runJsHint(paths.srcPaths);
53 }
54 if (lintConfig.tests) {
55 _runJsHint([paths.testSrcPath]);
56 }
57}
58
59function runEslint(langConfig, lintConfig) {
60 var eslint = require('gulp-eslint');
61 var eslintConfig;
62
63 if (!esLintConfig) {
64 if (hasJSX) {
65 logger.logInfo('\t- Using the "react" eslint configuration from eslint-config-jenkins. Override by defining a .eslintrc in this folder (if you really must).');
66 eslintConfig = require('@jenkins-cd/eslint-config-jenkins/react');
67 } else if (langConfig.ecmaVersion === 6 || hasES6) {
68 logger.logInfo('\t- Using the "es6" eslint configuration from eslint-config-jenkins. Override by defining a .eslintrc in this folder (if you really must).');
69 eslintConfig = require('@jenkins-cd/eslint-config-jenkins/es6');
70 } else {
71 logger.logInfo('\t- Using the "es5" eslint configuration from eslint-config-jenkins. Override by defining a .eslintrc in this folder (if you really must).');
72 eslintConfig = require('@jenkins-cd/eslint-config-jenkins/es5');
73 }
74 } else {
75 logger.logInfo('\t- Using ' + esLintConfig + '. Override by defining a .eslintrc in this folder.');
76 eslintConfig = {
77 extends: esLintConfig
78 };
79 }
80
81 var fixLint = args.isArgvSpecified('--fixLint');
82 if (fixLint) {
83 eslintConfig.fix = true;
84 }
85 function isFixed(file) {
86 // Has ESLint fixed the file contents?
87 return fixLint && file.eslint != null && file.eslint.fixed;
88 }
89
90 function _runEsLint(pathSet, patterns) {
91
92 function getSrcPaths(path) {
93 var srcPaths = [];
94 for (var i = 0; i < patterns.length; i++) {
95 srcPaths.push(path + '/' + patterns[i]);
96 }
97 return srcPaths;
98 }
99
100 for (var i = 0; i < pathSet.length; i++) {
101 var srcPaths = getSrcPaths(pathSet[i]);
102 gulp.src(srcPaths)
103 .pipe(eslint(eslintConfig))
104 .pipe(eslint.format())
105 .pipe(eslint.results(function (results) {
106 if (results.errorCount > 0 || results.warningCount > 0) {
107 logger.logWarn('Oops, there are some eslint errors/warnings:');
108 if (results.warningCount > 0) {
109 logger.logWarn('\tWarnings: ' + results.warningCount);
110 }
111 if (results.errorCount > 0) {
112 logger.logError('\tErrors: ' + results.errorCount);
113 if (!fixLint && !args.isArgvSpecified('--continueOnLint')) {
114 logger.logError('There are eslint errors. Failing the build now. (--continueOnLint to continue on lint errors)');
115 logger.logInfo('** try "gulp lint --fixLint" to fix some/all linting issues **');
116 process.exit(1);
117 } else {
118 logger.logInfo('** try "gulp lint --fixLint" to fix some/all linting issues **');
119 }
120 }
121 }
122 }))
123 .pipe(gulpIf(isFixed, gulp.dest(pathSet[i])))
124 ;
125 }
126 }
127 if (lintConfig.src) {
128 _runEsLint([cwd], ['index.js']);
129 _runEsLint(paths.srcPaths, ['**/*.js', '**/*.jsx', '**/*.es6']);
130 }
131 if (lintConfig.tests) {
132 _runEsLint([paths.testSrcPath], ['**/*.js', '**/*.jsx', '**/*.es6']);
133 }
134}
\No newline at end of file