-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathrunner.js
More file actions
218 lines (199 loc) · 7.13 KB
/
Copy pathrunner.js
File metadata and controls
218 lines (199 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const readline = require('readline');
const path = require('path');
const { fileURLToPath } = require('url');
const fsPromises = require('fs').promises;
const {ESLINT_ERROR_LINT_FAILED, ESLINT_ERROR_MODULE_LOAD_FAILED, ESLINT_ERROR_MODULE_NOT_FOUND,
OPERATION_LINT_TEXT, OPERATION_QUIT, OPERATION_RESPONSE, OPERATION_GET_LOADED_VERSION
}
= require("./constants");
if (!process.argv[2]) {
console.error('Error: ESLintModulePath first argument is not set when running ESLint/runner.js');
process.exit(1);
}
if (!process.argv[3]) {
console.error('Error: projectRootPath second argument is not set when running ESLint/runner.js');
process.exit(1);
}
// Get ESLint full path from the environment variable
const ESLintModulePath = path.resolve(process.argv[2]);
const projectRootPath = path.resolve(process.argv[3]);
const lintFilePath = process.argv[4] ? path.resolve(process.argv[4]) : null; // this is just for testing with console
const fs = require('fs');
function sendToPHNode(jsObj) {
console.log(JSON.stringify(jsObj));
}
async function checkExists(directoryPath, isDir = true) {
try {
const stats = await fsPromises.stat(directoryPath);
return isDir ? stats.isDirectory() : stats.isFile();
} catch (error) {
return false;
}
}
// Dynamically require the ESLint module
let ESLintCached, configFileError;
async function getESLintModule() {
if(ESLintCached){
return ESLintCached;
}
try{
const directoryExists = await checkExists(ESLintModulePath);
if(!directoryExists){
return null;
}
const ESLintModule = require(ESLintModulePath);
ESLintCached = ESLintModule.ESLint;
configFileError = null;
return ESLintCached;
} catch (e) {
console.error("ESLint runner: failed to load ESLintModule", e);
}
return null;
}
async function getESLinter() {
const ESLint = await getESLintModule();
if(!ESLint){
return null;
}
return new ESLint({ cwd: projectRootPath });
// consider caching this if performance is not adequate.
// when caching, make sure that a new eslint object is created when any of the eslint config file changes!!!.
}
async function _getConfigDetails(eslinter, filePath) {
try{
const config = await eslinter.calculateConfigForFile(filePath);
return JSON.parse(JSON.stringify(config)); // ensure that this is stringify able
} catch (e) {
console.error("Failed to compute config", e);
return null;
}
}
async function lintTextWithPath(text, fullFilePath) {
// Create an ESLint instance
const eslinter = await getESLinter();
if(!eslinter){
const directoryExists = await checkExists(ESLintModulePath);
return {
isError: true,
errorCode: directoryExists ? ESLINT_ERROR_MODULE_LOAD_FAILED : ESLINT_ERROR_MODULE_NOT_FOUND,
configFileError
};
}
const isPathIgnored = await eslinter.isPathIgnored(fullFilePath);
if(isPathIgnored) {
return {
isPathIgnored
};
}
// Lint the project directory
const result = (await eslinter.lintText(text, {
filePath: fullFilePath
}))[0];
// Return the results as an array
delete result.source;
return {
result,
config: await _getConfigDetails(eslinter, fullFilePath)
};
}
const ESLINT_CONFIG_JS_FILE_NAMES = [
// eslint 9
"eslint.config.js",
"eslint.config.mjs",
"eslint.config.cjs",
// legacy
".eslintrc.js",
".eslintrc.cjs"
];
function getTopmostStackFileName(error, nameOnly) {
let fileName = null;
try{
if (error.stack) {
const stackLines = error.stack.split('\n');
for (let i=0; i<stackLines.length; i++) {
const topmostStackLine = stackLines[i];
// Extract the file name using a regular expression
const match = topmostStackLine.match(/\((.*):\d+:\d+\)/);
if (match) {
fileName = match[1];
break;
} else {
// In case the format is different, try another pattern
const alternativeMatch = topmostStackLine.match(/at (.*):\d+:\d+/);
if (alternativeMatch) {
fileName = alternativeMatch[1];
break;
}
}
}
}
if(fileName && fileName.startsWith("file://")) {
fileName = fileURLToPath(fileName);
}
// now convert the full path to name
if(nameOnly){
fileName = path.basename(fileName);
}
} catch (e) {
console.error("Error getting topmost stack file", e);
}
return fileName;
}
if(lintFilePath) {
const text = fs.readFileSync(lintFilePath, { encoding: 'utf8' });
lintTextWithPath(text, lintFilePath)
.then(console.log)
.catch(err=>{
console.error(err);
const topmostFileName = getTopmostStackFileName(err);
console.log('Topmost stack file name:', topmostFileName);
});
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// each event is a json object
rl.on('line', (input) => {
try {
if(!input.trim()){
return; // empty line
}
// {operation, text, fullFilePath, requestID}
const eslintRequest = JSON.parse(input);
if(eslintRequest.operation === OPERATION_LINT_TEXT) {
lintTextWithPath(eslintRequest.text, eslintRequest.fullFilePath)
.then(result=>{
result.requestID = eslintRequest.requestID;
result.operation = OPERATION_RESPONSE;
sendToPHNode(result);
}).catch(err=>{
console.error("ESlint Runner error:", err);
const errorCausedFile = getTopmostStackFileName(err, true);
let errorMessage = err.message || "";
if(errorCausedFile && ESLINT_CONFIG_JS_FILE_NAMES.includes(errorCausedFile)){
errorMessage = `${getTopmostStackFileName(err)}: ${errorMessage}`;
}
sendToPHNode({
operation: OPERATION_RESPONSE,
requestID: eslintRequest.requestID,
isError: true,
errorMessage: errorMessage,
errorCode: ESLINT_ERROR_LINT_FAILED
});
});
} else if(eslintRequest.operation === OPERATION_QUIT) {
process.exit(0);
} else if(eslintRequest.operation === OPERATION_GET_LOADED_VERSION) {
sendToPHNode({
operation: OPERATION_RESPONSE,
requestID: eslintRequest.requestID,
esLintVersion: ESLintCached && ESLintCached.version
});
} else {
console.error("ESLint runner: Unknown operation: ", eslintRequest);
}
} catch (error) {
console.error('ESLint runner:: Error while processing message:', error.message);
}
});