|
1 | 1 | #!/usr/src/app/bin/node_gc |
2 | 2 |
|
3 | | -var CODE_DIR = "/code"; |
| 3 | +const CODE_DIR = "/code"; |
4 | 4 | process.chdir(CODE_DIR); |
5 | 5 |
|
6 | | -// Redirect `console.log` so that we are the only ones |
7 | | -// writing to STDOUT |
8 | | -var stdout = console.log; |
9 | | -console.log = console.error; |
10 | | - |
11 | | -var eslint = require('../lib/eslint-patch')(require('eslint')); |
12 | | - |
13 | | -var CLIEngine = eslint.CLIEngine; |
14 | | -var docs = eslint.docs; |
15 | | -var fs = require("fs"); |
16 | | -var glob = require("glob"); |
17 | | -var options = { extensions: [".js"], ignore: true, reset: false, useEslintrc: true }; |
18 | | -var cli; // instantiation delayed until after options are (potentially) modified |
19 | | -var debug = false; |
20 | | -var BatchSanitizer = require("../lib/batch_sanitizer"); |
21 | | -var ignoreWarnings = false; |
22 | | -var ESLINT_WARNING_SEVERITY = 1; |
23 | | -var checks = require("../lib/checks"); |
24 | | -var validateConfig = require("../lib/validate_config"); |
25 | | -var computeFingerprint = require("../lib/compute_fingerprint"); |
26 | | -const ConfigUpgrader = require("../lib/config_upgrader"); |
27 | | - |
28 | | -// a wrapper for emitting perf timing |
29 | | -function runWithTiming(name, fn) { |
30 | | - var start = new Date() |
31 | | - , rv = fn() |
32 | | - , duration = (new Date() - start) / 1000; |
33 | | - if (debug) { |
34 | | - console.error("eslint.timing." + name + ": " + duration + "s"); |
35 | | - } |
36 | | - return rv; |
37 | | -} |
38 | | - |
39 | | -function contentBody(check) { |
40 | | - var content = docs.get(check) || "For more information visit "; |
41 | | - return content + "Source: http://eslint.org/docs/rules/\n"; |
42 | | -} |
43 | | - |
44 | | -function buildIssueJson(message, path) { |
45 | | - // ESLint doesn't emit a ruleId in the |
46 | | - // case of a fatal error (such as an invalid |
47 | | - // token) |
48 | | - var checkName = message.ruleId; |
49 | | - if(message.fatal) { |
50 | | - checkName = "fatal"; |
51 | | - } |
52 | | - var line = message.line || 1; |
53 | | - var column = message.column || 1; |
54 | | - |
55 | | - var issue = { |
56 | | - type: "issue", |
57 | | - categories: checks.categories(checkName), |
58 | | - check_name: checkName, |
59 | | - description: message.message, |
60 | | - content: { |
61 | | - body: contentBody(checkName) |
62 | | - }, |
63 | | - location: { |
64 | | - path: path, |
65 | | - positions: { |
66 | | - begin: { |
67 | | - line: line, |
68 | | - column: column |
69 | | - }, |
70 | | - end: { |
71 | | - line: line, |
72 | | - column: column |
73 | | - } |
74 | | - } |
75 | | - }, |
76 | | - remediation_points: checks.remediationPoints(checkName, message, cli.getConfigForFile(path)) |
77 | | - }; |
78 | | - |
79 | | - var fingerprint = computeFingerprint(path, checkName, message.message); |
80 | | - |
81 | | - if (fingerprint) { |
82 | | - issue["fingerprint"] = fingerprint; |
83 | | - } |
84 | | - |
85 | | - return JSON.stringify(issue); |
86 | | -} |
87 | | - |
88 | | -function isFileWithMatchingExtension(file, extensions) { |
89 | | - var stats = fs.lstatSync(file); |
90 | | - var extension = "." + file.split(".").pop(); |
91 | | - return ( |
92 | | - stats.isFile() && |
93 | | - !stats.isSymbolicLink() |
94 | | - && extensions.indexOf(extension) >= 0 |
95 | | - ); |
96 | | -} |
97 | | - |
98 | | -function isFileIgnoredByLibrary(file) { |
99 | | - return cli.isPathIgnored(file); |
100 | | -} |
101 | | - |
102 | | -function prunePathsWithinSymlinks(paths) { |
103 | | - // Extracts symlinked paths and filters them out, including any child paths |
104 | | - var symlinks = paths.filter(function(path) { |
105 | | - return fs.lstatSync(path).isSymbolicLink(); |
106 | | - }); |
107 | | - |
108 | | - return paths.filter(function(path) { |
109 | | - var withinSymlink = false; |
110 | | - symlinks.forEach(function(symlink) { |
111 | | - if (path.indexOf(symlink) === 0) { |
112 | | - withinSymlink = true; |
113 | | - } |
114 | | - }); |
115 | | - return !withinSymlink; |
116 | | - }); |
117 | | -} |
118 | | - |
119 | | -function inclusionBasedFileListBuilder(includePaths) { |
120 | | - // Uses glob to expand the files and directories in includePaths, filtering |
121 | | - // down to match the list of desired extensions. |
122 | | - return function(extensions) { |
123 | | - var analysisFiles = []; |
124 | | - |
125 | | - includePaths.forEach(function(fileOrDirectory, i) { |
126 | | - if ((/\/$/).test(fileOrDirectory)) { |
127 | | - // if it ends in a slash, expand and push |
128 | | - var filesInThisDirectory = glob.sync( |
129 | | - fileOrDirectory + "/**/**" |
130 | | - ); |
131 | | - prunePathsWithinSymlinks(filesInThisDirectory).forEach(function(file, j){ |
132 | | - if (!isFileIgnoredByLibrary(file) && isFileWithMatchingExtension(file, extensions)) { |
133 | | - analysisFiles.push(file); |
134 | | - } |
135 | | - }); |
136 | | - } else { |
137 | | - if (!isFileIgnoredByLibrary(fileOrDirectory) && isFileWithMatchingExtension(fileOrDirectory, extensions)) { |
138 | | - analysisFiles.push(fileOrDirectory); |
139 | | - } |
140 | | - } |
141 | | - }); |
142 | | - |
143 | | - return analysisFiles; |
144 | | - }; |
145 | | -} |
146 | | - |
147 | | -var buildFileList; |
148 | | -runWithTiming("engineConfig", function () { |
149 | | - if (fs.existsSync("/config.json")) { |
150 | | - var engineConfig = JSON.parse(fs.readFileSync("/config.json")); |
151 | | - |
152 | | - if (engineConfig.include_paths) { |
153 | | - buildFileList = inclusionBasedFileListBuilder( |
154 | | - engineConfig.include_paths |
155 | | - ); |
156 | | - } else { |
157 | | - // No explicit includes, let's try with everything |
158 | | - buildFileList = inclusionBasedFileListBuilder(["./"]); |
159 | | - } |
160 | | - |
161 | | - var userConfig = engineConfig.config || {}; |
162 | | - if (userConfig.config) { |
163 | | - options.configFile = CODE_DIR + "/" + userConfig.config; |
164 | | - options.useEslintrc = false; |
165 | | - } |
166 | | - |
167 | | - if (userConfig.extensions) { |
168 | | - options.extensions = userConfig.extensions; |
169 | | - } |
170 | | - |
171 | | - if (userConfig.ignore_path) { |
172 | | - options.ignorePath = userConfig.ignore_path; |
173 | | - } |
174 | | - |
175 | | - if (userConfig.ignore_warnings) { |
176 | | - ignoreWarnings = true; |
177 | | - } |
178 | | - |
179 | | - if (userConfig.debug) { |
180 | | - debug = true; |
181 | | - } |
182 | | - } |
183 | | - |
184 | | - cli = new CLIEngine(options); |
185 | | -}); |
186 | | - |
187 | | -var analysisFiles = runWithTiming("buildFileList", function() { |
188 | | - return buildFileList(options.extensions); |
189 | | -}); |
190 | | - |
191 | | -function analyzeFiles() { |
192 | | - var batchNum = 0 |
193 | | - , batchSize = 10 |
194 | | - , batchFiles |
195 | | - , batchReport |
196 | | - , sanitizedBatchFiles; |
197 | | - |
198 | | - while(analysisFiles.length > 0) { |
199 | | - batchFiles = analysisFiles.splice(0, batchSize); |
200 | | - sanitizedBatchFiles = (new BatchSanitizer(batchFiles)).sanitizedFiles(); |
201 | | - |
202 | | - if (debug) { |
203 | | - process.stderr.write("Analyzing: " + batchFiles + "\n"); |
204 | | - } |
205 | | - |
206 | | - runWithTiming("analyze-batch-" + batchNum, function() { |
207 | | - batchReport = cli.executeOnFiles(sanitizedBatchFiles); |
208 | | - }); |
209 | | - runWithTiming("report-batch" + batchNum, function() { |
210 | | - batchReport.results.forEach(function(result) { |
211 | | - var path = result.filePath.replace(/^\/code\//, ""); |
212 | | - |
213 | | - result.messages.forEach(function(message) { |
214 | | - if (ignoreWarnings && message.severity === ESLINT_WARNING_SEVERITY) { return; } |
215 | | - |
216 | | - var issueJson = buildIssueJson(message, path); |
217 | | - process.stdout.write(issueJson + "\u0000\n"); |
218 | | - }); |
219 | | - }); |
220 | | - }); |
221 | | - runWithTiming("gc-batch-" + batchNum, function() { |
222 | | - batchFiles = null; |
223 | | - batchReport = null; |
224 | | - global.gc(); |
225 | | - }); |
226 | | - |
227 | | - batchNum++; |
228 | | - } |
229 | | -} |
230 | | - |
231 | | -if (validateConfig(options.configFile)) { |
232 | | - console.error("ESLint is running with the " + cli.getConfigForFile(null).parser + " parser."); |
233 | | - |
234 | | - for (const line of ConfigUpgrader.upgradeInstructions(options.configFile, analysisFiles, process.cwd())) { |
235 | | - console.error(line); |
236 | | - } |
237 | | - |
238 | | - analyzeFiles(); |
239 | | -} else { |
240 | | - console.error("No rules are configured. Make sure you have added a config file with rules enabled."); |
241 | | - console.error("See our documentation at https://docs.codeclimate.com/docs/eslint for more information."); |
242 | | - process.exit(1); |
243 | | -} |
| 6 | +const ESLint = require("../lib/eslint"); |
| 7 | +ESLint.run(console, { dir: CODE_DIR }); |
0 commit comments