|
2 | 2 | /*global require, exports*/ |
3 | 3 |
|
4 | 4 | (function () { |
5 | | - 'use strict'; |
6 | | - |
7 | | - var fs = require('fs'); |
8 | | - var CLIEngine = require('eslint').CLIEngine; |
9 | | - var cli = new CLIEngine(); |
10 | | - var currentProjectRoot = null; |
11 | | - var domainName = 'brackets-eslint'; |
12 | | - var domainManager = null; |
13 | | - var noop = function () {}; |
14 | | - var globalPackagesAvailable = false; |
15 | | - |
16 | | - function _setProjectRoot(projectRoot) { |
17 | | - var opts = { useEslintrc: true }; |
18 | | - var configPath; |
19 | | - var rulesDirPath; |
20 | | - var ignorePath; |
21 | | - |
22 | | - if (projectRoot) { |
23 | | - configPath = projectRoot + '.eslintrc'; |
24 | | - try { |
25 | | - if (fs.statSync(configPath).isFile()) { |
26 | | - noop(); |
| 5 | + 'use strict'; |
| 6 | + |
| 7 | + var fs = require('fs'); |
| 8 | + var CLIEngine = require('eslint').CLIEngine; |
| 9 | + var cli = new CLIEngine(); |
| 10 | + var currentProjectRoot = null; |
| 11 | + var domainName = 'brackets-eslint'; |
| 12 | + var domainManager = null; |
| 13 | + var noop = function () {}; |
| 14 | + var globalPackagesAvailable = false; |
| 15 | + |
| 16 | + function _setProjectRoot(projectRoot) { |
| 17 | + var opts = { useEslintrc: true }; |
| 18 | + var configPath; |
| 19 | + var rulesDirPath; |
| 20 | + var ignorePath; |
| 21 | + |
| 22 | + if (projectRoot) { |
| 23 | + configPath = projectRoot + '.eslintrc'; |
| 24 | + try { |
| 25 | + if (fs.statSync(configPath).isFile()) { |
| 26 | + noop(); |
| 27 | + } |
| 28 | + } catch (e) { |
| 29 | + // config file not found, use default |
| 30 | + opts.rules = require('eslint/conf/eslint.json').rules; |
| 31 | + } |
| 32 | + |
| 33 | + rulesDirPath = projectRoot + '.eslintrules'; |
| 34 | + try { |
| 35 | + if (fs.statSync(rulesDirPath).isDirectory()) { |
| 36 | + opts.rulePaths = [rulesDirPath]; |
| 37 | + } |
| 38 | + } catch (e) { |
| 39 | + // no action required |
| 40 | + noop(e); |
| 41 | + } |
| 42 | + |
| 43 | + ignorePath = projectRoot + '.eslintignore'; |
| 44 | + try { |
| 45 | + if (fs.statSync(ignorePath).isFile()) { |
| 46 | + opts.ignore = true; |
| 47 | + opts.ignorePath = ignorePath; |
| 48 | + } |
| 49 | + } catch (e) { |
| 50 | + // no action required |
| 51 | + noop(e); |
| 52 | + } |
27 | 53 | } |
28 | | - } catch (e) { |
29 | | - // config file not found, use default |
30 | | - opts.rules = require('eslint/conf/eslint.json').rules; |
31 | | - } |
32 | | - |
33 | | - rulesDirPath = projectRoot + '.eslintrules'; |
34 | | - try { |
35 | | - if (fs.statSync(rulesDirPath).isDirectory()) { |
36 | | - opts.rulePaths = [rulesDirPath]; |
37 | | - } |
38 | | - } catch (e) { |
39 | | - // no action required |
40 | | - noop(e); |
41 | | - } |
42 | | - |
43 | | - ignorePath = projectRoot + '.eslintignore'; |
44 | | - try { |
45 | | - if (fs.statSync(ignorePath).isFile()) { |
46 | | - opts.ignore = true; |
47 | | - opts.ignorePath = ignorePath; |
48 | | - } |
49 | | - } catch (e) { |
50 | | - // no action required |
51 | | - noop(e); |
52 | | - } |
53 | | - } |
54 | 54 |
|
55 | | - cli = new CLIEngine(opts); |
56 | | - } |
57 | | - |
58 | | - require('enable-global-packages').on('ready', function () { |
59 | | - // global packages are available now |
60 | | - _setProjectRoot(currentProjectRoot); |
61 | | - globalPackagesAvailable = true; |
62 | | - }); |
63 | | - |
64 | | - function lintFile(fullPath, projectRoot, callback) { |
65 | | - if (!globalPackagesAvailable) { |
66 | | - setTimeout(function () { |
67 | | - lintFile(fullPath, projectRoot, callback); |
68 | | - }, 250); |
69 | | - return; |
| 55 | + cli = new CLIEngine(opts); |
70 | 56 | } |
71 | | - if (projectRoot !== currentProjectRoot) { |
72 | | - _setProjectRoot(projectRoot); |
73 | | - currentProjectRoot = projectRoot; |
74 | | - } |
75 | | - fs.readFile(fullPath, {encoding: 'utf8'}, function (err, text) { |
76 | | - if (err) { |
77 | | - return callback(err); |
78 | | - } |
79 | | - var relativePath = fullPath.indexOf(projectRoot) === 0 ? fullPath.substring(projectRoot.length) : fullPath; |
80 | | - |
81 | | - // this is important for ESLint so .eslintrc is properly loaded |
82 | | - // we could go around this by parsing .eslintrc manually but that'd |
83 | | - // bring complexity we don't need here right now |
84 | | - // related: https://github.com/eslint/eslint/issues/4472 |
85 | | - process.chdir(projectRoot); |
86 | | - |
87 | | - var res; |
88 | | - try { |
89 | | - res = cli.executeOnText(text, relativePath); |
90 | | - } catch (e) { |
91 | | - err = e.toString(); |
92 | | - } |
93 | | - callback(err, res); |
94 | | - }); |
95 | | - } |
96 | 57 |
|
97 | | - exports.init = function (_domainManager) { |
98 | | - domainManager = _domainManager; |
| 58 | + require('enable-global-packages').on('ready', function () { |
| 59 | + // global packages are available now |
| 60 | + _setProjectRoot(currentProjectRoot); |
| 61 | + globalPackagesAvailable = true; |
| 62 | + }); |
99 | 63 |
|
100 | | - if (!domainManager.hasDomain(domainName)) { |
101 | | - domainManager.registerDomain(domainName, { |
102 | | - major: 0, |
103 | | - minor: 1 |
104 | | - }); |
| 64 | + function lintFile(fullPath, projectRoot, callback) { |
| 65 | + if (!globalPackagesAvailable) { |
| 66 | + setTimeout(function () { |
| 67 | + lintFile(fullPath, projectRoot, callback); |
| 68 | + }, 250); |
| 69 | + return; |
| 70 | + } |
| 71 | + if (projectRoot !== currentProjectRoot) { |
| 72 | + _setProjectRoot(projectRoot); |
| 73 | + currentProjectRoot = projectRoot; |
| 74 | + } |
| 75 | + fs.readFile(fullPath, {encoding: 'utf8'}, function (err, text) { |
| 76 | + if (err) { |
| 77 | + return callback(err); |
| 78 | + } |
| 79 | + var relativePath = fullPath.indexOf(projectRoot) === 0 ? fullPath.substring(projectRoot.length) : fullPath; |
| 80 | + |
| 81 | + // this is important for ESLint so .eslintrc is properly loaded |
| 82 | + // we could go around this by parsing .eslintrc manually but that'd |
| 83 | + // bring complexity we don't need here right now |
| 84 | + // related: https://github.com/eslint/eslint/issues/4472 |
| 85 | + process.chdir(projectRoot); |
| 86 | + |
| 87 | + var res; |
| 88 | + try { |
| 89 | + res = cli.executeOnText(text, relativePath); |
| 90 | + } catch (e) { |
| 91 | + err = e.toString(); |
| 92 | + } |
| 93 | + callback(err, res); |
| 94 | + }); |
105 | 95 | } |
106 | 96 |
|
107 | | - domainManager.registerCommand( |
108 | | - domainName, |
109 | | - 'lintFile', // command name |
110 | | - lintFile, // handler function |
111 | | - true, // is async |
112 | | - 'lint given file with eslint', // description |
113 | | - [ |
114 | | - { |
115 | | - name: 'fullPath', |
116 | | - type: 'string' |
117 | | - }, |
118 | | - { |
119 | | - name: 'projectRoot', |
120 | | - type: 'string' |
121 | | - } |
122 | | - ], [ |
123 | | - { |
124 | | - name: 'report', |
125 | | - type: 'object' |
| 97 | + exports.init = function (_domainManager) { |
| 98 | + domainManager = _domainManager; |
| 99 | + |
| 100 | + if (!domainManager.hasDomain(domainName)) { |
| 101 | + domainManager.registerDomain(domainName, { |
| 102 | + major: 0, |
| 103 | + minor: 1 |
| 104 | + }); |
126 | 105 | } |
127 | | - ] |
128 | | - ); |
129 | 106 |
|
130 | | - }; |
| 107 | + domainManager.registerCommand( |
| 108 | + domainName, |
| 109 | + 'lintFile', // command name |
| 110 | + lintFile, // handler function |
| 111 | + true, // is async |
| 112 | + 'lint given file with eslint', // description |
| 113 | + [ |
| 114 | + { |
| 115 | + name: 'fullPath', |
| 116 | + type: 'string' |
| 117 | + }, |
| 118 | + { |
| 119 | + name: 'projectRoot', |
| 120 | + type: 'string' |
| 121 | + } |
| 122 | + ], [ |
| 123 | + { |
| 124 | + name: 'report', |
| 125 | + type: 'object' |
| 126 | + } |
| 127 | + ] |
| 128 | + ); |
| 129 | + |
| 130 | + }; |
131 | 131 |
|
132 | 132 | }()); |
0 commit comments