Skip to content

Commit 56a4983

Browse files
committed
fix(eslint): 修复eslint无法extend, 提供lint设置接口
1 parent 2c12dd7 commit 56a4983

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
cache

lib/models/Project.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var Project = function () {
3030
cwd: this.cwd
3131
})[0] || '';
3232
this.extendConfig = this.configFile && this.configFile.match(/ykit\.([\w\.]+)\.js/) && this.configFile.match(/ykit\.([\w\.]+)\.js/)[1] && this.configFile.match(/ykit\.([\w\.]+)\.js/)[1].replace(/\./g, '-');
33-
this.ignores = ["node_modules/**/*", "bower_components/**/*", "dev/**/*", "prd/**/*"];
33+
this.ignores = ["node_modules/**/*", "bower_components/**/*", "dev/**/*", "prd/**/*", ".ykit_cache/**/*"];
3434
this.cachePath = this._isCacheDirExists(cwd) || '';
3535

3636
this.readConfig();
@@ -48,6 +48,16 @@ var Project = function () {
4848
this.commands = this.commands.concat(nextCommands);
4949
}
5050
}
51+
}, {
52+
key: 'setEslintConfig',
53+
value: function setEslintConfig(projectEslintConfig) {
54+
extend(true, this.eslintConfig, projectEslintConfig);
55+
}
56+
}, {
57+
key: 'setStylelintConfig',
58+
value: function setStylelintConfig(projectStylelintConfig) {
59+
extend(true, this.stylelintConfig, projectStylelintConfig);
60+
}
5161
}, {
5262
key: 'readConfig',
5363
value: function readConfig(options) {
@@ -64,6 +74,8 @@ var Project = function () {
6474
setGroupExports: this.config.setGroupExports.bind(this.config),
6575
setSync: this.config.setSync.bind(this.config),
6676
setCommands: this.setCommands.bind(this),
77+
setEslintConfig: this.setEslintConfig.bind(this),
78+
setStylelintConfig: this.setStylelintConfig.bind(this),
6779
config: this.config.getConfig(),
6880
commands: this.commands,
6981
middlewares: this.middlewares,
@@ -222,16 +234,22 @@ var Project = function () {
222234
}, {
223235
key: 'lint',
224236
value: function lint(dir, callback) {
237+
warn('Linting JS Files ...');
238+
225239
var CLIEngine = require('eslint').CLIEngine;
226240

227-
warn('Linting JS Files ...');
228-
this.eslintConfig.useEslintrc = false;
241+
// 如果有本地eslint优先使用本地eslint
242+
if (requireg(sysPath.join(this.cwd, 'node_modules/', 'eslint'))) {
243+
CLIEngine = requireg(sysPath.join(this.cwd, 'node_modules/', 'eslint')).CLIEngine;
244+
}
229245

230-
extend(true, this.eslintConfig, this.config._config.eslintConfig);
246+
// 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;
250+
this.eslintConfig.configFile = configFilePath;
231251

232-
var cliengine = this.eslintConfig.linter || CLIEngine,
233-
// 优先使用项目配置的linter
234-
cli = new cliengine(this.eslintConfig),
252+
var cli = new CLIEngine(this.eslintConfig),
235253
report = cli.executeOnFiles(this._getLintFiles(dir, 'js')),
236254
formatter = cli.getFormatter();
237255

src/models/Project.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Project {
2525
&& this.configFile.match(/ykit\.([\w\.]+)\.js/)
2626
&& this.configFile.match(/ykit\.([\w\.]+)\.js/)[1]
2727
&& this.configFile.match(/ykit\.([\w\.]+)\.js/)[1].replace(/\./g, '-');
28-
this.ignores = ["node_modules/**/*", "bower_components/**/*", "dev/**/*", "prd/**/*"];
28+
this.ignores = ["node_modules/**/*", "bower_components/**/*", "dev/**/*", "prd/**/*", ".ykit_cache/**/*"];
2929
this.cachePath = this._isCacheDirExists(cwd) || ''
3030

3131
this.readConfig();
@@ -38,6 +38,12 @@ class Project {
3838
this.commands = this.commands.concat(nextCommands)
3939
}
4040
}
41+
setEslintConfig(projectEslintConfig) {
42+
extend(true, this.eslintConfig, projectEslintConfig);
43+
}
44+
setStylelintConfig(projectStylelintConfig) {
45+
extend(true, this.stylelintConfig, projectStylelintConfig);
46+
}
4147
readConfig(options) {
4248
if (this.check()) {
4349
let userConfig = {
@@ -50,6 +56,8 @@ class Project {
5056
setGroupExports: this.config.setGroupExports.bind(this.config),
5157
setSync: this.config.setSync.bind(this.config),
5258
setCommands: this.setCommands.bind(this),
59+
setEslintConfig: this.setEslintConfig.bind(this),
60+
setStylelintConfig: this.setStylelintConfig.bind(this),
5361
config: this.config.getConfig(),
5462
commands: this.commands,
5563
middlewares: this.middlewares,
@@ -199,15 +207,25 @@ class Project {
199207
}
200208

201209
lint(dir, callback) {
202-
const CLIEngine = require('eslint').CLIEngine;
203-
204210
warn('Linting JS Files ...');
205-
this.eslintConfig.useEslintrc = false;
206211

207-
extend(true, this.eslintConfig, this.config._config.eslintConfig);
212+
let CLIEngine = require('eslint').CLIEngine;
213+
214+
// 如果有本地eslint优先使用本地eslint
215+
if(requireg(sysPath.join(this.cwd, 'node_modules/', 'eslint'))){
216+
CLIEngine = requireg(sysPath.join(this.cwd, 'node_modules/', 'eslint')).CLIEngine
217+
}
218+
219+
// 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
226+
this.eslintConfig.configFile = configFilePath
208227

209-
const cliengine = this.eslintConfig.linter || CLIEngine, // 优先使用项目配置的linter
210-
cli = new cliengine(this.eslintConfig),
228+
const cli = new CLIEngine(this.eslintConfig),
211229
report = cli.executeOnFiles(this._getLintFiles(dir, 'js')),
212230
formatter = cli.getFormatter();
213231

0 commit comments

Comments
 (0)