Skip to content

Commit 127206d

Browse files
committed
fix(lint): 修复找不到eslintrc问题
1 parent e37dcf6 commit 127206d

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

lib/models/Project.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ var Project = function () {
234234
}, {
235235
key: 'lint',
236236
value: function lint(dir, callback) {
237+
var _this3 = this;
238+
237239
warn('Linting JS Files ...');
238240

239241
var CLIEngine = require('eslint').CLIEngine;
@@ -244,9 +246,20 @@ var Project = function () {
244246
}
245247

246248
// prepare eslint config file
247-
var configFilePath = sysPath.join(__dirname, '../../cache', '.eslintrc.json');
248-
fs.writeFileSync(configFilePath, JSON.stringify(this.eslintConfig));
249-
this.eslintConfig.useEslintrc = false;
249+
// const configFilePath = sysPath.join(__dirname, '../../' ,'.eslintrc.json')
250+
var eslintExts = ['.js', '.yaml', '.yml', '.json', ''];
251+
var configFilePath = '';
252+
eslintExts.forEach(function (eslintExtItem) {
253+
if (_this3._fileExists(sysPath.join(_this3.cwd, '.eslintrc' + eslintExtItem))) {
254+
configFilePath = sysPath.join(_this3.cwd, '.eslintrc' + eslintExtItem);
255+
}
256+
});
257+
258+
if (!configFilePath) {
259+
configFilePath = sysPath.join(this.cwd, '.eslintrc.json');
260+
fs.writeFileSync(configFilePath, JSON.stringify(this.eslintConfig, null, ' '));
261+
}
262+
250263
this.eslintConfig.configFile = configFilePath;
251264

252265
var cli = new CLIEngine(this.eslintConfig),
@@ -289,7 +302,7 @@ var Project = function () {
289302
}, {
290303
key: 'pack',
291304
value: function pack(opt, callback) {
292-
var _this3 = this;
305+
var _this4 = this;
293306

294307
var config = this.config.getConfig();
295308
UtilFs.deleteFolderRecursive(this.cachePath);
@@ -316,7 +329,7 @@ var Project = function () {
316329
config.output = config.output.dev;
317330
}
318331

319-
_this3.fixCss();
332+
_this4.fixCss();
320333

321334
if (opt.clean) {
322335
try {
@@ -357,7 +370,7 @@ var Project = function () {
357370
}
358371
});
359372
info();
360-
_this3.packCallbacks.forEach(function (cb) {
373+
_this4.packCallbacks.forEach(function (cb) {
361374
return cb(opt, stats);
362375
});
363376
}
@@ -368,9 +381,9 @@ var Project = function () {
368381

369382
if (opt.lint) {
370383
async.series([function (callback) {
371-
return _this3.lint(callback);
384+
return _this4.lint(callback);
372385
}, function (callback) {
373-
return _this3.lintCss(callback);
386+
return _this4.lintCss(callback);
374387
}], function (err, results) {
375388
if (!err) {
376389
if (results[0] && results[1]) {
@@ -469,6 +482,15 @@ var Project = function () {
469482

470483
return false;
471484
}
485+
}, {
486+
key: '_fileExists',
487+
value: function _fileExists(filePath) {
488+
try {
489+
return fs.statSync(filePath).isFile();
490+
} catch (err) {
491+
return false;
492+
}
493+
}
472494
}]);
473495

474496
return Project;

src/models/Project.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,23 @@ class Project {
217217
}
218218

219219
// prepare eslint config file
220-
const configFilePath = sysPath.join(__dirname, '../../cache' ,'.eslintrc.json')
221-
fs.writeFileSync(
222-
configFilePath,
223-
JSON.stringify(this.eslintConfig)
224-
);
225-
this.eslintConfig.useEslintrc = false
220+
// const configFilePath = sysPath.join(__dirname, '../../' ,'.eslintrc.json')
221+
const eslintExts = ['.js', '.yaml', '.yml', '.json', '']
222+
let configFilePath = ''
223+
eslintExts.forEach((eslintExtItem) => {
224+
if(this._fileExists(sysPath.join(this.cwd, '.eslintrc' + eslintExtItem))) {
225+
configFilePath = sysPath.join(this.cwd, '.eslintrc' + eslintExtItem)
226+
}
227+
})
228+
229+
if(!configFilePath) {
230+
configFilePath = sysPath.join(this.cwd, '.eslintrc.json')
231+
fs.writeFileSync(
232+
configFilePath,
233+
JSON.stringify(this.eslintConfig, null, ' ')
234+
);
235+
}
236+
226237
this.eslintConfig.configFile = configFilePath
227238

228239
const cli = new CLIEngine(this.eslintConfig),
@@ -432,6 +443,14 @@ class Project {
432443

433444
return false
434445
}
446+
447+
_fileExists(filePath) {
448+
try {
449+
return fs.statSync(filePath).isFile();
450+
} catch (err) {
451+
return false;
452+
}
453+
}
435454
}
436455

437456
module.exports = Project;

0 commit comments

Comments
 (0)